There are times when you need to delete files in the Linux terminal. The rm
command lets you delete directories, files, symbolic links, and more. This in-depth guide shows you how to use the rm
command effortlessly.
Note: The rm command removes the file permanently without moving it to the Trash/Recycle Bin. Hence, ensure you fully understand the rm command, or you might end up losing your important files forever.
Also read: How to Use the lp Command in Linux to Print Files From Terminal
Knowing more about rm
To get started, you might want to use --help
to find out more about rm
and its usage:
rm --help
How to Remove a Single File
You can remove a single file using the following command:
rm <filename>
Also read: Useful Linux Commands to List Contents of a Directory
How to Remove Multiple Files
For removing multiple files, you only need to add the names of the files, separated with a space:
rm <filename1> <filename2> <filename3>.....
If you want to remove all the files with the same extension, you can use *
as the placeholder. For example, to remove all “.txt” files:
rm *.txt
Similarly, to remove all files (with extension) in a directory, you can use:
rm *.*
How to Remove a Directory
For removing an empty directory, you can use the -d
option, or rmdir
command:
rm -d <empty_directory_name>
or
rmdir <empty_directory_name>
For director with files, you have to use the -r
(recursive) option to remove a directory. This will also remove all files within the folder.
rm -r <directory_name>
Similarly, you can remove everything, including subfolders and the files within, from a directory:
rm -r *
Also read: How to Resize and Optimize Images From the Linux Terminal
Get a Prompt Before Removing a File
If you want to verify everything before removing any file, it is better to use the -i
option. This option shows a confirmation prompt before removing any file from the system.
rm -i <filename>
Once you execute the above command, the system will ask you to press Y or N to confirm your selection.
In case you are deleting more than three files with the rm command, please use the -I
option instead:
rm -I <filename1> <filename2> <filename3>.....
or
rm -I *.txt
Get a Message After Removing Files
With the -v
option, the rm
command shows what is being done by the command:
rm -v <filename>
You can use both the -v
and -i
option to remove files interactively:
rm -vi <filename>
Also read: How to Use the dd Command in Linux
Remove a File Forcefully
The -f
option overrides any minor protection of a write-protected file to remove it forcefully.
rm -f <filename>
You can combine this with the -r
option to forcefully remove a directory and its subfolders.
rm -rf <directory_name>
Note: the -rf
option coupled with sudo
can be a lethal command that can remove any/all files and folders in the system. Use it with care.
Remove a File Named with a Dash (-)
If you try to remove a file that has a dash in its name, you may get an error message.
To deal with the above error, please use a double dash (–) that works as an “end of options” instruction for a command in Linux:
rm -v -- -tech_info(sample_file).txt
Combine Rm with Xargs Command
You can combine the rm
command with the xargs command in Linux to delete many files efficiently. For instance, let’s delete the files listed in the info.txt file:
xargs rm < info.txt
Also read: How to Check Sudo History in Linux
Frequently Asked Questions
Is the rm command different from unlink?
unlink
is a system call while rm
is a shell utility that call unlink. Fundamentally, they work the same to delete files from system, but they work differently.
Is the rm command available for macOS and Windows?
The rm
command is available in macOS. For Windows, the rmdir
command is available, but not the rm
command. To delete files in Windows command prompt, one have to use the del
command.
Do I need to use "sudo" with the rm command?
If you are only deleting your own files, or you are logged in as the root user, there is no need to use “sudo”. You will have to use “sudo” if you are deleting system files, or files owned by others.
Our latest tutorials delivered straight to your inbox