I am a new Linux user. How do I remove directory in Linux using the command line or terminal applications?
Introduction: Everything is a file in Linux including the directory. A directory is nothing but a group of files (or data). One can group files as per needs. For example, store documents in ~/Documents/ directory. You can use any one of the following commands to remove the directory in Linux (also known as a folder in the Macintosh OS X/macOS and Microsoft Windows operating system):
- rmdir command – Remove the specified empty directories
- rm command – Remove directories even if it is not empty
Let us see how to remove directories in Linux using the command line.
How to remove directory in Linux
Say you want to delete a directory named /tmp/foo, run:
rmdir -v /tmp/foo/
Verify directory deleted from the system with help of ls command:
Please note that when attempting to remove a directory using the rmdir command, the directory must be empty. Otherwise, you might see an error message that read as follows on screen when execute rmdir -v /tmp/delta/:
rmdir: failed to remove '/tmp/delta/': Directory not empty
How do I remove a full directory in Linux?
As I said earlier rmdir command remove the DIRECTORY(ies) if they are empty. But, how do you delete a full directory that has many files and sub-directories? The solutions is to pass the -rf option to the rm command. The syntax is:
rm -r /path/to/dir/
rm -rf dir1
rm -rfv /path/to/dir
rm -rfv /tmp/delta/
- -r – Remove directories and their contents recursively
- -f – Forceful option i.e. ignore nonexistent files and arguments, never prompt for anything
- -v – Be verbose. Show what rmdir or rm command doing with given directory
- -i – Prompt before every removal of file/dir
- -I – Prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes. Useful when working on a large number of files on Linux
This page demonstrated how to delete both empty and non-empty directories along with all files/sub-directories using rm and rmdir command.