In my spare time, I facilitate online sessions for small businesses that are using unmanaged Linux systems. The question I resolved this week was how to delete files and directories from the Linux command line. The standard tool in question to removing directories is rmdir, but when attempting to remove a directory with files, you will get a prompt similar to “rmdir: ‘dir’: Directory not empty” and unable to delete the directory.
As the name suggests, the rmdir command is focused at removing directories, although empty-ones only.
Q1. How rmdir works?
That’s pretty straight forward – just pass the directory name as input to the command. For example:
[root@server ~]# rmdir directoryname
The system then deletes the directory, if empty.
Q2. How to make rmdir ignore non-empty directories.
To remove a directory that contains other files or directories, use the following command.
[root@server ~]# rm -rf directoryname
In the example above, you would replace “directory name” with the name of the directory you want to delete. Executing the command would recursively delete all files and subdirectories in that directory.
Caution: Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r or rm-rf command.
Patch Job Sample – Removing full backup directories
[root@server ~]# cd /backup [root@server backup]# du -h --max-depth=1 12M ./.meta 34G ./weekly 34G . [root@server backup]# cd ./weekly [root@server weekly]# du -h --max-depth=1 34G ./2020-06-12 76K ./2020-06-05 34G . [root@server weekly]# sudo rm -rf ./2020-06-12 [root@server weekly]# rm -rf ./2020-06-12 [root@server weekly]# du -h --max-depth=1 76K ./2020-06-05 80K . [root@server weekly]#