How to Remove Directories in Linux

Spring cleaning is not only necessary for homes, Your Linux Mint 20 file system also needs to be decluttered regularly. An organized file system will save you or the system administrator a lot of headaches in the long run.

In this article, you will learn how to remove your directories to organize your file system.

Pre-requisites

  • Linux system, I'll use Linux Mint 20 here.
  • Terminal access
  • A user account with sudo privileges.

Note: All the methods used in this tutorial will remove the directory and its files permanently and can not be recovered, so make sure to back up the important files and directories before proceeding.

Find Your Current Working Directory

First of all, you will need your directory name/path to follow this tutorial. In case you do not know your current directory, run the following command in the terminal to find out your directory path.

pwd

Get working directory

List Down Directories/Files in Your Current Directory

List down all the directories and files in your current directory and verify the deleted directories after every with the following command.

ls

List directories

Remove Directories Using rmdir Command

To delete the directory, run the following command.

rmdir <directory full path>

Delete directory

Note: The rmdir command will only delete the empty directories. Otherwise, it will give the following error output.

Remove directory

Remove Directories Using rm Command

The rm command can delete any directory, unlike the rmdir command which only works for empty directories.

To remove the directories, use the following command syntax with the -r option.

rm -r <directory full path>

Remove directory recursively

You can see in the above snippet that test1 is deleted and not listed in the directory.

To delete multiple directory at once, run the following command.

rm -r <directory full path> <directory full path> ...

Delete multiple directories

If you want confirmation on deleting a directory, run the rm command with the ā€˜Iā€™ option.

rm -rI <directory full path>

Confirm deleting directory

Remove Directories Using find Command

If you are aiming at deleting multiple directories and do not know exactly where targeted files are at. Or simply do not want to list long commands with a long line of argument then you should use the find command to simultaneously search directories for target and delete them using one command.

For example, use the following command to delete all the directories with a certain name pattern.

find . -type d -name '<name pattern>' -exec rm -r {} +

Note: Remember to set your directory name pattern before running the above command.

Combine find and rm command

Similarly, run the following command to delete all the empty subdirectories in a particular directory.

find <directory path> -type d -empty -delete

Delete empty directories

How to solve the permission denied error?

If any directory or file in the directory has restrictions like read-only etc, You will get the following outcome with a permission denied msg.

Permission denied error when removing a directory

In that case, you simply need to run the respective command with a sudo/root privileged account.

Use sudo to prevent permission denied errors

Leave a Comment

*