How To Remove A Directory in Linux - Nixcraft
How To Remove A Directory in Linux - Nixcraft
Tutorial details
Root privileges No
AIX • Alma • Alpine • Arch • BSD • Debian • Fedora • FreeBSD • HP-UX • Linux •
➔ nixCraft is a one-person operation. I create all the content myself, with no help
from AI or ML. I keep the content accurate and up-to-date.
➔ Your privacy is my top priority. I don’t track you, show you ads, or spam you with
emails. Just pure content in the true spirit of Linux and FLOSS.
➔ Fast and clean browsing experience. nixCraft is designed to be fast and easy to
use. You won’t have to deal with pop-ups, ads, cookie banners, or other distractions.
➔ Support independent content creators. nixCraft is a labor of love, and it’s only
possible thanks to the support of our readers. If you enjoy the content, please support
us on Patreon or share this page on social media or your blog. Every bit helps.
Join Patreon ➔
1. rmdir command – Deletes the specified empty directories and folders in Linux.
2. rm command – Remove the file including sub-directories. You can delete non-empty
directories with rm command in Linux.
Let us see some examples and usage in details to delete the directories or folders under Linux
operating system using the CLI.
$ rmdir directory-name
$ rmdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete
a folder named dir1:
$ rmdir dir1
$ rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows:
$ rmdir letters
Sample outputs:
You can cd to the directory to find out and list all files:
$ cd letters
$ ls
Delete those files or directories. In this next example, remove data, foo and bar if bar were
empty, foo only contained bar and data only contained foo directories:
$ cd /home/nixcraft
$ rmdir -p data/foo/bar
Where,
$ rmdir -v dir1
Sample outputs:
We can use wildcards such as ‘*’ and ‘?’ to match and delete multiple directories. For example:
$ ls -l dir*
We have three dirs named dir1, dir2, and dir3. To delete all directories starting with ‘dir’ in the
current, you would use the following command:
$ rmdir -v dir*
Linux remove entire directory including all files and sub-
directories command
To remove all directories and subdirectories use the rm command. For example, remove
*.doc files and all sub-directories and files inside letters directory, type the following
command:
Warning: All files including subdirectories will be deleted permanently when executed the
following commands.
$ rm -rf letters/
Sample session:
Where,
-r : Attempt to remove the file hierarchy rooted in each file argument i.e. recursively
remove subdirectories and files from the specified directory.
-f : Attempt to remove the files without prompting for confirmation, regardless of the file’s
permissions
When prompted, you need to provide root user or sudo user password.
Where,
-type d : Only search for directories and ignore all other files.
-iname 'session' : Search directory named ‘session’. You can use wildcards here too.
For example, -iname 'dir*' .
-empty : Only match empty directories
OR
The -exec option to the find command run an external command named rm to delete all files.
The “ rm {} +/ ” is a better option as it uses one rm command to delete all .DS_Store
directories.
Conclusion
This page showed how to delete a directory when it is empty. Further, it showed, how to
remove folders using the rm and rmdir commands. See rm help page page for more info using
the man command or rm command:
$ man rmdir
$ man rm
$ rm --help
$ rmdir --help
🥺 Was this helpful? Please add a comment to show your appreciation or feedback.
🔎 To search, type & hit enter...
Related Tutorials
UNIX Delete / Remove Directory Command