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 delete the directories.
rmdir command syntax to delete directory in Linux
The rmdir command remove the DIRECTORY(ies), if they are empty. The syntax is:
rmdir directory-namermdir [option] directory-name
Open the terminal application and run command to delete given directory. For example, delete a folder named 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:
If a directory is not empty you will get an error message that read as follows:
rmdir: letters: Directory not empty
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/nixcraftrmdir -p data/foo/bar
- -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
rmdir: removing directory, ‘dir1’
REMOVING DIRECTORIES WITH RMDIR AND WILDCARDS
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*
Curated by: https://www.ParagonHost.net
Est. 1999 | Paragon Host Internet Group | Web, Email, Workspace | Global Hosting Services