rm – Used to Remove Given Files and Directories

The rm command is a Linux utility used to remove files and directories from the file system. It is a powerful command that can permanently delete files and directories, so it should be used with caution.

Overview

The basic syntax for the rm command is as follows:

rm [OPTIONS] FILE [FILE...]

where OPTIONS are any command-line options and FILE is the name of the file or directory to be removed.

Removing a File

To remove a file, simply specify the name of the file as an argument to the rm command. For example, to remove a file named example.txt, run the following command:

rm example.txt

Removing Multiple Files

To remove multiple files at once, specify each file name as a separate argument to the rm command. For example, to remove three files named file1.txt, file2.txt, and file3.txt, run the following command:

rm file1.txt file2.txt file3.txt

Removing a Directory

To remove a directory and all of its contents, including subdirectories and files, use the -r option. For example, to remove a directory named mydir, run the following command:

rm -r mydir

Prompting for Confirmation

By default, the rm command does not prompt for confirmation before deleting files or directories. To enable confirmation, use the -i option. For example, to prompt for confirmation before deleting a file named example.txt, run the following command:

rm -i example.txt

Verbose Output

To display a message for each file or directory that is deleted, use the -v option. For example, to display a message for each file that is deleted when removing a directory named mydir, run the following command:

rm -rv mydir

Options

The following table lists the available options for the rm command:

Option Description
-f Force removal of files without prompting for confirmation.
-i Prompt for confirmation before deleting each file.
-r Recursively remove directories and their contents.
-v Display a message for each file or directory that is deleted.
-- Indicates the end of command options.

Troubleshooting Tips

  • Be careful when using the rm command, as it can permanently delete files and directories.
  • Always double-check the file or directory name before running the rm command.
  • Use the -i option to prompt for confirmation before deleting files or directories.
  • Use the -v option to display a message for each file or directory that is deleted.

Notes

  • The rm command cannot be used to recover deleted files or directories.
  • To recover deleted files or directories, use a file recovery tool such as testdisk or photorec.