how to remove or delete a directory in linux

There are several different ways to delete folders or directories in Linux file systems. If you use a graphical file browser such as dolphin or nautilus then it provides an easy enough way to delete directory, files and sub-folders. But, sometimes you have access only to the command line and want to delete directory from the command line. Using the command line is also very useful when you need to delete nested folders selectively.

The commands that you will most likely need to use are either rmdir and/or rm.

Using rmdir

rmdir is a specialized form of the rm command and works solely on empty directories. This command removes directories if and only if they are empty, meaning it does not contain any other files or directories inside them. The advantage of using this command is that it is pretty much impossible to accidentally remove files or folders.

In order to remove a folder named samples inside the /tmp directory, you can use

bash$ rmdir /tmp/samples

if the samples directory is not empty, and say it contains another directory called sampleone, then you will receive the following error in the output

rmdir: failed to remove 'samples': Directory not empty

You will need to manually remove all of the contents inside the samples folder before you can delete it.

The rmdir provides the option (-p or –parents) to remove the directories recursively,  provided each of those directories are empty. The command works in a bottom up pattern so that the inner most directory is deleted first. The following command will remove sampletwo first, then sampleone and then finally samples.

bash$ rmdir -p /tmp/samples/sampleone/sampletwo/

The command will fail as soon as it finds a non-empty directory in its path, but the already deleted directories stay deleted. In the above command, the /tmp directory will not deleted because it is very unlikely that the /tmp directory will be empty on a running system. Still, the following command is a better option to make sure that the /tmp directory is not deleted…

bash$ cd /tmp
bash$ rmdir -p samples/sampleone/sampletwo

Using rm

Another handy command which removes folders is the rm command. Unlike the rmdir command this is a much powerful command and can be used to remove both files and folders. Being powerful also means that you are much more likely to accidentally delete directory or files that you did not intend to if you are not careful.

By default, rm command does not support deletion of directories. You need to use either the -r or the -d option explicitly if you need to delete the directories. This is mostly a safe guard against accidental deletion of directories and files.

The -d or –dir option allows you to remove empty directories and only empty directories, much like the rmdir command mentioned above.

The option –recursive (or -r or -R) allows you to remove directories recursively. The recursive nature of rm is not just limited to directories but also to files inside the directories, which means all your files inside the directories and sub-directories are fair game.

So in order to remove directories recursively with minimum fuss, you can use the -f (or –force) option. This removes everything that you specified without any further questions. So, to remove the directory samples in the tmp folder

bash$ rm -fr /tmp/samples

In order to avoid accidentally deleting files that might be inside the nested folders, you can use the interactive mode. The option -i prompts you before removing every single file. Although useful when deleting a small number of files, this can get pretty annoying and tiresome when you are deleting a large number of files. There is an alternative in -I option which prompts only once while removing more than three files or when the recursive option is used.

Using wildcards

In order to remove all the files and sub directories inside a directory with out actually removing the parent directory itself, you can use the wildcard * (star).

bash$ rm -fr /tmp/samples/*

The above command will remove all the files and folders inside the samples/ directory, and will leave samples as an empty directory. The * wildcard is also useful in matching parts of the filename or extensions, for example

bash$ rm -fr /tmp/samples/*.jpg

This will remove all the files (and folders) with a file extension .jpg.

Using Alias for remove commands

As the rm command can cause a lot of pain when used incorrectly, it is always a good idea to put some safe guards in place. One thing you can do is to override the rm command with an alias for the command shell. By adding the interactive prompt option, the system will prompt you even if forget to use the -i option. Also, a handy option is the –verbose which tells you what you did, albeit sometimes too late.

bash$ alias rm="rm -iv"
bash$ alias rmdir="rmdir -v"

You can put these alias into your .bashrc file, to make them permanent on your shell prompts. Even though the interactive mode is now default for the rm, you can always override it by using the -f option. The -f option used along with the -i option will override interactive mode.

Another way to temporarily override the alias (or unalias as some might say) is to use the “\” (backslash) before the command. The following example shows you how you can use the rm command without the -i or -v option, though it is set as an alias.

bash$ \rm /tmp/samples/

This can be useful when deleting a very large number of files and the verbose mode can get rather time consuming.

Find Command

Another (maybe advanced) command that can used to remove the folders (and files) is the find command. It can be very useful in removing files and folders recursively when used with the -delete option or with the rm command.

In order to remove only files from within all the folders and sub-folders but to leave the directory structure alone, you can use the following command.

bash$ find /tmp/samples -type f -delete

In order to remove all the empty sub directories while leaving the files and non-empty directories alone, use the -type d option.

bash$ find /tmp/samples -type d -delete

To forcefully remove the directories, you can use the find command along with the rm command as shown below

bash$ find /tmp/samples -type d -exec rm -fr {} \;

The above command will remove all the sub directories along with the files inside them, but will leave the files in the top level directory untouched. If you like to further filter the directories or sub-directories based on the name, you can do that with the find command.

bash$ find /tmp/samples -type d -name "log" -exec rm -fr {} \;

This command will remove all the folders (and sub-folders)  with the name log along with the files inside those folders.