A folder is nothing but a group of files. The folder is also known as a directory in Linux. Everything is a file in Linux. You can delete the folder or directory in Linux, if they are empty using the following rmdir command. Otherwise you need to use the rm command.
Linux delete folder using rmdir
The syntax is
rmdir foldername rmdir dirname rmdir /path/to/folder/ rmdir /path/to/directory/
Let us see some examples that show how to remove a folder in Linux.
Linux remove folder
In this example, delete the directory called /tmp/letters/
For example, the following command would remove two empty folders named alpha and delta in the current directory:
rmdir alpha delta
How to get additional information about what is happening when running rmdir
Pass the -v (verbose) option as follows:
rmdir -v dir1
rmdir -v foo bar
Remove Folder and Its Ancestors
The -p option can delete directory and its subdirectories/sub-folders:
rmdir -p dir1/dir2/dir3
Where rmdir command command options are as follows:
- -p : Linux remove folder i.e. remove the parent folders of the specified directory
- -v : Output a diagnostic for every directory processed
- --ignore-fail-on-non-empty : Ignore each failure that is solely because a folder is non-empty.
Delete All Files and Folders Including Subdirectories
Use the following syntax:
rm -rf /path/to/dir
For example, delete /home/vivek/docs and all its subdirectories including files, enter:
rm -rf /home/vivek/docs/ rm -rfv /home/vivek/dir2/ ls -l /home/vivek/docs/
Where rm command options are as follows,
- -r : Recursively delete a directory by first deleting all of its contents
- -f : Linux delete folder forcefully
- -v : Verbose output
GUI File Manager
The Nautilus file manager (GNOME desktop) provides a simple and integrated way to manage your files and applications. Just open it from Places menu and select folder and hit delete key.
In this tutorial, you learned how to delete folders in the Linux using rmdir and rm command-line options. The rmdir command is used to remove empty folders in Linux. The rm command used to delete both files and folders even if they are not empty. See rmdir help page here for more info.