The rm (remove) command uses a list of arguments that specifies either the absolute or relative path of the files to remove. Use this command with care as you cannot recover a file once it has been removed. Because of this, the base rm command will prompt you before deleting to give you a chance to avoid any mistakes.
Command: rm
Synopsis: rm [OPTION]… FILE…
Option | Long option name | Description |
---|---|---|
-f | –force | Ignores nonexistent files. Also does not prompt the user before deleting. This option can be dangerous if misused. |
-i | –interactive | Prompts the user before any removal> This is default behavior even if the option is not used. |
-r -R | –recursive | Removes all directories and their contents recursively. |
-v | –verbose | Explains what is being done by displaying it on the terminal screen. |
Examples
Base rm command
This example shows the base command as we use it to remove a file named test.txt.
# rm est.txt rm: remove regular empty file `test.txt'? y
Forcing the rm command
This command is like the base command, simply removing a file. Note that adding the force option causes the computer not to prompt the user, so the removal was automatic.
# rm -f test.txt
Removing a directory
The format for removing a directory is similar to removing a file. You do need use the recursive option in order to make it remove the directory and all the files and other folders within it. It will, however, prompt you for each individual file and folder inside it. The example below demonstrates this process.
# rm -r test rm: descend into directory `test'? y rm: remove regular empty file `test/index.php'? y rm: descend into directory `test/images'? y rm: remove regular empty file `test/images/file3.png'? y rm: remove regular empty file `test/images/file1.png'? y rm: remove regular empty file `test/images/file2.png'? y rm: remove directory `test/images'? y rm: remove directory `test'? y
Removing a directory without prompting
Removing the files and folders without prompting is much faster. Be sure you know what you are deleting when using this option combination. Below we show an example of combining the recursive and force options to removing the same test directory.No prompting will occur and the items (all subfolders and files) including the directory itself will be deleted.
# rm -rf test