...

How do I force delete a directory in Linux? nixCraft

how-do-i-force-delete-a-directory-in-linux-nixcraft

I am trying to delete a directory in Linux using the rmdir dir1 command. But, I am getting an error that read as follows:
    rmdir: failed to remove `dir1': Directory not empty
How do I force remove and delete a directory in Linux?

A directory is nothing but a location for storing files on the Linux operating system in a hierarchical format. For example, /bin/ directory would store all executable binary files. This page shows how to remove a directory forcefully when it is not empty using the bash shell command prompt.

How to force delete a directory in Linux

  1. Open the terminal application on Linux.
  2. The rmdir command removes empty directories only. Hence you need to use the rm command to remove files on Linux.
  3. Type the command rm -rf dirname to delete a directory forcefully.
  4. Verify it with the help of ls command on Linux.

How to remove a directory that contains other files or directories

The syntax is:
rm -rf dirName
Say you have a directory named /tmp/data/ that contains two files and one directory as follows:
ls -l /tmp/data/
If you run the rmdir command, you will get an error as follows:
rmdir /tmp/data/
As explained earlier rmdir only delete the DIRECTORIES, if they are empty. Therefore you must use the rm command to remove a full directory in Linux:
rm -rf /tmp/data/
Verify it:
ls -l /tmp/data/

Force delete a directory in Linux using rm command
Use rm command to delete a non-empty directory in Linux terminal

How do I remove a full directory in Linux with verbose output?

Pass the -v option to the rm command as follows:
rm -rfv dirname
For example, delete a full directory named /tmp/bar in Linux and note down the output on the screen:
rm -rfv /tmp/bar/
Remove a full directory in Linux
Where,

  • -r : Recursive delete
  • -f : Force delete a directory
  • -v : Verbose output

Delete a non-empty directory in Linux terminal

In case if you don’t have the permission to delete the folder run rm command as a root user. Otherwise, you will see permission denied message on the screen. Therefore to avoid that prefix sudo at the beginning of the rm command :
sudo rm -rf dirName
OR
sudo rm -rf /somedir/
To remove a folder whose name starts with a ‘-‘, for example ‘-backups’, use one of these commands:
rm -rfv -- -backups/
OR
rm -rfv ./-bacups/

Learn more about rm command

Run:
rm --help
Sample outputs:

 -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root[=all] do not remove '/' (default); with 'all', reject any command line argument on a separate device from its parent -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit

Conclusion

This page explained how to use the rm command to remove a non-empty directory in Linux. The rmdir command only deletes non-empty directories, so we need to use the rm command.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading