rm


rm (remove files or directories)

The rm command is the primary utility for deleting files in RHEL. It is a powerful command, and since deleted files are generally not recoverable, it should be used with caution.


Example 1: Remove a single file

# Remove the file named 'stale_data.txt' from the current directory
rm stale_data.txt

Explanation: This is the most basic usage of rm. It permanently deletes the specified file. The system does not move it to a "Trash" or "Recycle Bin".


Example 2: Remove multiple files

# Remove three specified log files
rm access.log error.log combined.log

Explanation: You can provide a space-separated list of filenames to remove multiple files with a single rm command.


Example 3: Interactive removal for confirmation

# Prompt for confirmation before removing each file
rm -i *.tmp

Explanation: The -i (interactive) option prompts you to confirm the deletion of each file individually. This is a crucial safety measure, especially when using wildcards (*), to prevent accidental deletion of important files.


Example 4: Verbose output showing what is being removed

# Remove 'file1' and 'file2' and print a confirmation message for each
rm -v file1 file2

Explanation: The -v (verbose) flag instructs rm to print the name of each file as it is being removed. This can be useful for verification in scripts or when deleting many files.


Example 5: Removing a file with a hyphen in its name

# Remove a file named '-old_file.txt'
rm -- -old_file.txt

Explanation: Filenames that start with a hyphen (-) can be misinterpreted as command options. The double-hyphen (--) tells rm to stop processing options, ensuring that -old_file.txt is treated as a filename.


Example 6: Remove a file using its absolute path

# Remove the 'temp_archive.zip' file located in the '/tmp' directory
rm /tmp/temp_archive.zip

Explanation: You can remove a file from any location in the filesystem by providing its absolute path, provided you have the necessary write permissions on the containing directory.


Example 7: Attempting to remove a directory without -r

# This command will fail because 'my_directory' is a directory
rm my_directory

Explanation: By default, rm is designed to remove files only. Attempting to use it on a directory will result in an error message like rm: cannot remove 'my_directory': Is a directory. To remove directories, you must use the recursive option.