rm is a command on UNIX systems to delete files. In this article at OpenGenus, we have explained how to use this command rm to delete multiple files and other variants.
Table of contents:
- rm multiple files
- rm all files
- Variants of using rm
rm multiple files
To delete multiple files at once using rm command, use the following command:
rm <file1> <file2> <file3>
rm all files
To delete all files in the present working directory (pwd), use asterisk (
*) to select all files. The command is as follows:
In case, you would like to delete everything including directories, use rf option. The command is as follows:
rm -rf *
Variants of using rm
You can delete all files with a specific pattern. For example, if you want to delete all files with file extension .exe, use the following command:
Similarly, if you want to delete all files that contain "closed" in its filename, use the following command:
Note, there is an asterisk before and after the word closed.
To delete all files within a directory named "hen", use the following command:
To delete everything including files and sub-directories within a directory named "murk", use the following command:
rm -rf murk/*
This will make the directory "murk" empty.
Remember, these are very powerful commands and you should use with full responsibility. If you are a beginner, try to avoid using these commands and even if you have to, delete files one by one only.
Only once you are a confident developer and are working in full sense, use these command carefully. Take care as many developers have accidently deleted important files. Recovering is an option but this will require an expert.