Intro To 'rmdir' Command In Linux
2023-06-04 - By Robert Elder
I use the 'rmdir' command to delete empty directories:
rmdir empty-folder
The 'rmdir' Command Only Deletes Empty Directories
Here, I have a directory that contains only one file:
ls -l stuff
-rw-rw-r-- 1 robert robert 0 Jun 1 11:31 cat.jpg
If I try to delete this directory with the 'rmdir' command, it will produce an error message and refuse to delete the directory:
rmdir stuff
rmdir: failed to remove 'stuff': Directory not empty
If I delete the file first so that the directory is empty:
rm stuff/cat.jpg
the 'rmdir' command will delete the folder for me:
rmdir stuff
ls -l
total 0
The 'rmdir' Command Versus 'rm' Command
Deleting a directory could also be done with the 'rm' command using the '-r' flag:
rm -r stuff
However, the 'rmdir' command could be considered safer since it forces you to delete the contents of a folder before deleting the folder itself.
A Realistic Use Case Of The 'rmdir' Command
Let's try to set up a realistic use case where the 'rmdir' command could actually be useful. Let's assume that I start with the following set of files on a production server:
ls -l
drwxrwxr-x 2 robert robert 4096 Jun 1 11:56 production-data
drwxrwxr-x 2 robert robert 4096 Jun 1 11:56 production-data-test
find .
./production-data-test
./production-data
./production-data/all-customer-data.sql
In this scenario, my goal is to delete the unused test folder 'production-data-test' that was created for testing on a copy of the production database. In a hurry, I type 'rm -r prod' and then use tab to auto-complete the folder name, expecting it to expand to the full 'production-data-test', but instead it only expands to 'production-data' because of the folder name ambiguity:
rm -r production-data
Without double checking, I run the above command and now the production database is gone forever.
If I had instead used the 'rmdir' command I would have gotten an error message instead:
rmdir production-data
rmdir: failed to remove 'production-data': Directory not empty
and then I would have known to run this instead:
rmdir production-data-test
And that's why the 'rmdir' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|