rm ‐ #files - five4nets/Linux-Knowledgebase GitHub Wiki

Tutorial: Using the Linux rm Command

The rm (remove) command in Linux is used to delete files and directories from the file system. It is a powerful tool that requires caution, as deleted files are typically unrecoverable without specialized tools. This tutorial explains the rm command, its options, and provides practical examples to demonstrate its usage.

Table of Contents

Overview

The rm command deletes files and directories specified by the user. It is part of the GNU core utilities and is available on most Linux distributions. Unlike graphical file managers, rm does not move files to a trash or recycle bin; it permanently deletes them from the file system.

Syntax

rm [options] file1 [file2 ...]
rm [options] directory
  • [options]: Flags that modify the behavior of rm.
  • file1 [file2 ...]: One or more files or directories to delete.

Common Options

Option Description
-f, --force Ignores nonexistent files and suppresses confirmation prompts.
-i, --interactive Prompts for confirmation before deleting each file.
-r, -R, --recursive Deletes directories and their contents recursively.
-v, --verbose Displays a message for each deleted file or directory.
-d, --dir Removes empty directories.
--no-preserve-root Allows deletion of the root directory (/) (use with extreme caution).

Examples

1. Deleting a Single File

To delete a file named example.txt:

rm example.txt

This removes example.txt from the current directory without prompting.

2. Deleting Multiple Files

To delete multiple files, list them separated by spaces:

rm file1.txt file2.txt file3.txt

This deletes file1.txt,司大

, file2.txt, and file3.txt in one command.

3. Deleting a Directory and Its Contents

To delete a directory named backup and all its contents:

rm -r backup

The -r flag ensures the directory and its contents are deleted recursively.

4. Forcing Deletion Without Confirmation

To delete a file without prompting for confirmation:

rm -f old_log.txt

The -f flag skips any prompts, useful for scripts or when you're certain about deletion.

5. Interactive Deletion

To be prompted before deleting each file:

rm -i temp*.txt

This prompts for confirmation before deleting each file matching the pattern temp*.txt.

6. Verbose Deletion

To see a list of deleted files:

rm -v obsolete_files/*.bak

The -v flag prints the name of each file as it is deleted.

7. Deleting an Empty Directory

To delete an empty directory named temp:

rm -d temp

The -d flag removes the directory if it is empty.

8. Combining Options

To delete a directory old_project and its contents forcefully, with verbose output:

rm -rfv old_project

This recursively (-r), forcefully (-f), and verbosely (-v) deletes old_project and its contents.

Safety Tips

  • Double-check file paths: Ensure you're targeting the correct files, as rm deletions are permanent.
  • Use -i for caution: The interactive mode helps prevent accidental deletions.
  • Avoid rm -rf /: This can destroy your system. Most systems prevent this unless --no-preserve-root is used.
  • Backup important files: Always maintain backups to recover from accidental deletions.
  • Test with echo: Use echo rm [options] files to preview what would be deleted.

References