LFCS 12 ๐ง Deleting Files and Directories โ rm, rmdir
The rm command deletes files and directories. It is the most dangerous command in the shell because there is no trash bin, no undo, and no confirmation by default. The rmdir command deletes empty directories. The LFCS exam expects both commands to be automatic, and the exam tasks often require the cleanup of the files and the directories created by the earlier steps.
Key point: rm file deletes a file. rm -r dir deletes a directory and its contents recursively. rmdir dir deletes an empty directory and fails if the directory is not empty. The rm command is permanent: the file is unlinked, and the data is not recoverable with the standard tools. The -i option prompts before each deletion, the -f forces the deletion without the prompt, and the -v shows what was deleted. The alias rm='rm -i' is common on the interactive systems as a safety net.
Why the deletion commands matter
The permanence problem. The rm removes the directory entry and decrements the link count. When the link count reaches zero, the inode is freed, and the data blocks are marked as available. The data is not overwritten immediately, but the file is unreachable. The recovery requires the forensic tools and the luck, and the standard shell has no undo. The deletion is the permanent operation.
The recursive problem. The rm without the -r refuses to delete a directory. The -r makes the command descend into the directory and delete the contents. The recursive deletion is the operation that removes the entire tree, and it is the one that produces the catastrophic mistakes.
The wildcard problem. The rm *.txt deletes the matching files. The rm * deletes every file in the current directory. The rm -r * deletes every file and directory in the current directory, and the subdirectories with their contents. A typo in the pattern, a wrong working directory, or an unintended variable expansion turns the routine cleanup into the data loss.
The safety problem. The -i prompts before each deletion. The -I (the capital I) prompts once before deleting more than three files or recursively deleting a directory. The -I is the balance between the safety and the convenience. The alias rm='rm -i' is the common default, and the rm -f overrides it.
The exam problem. The LFCS tasks often require the cleanup of the temporary files and the test directories. The rm and the rmdir are the commands. The options and the safety behavior are the details that decide whether the cleanup succeeds without the unintended deletion.
a. The basic rm and the options
The rm takes one or more file arguments.
$ rm file.txt
The file.txt is deleted. There is no output, no prompt, and no confirmation. The shell returns to the prompt.
Multiple files can be deleted in one command.
$ rm file1.txt file2.txt file3.txt
The three files are deleted. The command is the same as three separate rm commands, but faster.
The -i option prompts before each deletion.
$ rm -i file.txt
rm: remove regular file 'file.txt'? y
The -f option forces the deletion without the prompt. It also suppresses the errors for the non-existent files.
$ rm -f file.txt
# no prompt, no error if the file does not exist
The -v option prints each deletion.
$ rm -v file.txt
removed 'file.txt'
The -r option deletes a directory and its contents recursively.
$ rm -r projects
The projects directory and everything inside it are deleted. The recursive deletion is the operation that removes the tree.
The -d option deletes the empty directories. It is the same as the rmdir.
$ rm -d emptydir
The -I option prompts once before deleting more than three files or recursively deleting a directory.
$ rm -I *.txt
rm: remove 5 arguments? y
The -I is the balance: the prompt for the bulk deletion, and the silence for the single file.
The --preserve-root option prevents the recursive deletion of the root directory. It is the default on the GNU coreutils.
$ rm -rf /
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe
The --no-preserve-root disables the protection. It should never be used.
The -r and the -f combined is the rm -rf. The combination is the most dangerous command in the shell. The -r is the recursion, and the -f removes the prompts and the errors. The rm -rf / is the catastrophic command, and the --preserve-root is the protection that the modern systems have by default.
b. The rmdir and the empty directory
The rmdir deletes an empty directory.
$ rmdir emptydir
The emptydir is deleted. The command fails if the directory is not empty.
$ rmdir projects
rmdir: failed to remove 'projects': Directory not empty
The error is the signal that the directory has the contents. The rm -r is the command for the non-empty directory.
The rmdir deletes the multiple directories in one command.
$ rmdir dir1 dir2 dir3
The three directories are deleted. Each must be empty.
The -p option deletes the directory and its empty parents.
$ rmdir -p a/b/c
The c is deleted, then the b, then the a, if each becomes empty. The -p is the cleanup for the nested empty directories.
The --ignore-fail-on-non-empty option suppresses the error for the non-empty directories.
$ rmdir --ignore-fail-on-non-empty dir1 dir2
The dir1 is deleted if it is empty, and the dir2 is skipped if it is not. The command does not fail. The option is for the scripts that delete the directories that may or may not be empty.
The rmdir and the rm -d are equivalent for the empty directories.
$ rmdir emptydir
$ rm -d emptydir
# same result
The rm -d is the option of the rm, and the rmdir is the separate command. The rmdir is the dedicated tool for the empty directory, and the rm -d is the convenience within the rm.
c. The safety and the wildcards
The rm is the dangerous command, and the safety is the discipline and the options.
The -i option. The -i prompts before each deletion. The alias rm='rm -i' is the common default on the interactive systems. The alias makes the rm prompt, and the rm -f overrides it.
$ alias rm
alias rm='rm -i'
$ rm file.txt
rm: remove regular file 'file.txt'?
The -I option. The -I prompts once before the bulk deletion. The -I is the balance: the prompt for the many files, and the silence for the few.
$ rm -I file.txt
# no prompt (one file)
$ rm -I *.txt
rm: remove 5 arguments? y
The wildcard danger. The rm *.txt deletes the matching files. The rm * deletes the visible files. The rm .* deletes the hidden files, and it includes the . and the .., which the rm refuses with the error. The rm -r .* is the catastrophic command that attempts to delete the current and the parent directories.
$ rm -r .*
rm: refusing to remove '.' or '..' directory: skipping '.'
rm: refusing to remove '.' or '..' directory: skipping '..'
The modern rm refuses the . and the .., but the other hidden files are deleted. The .* is the pattern to use with the caution.
The variable danger. The rm -rf $DIR/* is the command that deletes the contents of the $DIR. If the $DIR is empty, the command becomes rm -rf /*, which is the catastrophic command. The fix is the quotes and the checks.
$ rm -rf "$DIR"/*
The quotes protect the spaces, and the set -u makes the unset variable an error. The [ -n "$DIR" ] check is the guard.
[ -n "$DIR" ] && rm -rf "$DIR"/*
The find alternative. The find command is the safer tool for the selective deletion.
$ find /path -name "*.tmp" -delete
$ find /path -type f -mtime +30 -delete
The find with the -delete deletes the matching files. The predicate is the filter, and the deletion is the explicit action. The find is the preferred tool for the scripted cleanup.
The -r with the .. The rm -r . is the command that deletes the current directory and its contents. The rm refuses the . in the modern systems, but the older systems may delete the contents. The rm -r . should never be used.
$ rm -r .
rm: refusing to remove '.' or '..' directory: skipping '.'
The rm -rf /. The catastrophic command. The modern systems have the --preserve-root by default, and the command fails with the error. The --no-preserve-root disables the protection, and the command deletes the system.
$ rm -rf /
rm: it is dangerous to operate recursively on '/'
The rm -rf ~. The command that deletes the home directory. The ~ is expanded to the home path, and the -rf deletes the contents. The home directory is gone, and the user’s files with it.
$ rm -rf ~
# deletes the home directory contents
The rm -rf ~/ is the same. The rm -rf ~/* is the same for the visible files.
Complete Example Session
# ============================================
# PART 1: BASIC RM
# ============================================
rm file.txt
# ============================================
# PART 2: MULTIPLE FILES
# ============================================
rm file1.txt file2.txt file3.txt
# ============================================
# PART 3: PROMPT
# ============================================
rm -i file.txt
# rm: remove regular file 'file.txt'?
# ============================================
# PART 4: FORCE
# ============================================
rm -f file.txt
# ============================================
# PART 5: VERBOSE
# ============================================
rm -v file.txt
# removed 'file.txt'
# ============================================
# PART 6: RECURSIVE
# ============================================
rm -r projects
# ============================================
# PART 7: RMDIR
# ============================================
rmdir emptydir
rmdir dir1 dir2 dir3
# ============================================
# PART 8: RMDIR -P
# ============================================
rmdir -p a/b/c
# ============================================
# PART 9: RM -I
# ============================================
rm -I *.txt
# rm: remove 5 arguments?
# ============================================
# PART 10: FIND DELETE
# ============================================
find /tmp -name "*.tmp" -delete
find /var/log -type f -mtime +30 -delete
The ten parts covered the basic rm, the multiple files, the prompt, the force, the verbose, the recursive, the rmdir, the rmdir -p, the rm -I, and the find -delete.
Quick Reference
rm Options
| Option | Effect |
|---|---|
rm file | Delete a file |
rm -i | Prompt before each |
rm -f | Force, no prompt |
rm -v | Verbose |
rm -r | Recursive |
rm -d | Delete empty directory |
rm -I | Prompt once for the bulk |
rm --preserve-root | Protect the root (default) |
rmdir Options
| Option | Effect |
|---|---|
rmdir dir | Delete empty directory |
rmdir -p a/b/c | Delete the parents |
rmdir --ignore-fail-on-non-empty | Skip the non-empty |
Safety Options
| Option | Protection |
|---|---|
-i | Prompt each |
-I | Prompt for the bulk |
--preserve-root | Protect / |
alias rm='rm -i' | Interactive default |
Dangerous Commands
| Command | Effect |
|---|---|
rm -rf / | Delete the system (protected) |
rm -rf ~ | Delete the home |
rm -rf . | Delete the current (refused) |
rm -rf $DIR/* | Delete the contents (if $DIR empty) |
rm -r .* | Delete the hidden files |
Alternatives
| Tool | Use |
|---|---|
find -delete | Selective deletion |
trash-cli | Trash bin |
rm -I | Bulk prompt |
Best Practices
โ Do This:
# Use -i in the interactive shells
alias rm='rm -i' # โ
# Use -I for the bulk deletion
rm -I *.txt # โ
# Use the quotes for the variables
rm -rf "$DIR"/* # โ
# Check the variable before the deletion
[ -n "$DIR" ] && rm -rf "$DIR"/* # โ
# Use the find for the selective deletion
find /tmp -name "*.tmp" -delete # โ
# Use the verbose for the confirmation
rm -v file.txt # โ
# Use the -d for the empty directory
rm -d emptydir # โ
โ Don’t Do This:
# Don't use the rm -rf without the thought
rm -rf * # โ
# Don't use the unquoted variables
rm -rf $DIR/* # โ
# Don't use the .* pattern carelessly
rm -r .* # โ
# Don't use the --no-preserve-root
rm -rf --no-preserve-root / # โ
# Don't delete the home
rm -rf ~ # โ
# Don't use the rm -r .
rm -r . # โ
# Don't forget the -f to override the alias in the script
rm file.txt # the alias may prompt # โ ๏ธ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Accidental deletion | No prompt | Use -i or -I |
| Variable expansion | $DIR is empty | Quote and check |
| Hidden files deleted | .* pattern | Check the pattern |
| Directory not deleted | Not empty | Use rm -r |
| Root protection | --preserve-root | Expected |
| Alias prompts in script | The alias active | Use \rm or -f |
| No recovery | No trash | Use trash-cli |
Real-World Examples
1. Delete a File
rm file.txt
2. Delete Multiple
rm file1.txt file2.txt
3. Delete with Prompt
rm -i file.txt
4. Delete Directory
rm -r projects
5. Delete Empty Directory
rmdir emptydir
6. Delete Nested Empty
rmdir -p a/b/c
7. Bulk Delete with Prompt
rm -I *.log
8. Find Delete
find /tmp -name "*.tmp" -delete
9. Safe Variable Delete
[ -n "$DIR" ] && rm -rf "$DIR"/*
10. Verbose Delete
rm -rv projects
Visual
rm vs rmdir
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ rm file.txt โ
โ โโโโโโโโโโโ โ
โ โfile.txt โ โโโถ (deleted) โ
โ โโโโโโโโโโโ โ
โ โ
โ rm -r projects โ
โ โโโโโโโโโโโโโโโ โ
โ โprojects/ โ โ
โ โโโโ a โ โโโถ (deleted) โ
โ โโโโ b โ โ
โ โโโโโโโโโโโโโโโ โ
โ โ
โ rmdir emptydir โ
โ โโโโโโโโโโโโโโโ โ
โ โemptydir/ โ โโโถ (deleted) โ
โ โ(empty) โ โ
โ โโโโโโโโโโโโโโโ โ
โ โ
โ rmdir nonempty โ error โ
โ rm -r nonempty โ deleted โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Wildcard Danger
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ rm *.txt โ
โ Deletes: file1.txt, file2.txt, file3.txt โ
โ Keeps: notes.md, script.sh, .hidden โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ rm * โ
โ Deletes: file1.txt, notes.md, script.sh โ
โ Keeps: .hidden, .config โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ rm .* โ
โ Deletes: .hidden, .config โ
โ Refuses: ., .. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ rm -rf $DIR/* โ
โ If $DIR is empty โ rm -rf /* โ catastrophic โ
โ Fix: rm -rf "$DIR"/* or [ -n "$DIR" ] && rm -rf "$DIR"/* โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The Recovery
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ rm file.txt โ
โ โ โ
โ โผ โ
โ The directory entry is removed. โ
โ The inode link count is decremented. โ
โ โ โ
โ โผ โ
โ The link count reaches zero. โ
โ โ โ
โ โผ โ
โ The inode is freed, the data blocks are marked available. โ
โ โ โ
โ โผ โ
โ The data is not overwritten immediately. โ
โ The recovery is possible with the forensic tools until the โ
โ blocks are reused. โ
โ โ โ
โ โผ โ
โ The standard shell has no undo. โ
โ The trash-cli is the alternative. โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Safety Options
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ rm -i file.txt โ
โ โ Prompt for each file โ
โ โ
โ rm -I *.txt โ
โ โ Prompt once for the bulk โ
โ โ
โ rm --preserve-root -rf / โ
โ โ Refuses (the default) โ
โ โ
โ alias rm='rm -i' โ
โ โ Interactive default โ
โ โ
โ find /tmp -name "*.tmp" -delete โ
โ โ Selective, explicit โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Basic | rm file |
| Multiple | rm file1 file2 |
| Prompt | -i |
| Bulk prompt | -I |
| Force | -f |
| Verbose | -v |
| Recursive | -r |
| Empty directory | rm -d or rmdir |
| Parent cleanup | rmdir -p |
| Root protection | --preserve-root |
| Permanent | Yes, no undo |
| Alternative | find -delete, trash-cli |
Key takeaways:
- The
rmis the permanent deletion. The file is unlinked, and the data is not recoverable with the standard tools. Thetrash-cliis the alternative for the recoverable deletion. - The
rm -rdeletes the directory and its contents. The recursive flag is required for the directory. Thermwithout the-rrefuses the directory with the “is a directory” error. - The
rmdirdeletes the empty directory. The command fails if the directory has the contents. Therm -ris the command for the non-empty directory. - The
-iprompts before each deletion, and the-Iprompts once for the bulk. Thealias rm='rm -i'is the common interactive default. The-Iis the balance between the safety and the convenience. - The
--preserve-rootprotects the root directory. The option is the default on the modern systems. The--no-preserve-rootdisables the protection and should never be used. - The wildcards are dangerous. The
rm *deletes the visible files. Therm .*deletes the hidden files. Therm -rf $DIR/*with the empty$DIRbecomes the catastrophic command. Quote the variables, check the values, and use thefindfor the selective deletion. - The
find -deleteis the safer tool for the scripted cleanup. The predicate is the filter, and the deletion is the explicit action. Thefindis the preferred tool for the selective deletion of the files matching the pattern.
Remember: The rm is the most dangerous command in the shell because there is no undo. The deletion is permanent, the wildcards are powerful, and the variables are unpredictable. Use the -i or the -I on the interactive systems. Quote the variables. Check the values. Use the find for the selective deletion. And remember the --preserve-root is the last line of the defense. The rm is the command the LFCS tasks require, and the safety is the discipline that the tasks assume.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!