
In this tutorial, you will learn how remove files and directories using the rm
command in Linux system.
Syntax of rm Command
Below is the basic syntax of rm
command:
rm [OPTIONS] [FILE_NAME]
Here,
- OPTIONS: You can specify the options to use with
rm
command. - FILE_NAMES: File name(s) which need to be delete.
Remove Files
Following are the multiple examples with different options to delete files using rm command:
How to Remove a File
To remove or delete a file in Linux you can use rm command without any options.
rm test.jpg
In above example, it will remove test.jpg
file from the system.
You should make sure before deleting any files because once a file has been deleted will not be restore.
If the file is write protected then you will be prompted for confirmation as given below. You should press Y
and hit Enter
key to continue to delete, Otherwise if file is not write protected it will be deleted without any prompt.
rm: remove write-protected regular empty file 'test.jpg'?
Delete Multiple Files
Also you can delete multiple files using rm
command without passing any options. You just need to separate file names by space
.
rm test1.jpg test2.jpg test3.jpg
Once you run above command it will delete all test1.jpg
,test2.jpg
and test3.jpg
from system.
3. Prompt Before Deleting File
Specify -i
option with rm
command to show prompt before deleting a file in Linux system.
rm test1.jpg test2.jpg test3.jpg
When you run above command it will prompt you before deleting each file. If you really want to delete then press Y
or N
.
It will show like as below:
4. Delete Write Protected File without Prompt
If you don’t want to see any prompt even if file is write protected or not you can use -f
option with rm
command.
rm -f data.pdf
In above example data.pdf
file will be deleted without any confirmation even if that file is write protected.
5. Remove Multiple File with Regular Expression
When you have requirement to delete multiple files with specific type then you should use regular expression. With rm
command you can use regular expression to delete files as give below:
rm *.jpg
Above command will file all the files with .jpg
extension and delete from current directory. Like this, you can specify any regular expression as per your requirements.
Remove Directories
1. How to Remove a Directory or Folder
When the directory is empty then you can remove using -d
option with rm command. Run the below command to delete empty directory:
rm -d dirname
To delete non-empty directory run below command:
rm -r dirname
Same as file deletion, if the file is write protected then it will prompt you for confirmation.
2. Remove Multiple Directories or Folders
You can delete multiple directories simultaneously using -r
option with rm command and separate file with space one by one.
rm -r dirname1 dirname2 dirname3
Above command will delete all directories dirname1
, dirname2
and dirname3
.
3. Delete Write Protected Directory without Prompt
When you do not want any type of confirmation at the time of directories deletion you should use -rf
along with rm command.
rm -rf dirname
This command will delete directory without any confirmation even if file is write protected or not.
Conclusion
Finally, you have successfully learned to Remove Files and Directories Using Linux Command.
If you have any queries you can leave comment at below comment box.
Leave a Reply