How to delete a directory in Linux

Once in a while, you might want to remove a directory in Linux to free up space or simply get rid of unwanted files or directories. Deleting a directory in Linux is quite easy and straight-forward and should work in all Linux distributions such as Ubuntu and Fedora. This guide takes the following approaches:

  1. Deleting an empty directory
  2. Deleting a directory with content ( files and subdirectories ).

In this tutorial, you will learn how to delete or remove a directory in Linux. For this guide, I’ll be using an instance of Ubuntu virtual machine. Without much further ado, let’s roll our sleeves and dig in.

How to remove an empty directory in Linux

To remove an empty directory, use the

rmdir
command, short for remove directory. Using the rmdir command only work if and only if the directory is empty.

The syntax for deleting an empty is as follows:

$ rmdir  directory_name

For example, to remove an empty directory, say , mydirectory, execute the command:

$ rmdir mydirectory

To demonstrate this, we are first going to create a directory in Linux and later delete it. To create a directory, use the mkdir command as shown:

$ mkdir mydirectory
how to create a directory in linux

To remove the directory, execute:

$ rmdir mydirectory
how to remove a directory in Linux

NOTE:

If the directory is not owned/created by the user, prefix the command with the sudo command. This grants sufficient privileges to the user deleting the directory, otherwise you will get a permissions’ error. Also, ensure that the user has been added to the sudoers group to be able to carry out elevated tasks.

Therefore, if you are deleting an empty directory owned by root or another user, use the following syntax:

$ sudo rmdir directory_name
remove a directory in linux using sudo command

What happens if you try to remove a non-empty directory? In that case, you will get an error splashed on the screen as shown”

'rmdir: failed to remove ‘mydirectory’: Directory not empty'
error while removing a directory in Linux

 

How to remove a directory with files and folder in Linux

As you saw previously, removing a non-empty directory using the rmdir command fails and, instead, prints out an error on the terminal.

To resolve this issue, use the rm command. Short for remove command, the rm command is used to remove not only ordinary files but both empty and non-empty directories. The command is often used in conjunction with other options to delete directories. the syntax for using the rm command is as follows:

$ rm [options] directory_name

If you want to delete a directory, use the -r or -R options to recursively delete the directory. This deletes the entire directory and its files and sub-directories.

$ rm -r directory_name
remove a directory recusrively

Again, use the sudo command if the user you are logged in as does not have ownership to the directory.

$ sudo rm -r directory_name

In some scenarios, you might stumble upon a write-protected directory that keeps prompting you if you would like to delete the files therein and the entire directory. To avoid getting such prompts which can be somewhat annoying and disruptive, append the -f argument to force deletion of all the files and directory without getting prompts.

$ rm -rf directory_name

Also, you can remove or delete several directories simultaneously in one command as shown:

$ rm -rf directory1 directory2 directory3

Be advised that the command deletes all the directories recursively i.e alongside their associated files and sub-directories. If you want to delete a directory recursively with the 0f option, you need to be cocksure about the directory’s contents.

Alternatively, you can use the -i option which gives you an interactive session and prompts if you would like to proceed to delete the sub-directories inside a directory. This comes in handy especially when you are unsure of some of the contents inside the directory.

$ rm -ri directory_name
prompt when deleting a directory

The downside to using the -i option is that it will keep prompting you to delete each file inside the directory. Imagine a scenario where you have a hundred or a thousand files you need to delete. Your guess is as good as mine how cumbersome that would be.

To remedy this problem, use the -I option which will only prompt you once when deleting each subfolder.

$ rm -rI directory_name
I option when deleting a directory

As mentioned earlier, the rm command can also be used to remove an empty directory. To accomplish this, simply use the -d option as shown:

$ sudo rm -d empty_directory

Delete directories using the find command

Traditionally, find command is used to search and locate files and directories based on a certain criterion. In addition, the find command can be used to find and delete files and directories that you want to get rid of.

For example, to delete a directory called directory, run the command:

$ sudo find . -type d -name "directory" -exec rm -rf {} +

Let’s look closer at some of the options in this command:

Period sign (.) – This indicates that the search operation is taknig place in the current working directory.

-type d – This option specifies that the search operation should return directories only and not files.

-name – This specifies the directory name.

-exec rm -rf – This forces the deletion of the directory alongside its subdirectories and files.

{} + – The option appends all the files at the end of the rm command.

 

Removing empty directories using find command

To purge all empty directories using find command, simply execute the command:

$ sudo find. -type d -empty -delete

 

Conclusion

We have covered avenues that you can use to delete directories on Linux: both empty and non-empty directories. We hope that going forward, you will not be stuck trying to delete directories from your system.

If you want to delete a single file, you can use the command “unlink” instead of “rm.” The syntax of the unlink command is similar to the rm command:

$ unlink filename

The unlink command shares another similarity with the rm command – once the commands are used to remove a file, the file becomes irrecoverable.

Learning to delete files from a Linux machine is a part of the process of becoming a Linux power user.

Now that you’ve learned how to do it, you are one step closer to mastering Linux. You can check out our other posts for more Linux guides.