There are various ways to delete directories in a Linux operating system. If you are rocking a desktop file manager like Gnome’s Files or KDE’s Dolphin, you can delete files and directories through the file manager’s user interface with ease. However, you can delete directories (also known as folders) from the command line.
Chat with a Tech Support Specialist anytime, anywhere
In this article, we will teach you how to delete directories in Linux using the commands; rmdir, rm and find.
Before Deleting Directory
You can recover the deleted directory using the desktop file manager, but removing directory using the commands is permanent. So, be careful when executing these commands as you may end up permanently deleting other directories containing essential files.
Delete a directory using rmdir
rmdir command allows you to only delete specified empty directories/folders in Linux. It is handy as you don’t have to check whether the specified directory is empty or not.
Chat with a Tech Support Specialist anytime, anywhere
To remove a directory using rmdir, type rmdir and the name of the directory you wish to delete.
For example, To delete a folder named photos:
$rmdir photos
If the directory is not empty, you will be shown error statement:
rmdir : failed to remove ‘photos’ : No such file or directory
In these sorts of conditions, it is preferable to use the rm command line or remove all the files inside the directory before trying again.
Delete a Directory using rm command
rm is an essential command-line utility that allows you to delete files and directories. Compared to rmdir, rm command can remove both empty and non-empty directories.
When rm is used without any option, it doesn’t delete directories. For deleting an empty directory, you have to use -d (–dir) option and to remove a non-empty directory, and for deleting all its files use -r (–recursive or -R) option.
For example to delete a directory named photos, including all its files:
$rm -r photos
If the directory is write-protected, you will be asked for confirmation.
To delete a directory without any need for confirmation prompt, use the -f option:
$ rm -rf photos
Get step-by-step instructions from verified Tech Support Specialists to Delete, Cancel or Uninstall anything in just minutes.
Ask Tech Support Live NowDelete multiple directories using rm command at once
If you want to delete multiple directories at once then follow as below:
Syntax: $rm -r (direcotry name) [SPACE] (directory name)
Example: $rm -r dir1 dir2 dir3
You can use the -i option following rm to prompt for confirmation on deletion of each subdirectory and fil. If there is a large number of files, it can get a bit irritating. For that, you can use the -I which will only prompt once before start deleting.
For example: $ rm -rI [directory name]
You can also imply the regular expansions to find and delete multiple directories. For example, to delete all the first-level directories in the current directory that ends with _cas, you can use:
$ rm -r *_cas
However, you should use this very carefully as you may end up regretting it. We strongly recommend you list the directories with the ls command to see what directories will get deleted before running the command.
Deleting Directories using find
find command lets you search files and directories on the base of given expression and perform an action on each matching file or directory.
For example to delete all directories ending with _bas in the current directory:
$ find . -type d -name ‘*_bas’ -exec rm -r {} +
Breakdown:
- /dir : recursively search in the current directory.
- -type d :limits the search to directories.
- -name ‘*_bas’ : only search directories ending with _bas.
- -exec : performs an external command with option arguments, rm -r.
- {} + : adds the detected files to the end of the rm command.
Deleting all empty directories
To delete all empty directories in a directory tree:
$ find /dir -type d -empty -delete
Breakdown:
- /dir: recursively search inside /dir directory
- -type d: limits the search to directories:
- -empty – limits the search to only empty directories.
- -delete: removes all found empty directories in the subtree. It can only remove the empty directories.
Be very careful while using the -delete option. The find command is assessed as an expression, and if you include the -delete option first, the command will delete everything below the options you specified.
We strongly recommend you use the –delete option and the last resort.
Abby
Get step-by-step instructions from verified Tech Support Specialists to Delete, Cancel or Uninstall anything in just minutes.
Ask Tech Support Live Now