Linux rm command


The rm command in Ubuntu is used to delete files and directories. Unlike rmdir, which only removes empty directories, rm can delete both files and directories, including those that contain other files or subdirectories, by using certain options.

Syntax

rm [options] file_or_directory
  • [options]: Flags to modify the command’s behavior.
  • file_or_directory: The name(s) of the file(s) or directory(ies) you want to delete. You can specify multiple files or directories separated by spaces.

Common Options

OptionDescription
-rRecursive; deletes directories and their contents
-fForce; ignores non-existent files and prompts
-iInteractive; prompts for confirmation before each delete
-vVerbose; shows each file as it is deleted

Examples with Output

1. Removing a Single File

To delete a single file, use:

rm filename.txt

Example Output: If the file is successfully deleted, there will be no output.

$ touch file1.txt $ rm file1.txt $ ls

If file1.txt was deleted, ls will not list it anymore.

2. Removing Multiple Files

You can delete multiple files in one command by specifying their names:

rm file1.txt file2.txt

Example Output: There is no output if the files are deleted successfully.

$ touch file1.txt file2.txt $ rm file1.txt file2.txt $ ls

Files file1.txt and file2.txt are no longer present.

3. Removing a Directory with -r (Recursive)

To delete a directory and all of its contents, use the -r (recursive) option:

rm -r my_directory

Example Output: If the directory and its contents are deleted, there will be no output.

$ mkdir -p my_directory/subdir $ touch my_directory/file.txt $ rm -r my_directory $ ls

If my_directory was deleted, ls will not show it anymore.

4. Removing Without Prompting with -f (Force)

Using -f will forcefully delete files without any confirmation, even if you don’t have write permissions on them.

rm -f filename.txt

Example Output: No output if the file is deleted.

$ touch file.txt $ chmod 444 file.txt # Make the file read-only $ rm -f file.txt $ ls

Using -f deletes the file even if it’s read-only.

5. Interactive Mode with -i

Using -i will prompt you for confirmation before deleting each file. This is useful to avoid accidental deletions.

rm -i file1.txt file2.txt

Example Output:

$ touch file1.txt file2.txt $ rm -i file1.txt file2.txt rm: remove regular empty file 'file1.txt'? y rm: remove regular empty file 'file2.txt'? y $ ls

If you press y for each prompt, the files are deleted. Pressing n skips deletion.

6. Verbose Mode with -v

The -v option shows a message for each file or directory deleted.

rm -v file1.txt

Example Output:

$ touch file1.txt $ rm -v file1.txt removed 'file1.txt'

7. Combining Options: Forcefully Removing a Directory with -rf

To forcefully delete a directory and all of its contents without confirmation, use -rf.

rm -rf my_directory

Example Output: If successful, there is no output.

$ mkdir -p my_directory/subdir $ touch my_directory/file.txt $ rm -rf my_directory $ ls

Summary Table of Commands

CommandDescriptionExample Output
rm file.txtDeletes a single file(No output if successful)
rm file1.txt file2.txtDeletes multiple files(No output if successful)
rm -r directoryDeletes a directory and all its contents(No output if successful)
rm -f file.txtForcefully deletes a file without confirmation(No output if successful)
rm -i file.txtPrompts before deletingrm: remove regular file 'file.txt'? y
rm -v file.txtShows each file being deletedremoved 'file.txt'
rm -rf directoryForcefully deletes a directory and contents(No output if successful)

Summary

The rm command in Ubuntu is a powerful tool for deleting files and directories, with options for safe deletion (-i), forced deletion (-f), and recursive deletion of directories (-r). Always use rm with caution, especially with -rf, as it can permanently delete data without prompting.