The rm command in Linux: delete files and directories

Introduction to the rm command

The rm (remove) command is one of the most basic and powerful tools in the Linux command line. Its main function is to delete files and directories from the filesystem. Despite its simplicity, rm can cause irreversible damage if used without caution, because by default it does not send items to a recycle bin; it deletes them immediately and permanently.

Basic syntax

The simplest way to use rm is:

rm filename

This command deletes the specified file if the user has the necessary permissions. If the file does not exist, rm will show an error message and continue with the following arguments.

Most used options

  • -f (force): forces deletion without prompting, even if the file is write-protected.
  • -i (interactive): prompts for confirmation before deleting each file, useful to avoid accidental deletions.
  • -r or -R (recursive): allows deleting directories and all their contents recursively.
  • -v (verbose): shows on screen each file being deleted, providing visual feedback.

Common combinations

Options can be combined as needed. For example, rm -rf /tmp/old file will forcibly and recursively delete the directory /tmp/old file without prompting. Another frequent combination is rm -ri directory, which asks for confirmation for each item inside the directory before deleting it.

Precautions and best practices

Because rm acts immediately, it is advisable to follow some safety practices:

  • Double-check the path and wildcards before running the command, especially when using * or ?.
  • Prefer the -i option in production environments or when working with critical data.
  • Use ls first to list what will be deleted: ls -la path/*.
  • Consider moving files to a temporary folder or to your desktop’s trash before deleting them permanently.
  • In scripts, test with echo or with the -v option first to see what would be affected.

Practical examples

  • Delete a single file: rm notes.txt
  • Delete several files with a wildcard: rm *.log
  • Delete an empty directory: rmdir empty_folder (safe alternative) or rm -r empty_folder
  • Delete a directory and all its contents without prompting: rm -rf /home/user/temp
  • Delete with confirmation: rm -i file1 file2

Safer alternatives

If you fear the risks of rm, there are alternatives that provide a layer of protection:

  • trash-cli: a package that implements a recycle bin on the command line. Instead of deleting, it sends files to ~/.local/share/Trash and allows restoring them.
  • gvfs-trash (part of GNOME): works similarly and integrates with the desktop.
  • Protective aliases: many users add alias rm='rm -i' to their ~/.bashrc so that each deletion prompts for confirmation by default.

Conclusion

The rm command is indispensable for Linux system administration, but its power comes with responsibility. Knowing its options, using wildcards with caution, and adopting verification habits can prevent painful data loss. Always remember that in Linux there is no recycle bin by default; therefore, prevention and verification are your best allies when working with rm

This post is also available in ESPAÑOL.

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .