Directories are used to store other directories of files. A directory can be even empty. Linux provides different commands in order to delete directory or directories. Command-line or GUI file managers can be used to delete or remove directories in Linux. In this tutorial, we will examine rm, rmdir, find commands alongside Nautilus File Manager in order to delete empty, or non-empty directories. by the way, directories are also called folders where you can think these operations as deleting or removing folders.
Remove/Delete Directories with rm Command
rm
command is the most popular and useful command which can be used to remove or delete directories. rm command also used to files. rm command provides a lot of different and useful features. Lets start examples to delete different types of directories.
rm command is created to delete files but can be used to delete directories by using the -d
or --dir
option. Below we will delete single and multiple directories with the rm command and the -d option. But this will only delete empty directories.
$ rm -d empty1
$ rm -d empty1 test2
$ rm -d empty1/another
If we want to delete directories which already have content we should use the -r
, -R
or --recursive
option to delete directories and their contents.
$ rm -d -r empty1
$ rm -d -r empty1 test2
$ rm -d -r empty1/another
Sometimes the directories can be set as write-protected in order to prevent being charged. If we try to delete these write-protected directories we will get an error and the directories will not be deleted. In order to delete the write-protected directories, we should force the delete operation with the -f
option.
$ rm -r -f empty1
$ rm -r -f empty1 test2
$ rm -r -f empty1/another
Deleting important directories like configuration, password, user data etc. is a bit stressful. During the removal operation we can make mistakes and delete uncecassary directories. We can prevent this and ask for each directory deletion operation and confirm by using the -i
parameter.
$ rm -r -f empty1
$ rm -r -f empty1 test2
$ rm -r -f empty1/another
$ rm -r -i test
We can also delete directories according to the names by using glob operations. *
is used to match any name and using it inside the name of directory will give us the ability to configure directory name patter. For example in the following example we will delete directories which name end with the tmp
which is assumed that temporary directory.
$ rm -rf *tmp
or we can delete old directories which names start with the old and changes according to the situation.
$ rm -rf old*
We can also implement other rm command options like -i
.
$ rm -rfi old*
By default deleted directories do not listed on the command line except for the -i
option. But if we want to see or log delete files by deleting them we can provide the --verbose
or -v
option like below.
$ rm -rf -v data
Remove/Delete Directories with rmdir Command
rmdir
is specifically designed to remove empty directories. If the directory is not empty rmdir can not remove it. You may ask why the rmdir command created to only delete empty directories. It is created to prevent errors and deletion of the directories that contain data and remove only empty directories.
$ rmdir test1
If you get an error like “rmdir: failed to remove ‘test1’: Directory not empty” just use the rm command with recursive and force options because rmdir can not delete non-empty directories.
$ rmdir a b c
Remove/Delete Directories with find Command
find
is a command which is created and used to find files and folders. This may sound a bir strage beucase using the find command to delete directories. find command provides the ability to run commands over matched files and folders. So you can run the rm command over the found directories easily. But first you should set the search type as directory with the -type d
. Optional but you will also set some name with the -name
option. The -exec
optoin is used to call rm command over matched directories like below.
$ find /var -type d -name "*tmp" -exec rm -r {} +
By using the following find command only empty directories can be removed easily for the specified path. We will provide the -empty
and -delete
options to the find command. So we will not execute rm command explicitly.
$ find /var -type d -delete -empty
Remove/Delete Directories with Nautilus Files Manager
Linux also provides different GUI and files managers in order to manage files. These files managers can be used to remove or delete directories. They also provide easier usage than the rm, rmdir and find commands. Just navigate to the path the directory you want to delete and right-click on the directory. This menu provide the Move to Trash
or Delete
or similar operation like below.