How To Delete A Directory In Linux

By Abby •  Updated: 05/06/20 •  4 min read

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.

24/7 Live Chat
Ask a Technician To Help You Cancel or Delete Live

Chat with a Tech Support Specialist anytime, anywhere

how to delete a directory in Linux

How to delete a directory in Linux

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. 

24/7 Live Chat
Ask a Technician To Help You Cancel or Delete Live

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 

Delete 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:

Deleting all empty directories

To delete all empty directories in a directory tree:

$ find /dir -type d -empty -delete

Breakdown:

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. 

(Visited 72 times, 1 visits today)

Abby