This article will outline how to delete files and directories in Linux with the rm command and give example usage.
The rm Command in Linux
Files and directories can be deleted from the shell/command line in Linux using the rm command.
rm Command Syntax
rm OPTIONS FILES
- OPTIONS is a list of options from the below table
- FILES is a list of files or directories (if the -r option is specified) to be removed
- Multiple files or directories can be specified, separated by spaces
Here are the most commonly used options for the rm command:
|-f||Ignore nonexistent files, never prompt|
|-i||Prompt before every removal|
|-I||Prompt once before removing more than three files or when removing recursively. Less intrusive than -i, while still giving protection against most mistakes|
|–one-file-system||When removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command-line argument.|
|-r, -R, –recursive||Remove directories and their contents recursively.|
|-v, –verbose||Explain what is being done|
For a full list of options, you can view the full rm command user manual by running:
Deleting/Removing a Single File
The default behavior of the rm command is to delete single files or a list of single files not contained in a directory.
The full path to the file can also be specified:
Deleting/Removing Multiple Files
rm file1 file2 file3 /path/to/file4
the -r (recursive) option will allow the rm command to delete a directory, as well as its contents.
rm -r directory1
The full path to the directory can also be specified:
rm -r /path/to/directory1
Prompting Before Deletion
If you want to confirm the deletion of tiles before them being removed, pass the -i (interactive) option:
rm -i file1 file2 file3
This can also be used when removing directories:
rm -i -r directory1
Unlike many desktop environments (or if you’re coming from Windows or macOS), there’s no Recycle Bin or Trash Bin equivalent when you’re working in the Linux shell.
When a file is deleted, it’s deleted. You aren’t getting it back – so be careful!
If you aren’t sure whether you might need a file later, you can always designate a folder as your own trash bin and move files there until you’re sure they’re no longer required.
Keeping your files backed up is another (the best) method to guard against accidental deletion, system failure, theft, or any other disaster.