Skip to content

Instantly share code, notes, and snippets.

@devmnj
Created September 14, 2019 02:57
Show Gist options
  • Save devmnj/557ad0e605480da632278a104f90fcbc to your computer and use it in GitHub Desktop.
Save devmnj/557ad0e605480da632278a104f90fcbc to your computer and use it in GitHub Desktop.
Commands to remove a directory in Linux
There are two command to delete a folder in Linux:
rmdir command – Deletes the specified empty directories and folders in Linux.
rm command – Delete the file including sub-directories. You can delete non-empty directories with rmdir command in Linux.
Let us see some examples and usage in details.
rmdir command syntax to delete directory in Linux
The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
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
Delete directory Linux Command
Open a command line terminal (select Applications > Accessories > Terminal), and then type the following command to remove a directory called /tmp/docs:
rmdir /tmp/docs
If a directory is not empty you will get an error message that read as follows::
rmdir letters
Sample outputs:
rmdir: letters: Directory not empty
You can cd to the directory to find out files:
$ cd letters
$ ls
In this 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,
-p – Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.
How to see a diagnostic message for every directory processed
Pass the -v option to the rmdir command:
$ rmdir -v dir1
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):
$ rm -rf letters/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment