Linux rm Command Examples – Should be Aware and Cautious
When a file no longer needed, we may delete it to save storage space. On Linux system, we can use rmcommand to do it.
What is rm
rm is a command to delete a file / directory without confirmation by default. Because of this behavior,users should really sure before deleting files.
Run rm command
To run rm command, type rm followed by filename. Please remember, that by default rm will not ask any confirmation. Here’s an example of rm in action.
Remove a file
$ rm computer.log
To remove a file named computer.log, we can do it with the command above.
Remove multiple file with specific extension
If you have multiple file with the same extension and you want to delete them, you can use this syntax :
$ rm *.log
From the above screenshot, all files with .log extension are deleted at the same time.
Remove a directory
Removing directory is a little bit tricky. If you are sure that the directory is empty, then we can use -dparameter to remove it.
$ rm -d documents/
But when the directory is not empty, you have to empty the directory first, or you can remove them recursively. To remove recursively, use -r or -R parameter.
$ rm -r movie/
Add interactive confirmation before deleting
If you feel more comfortable to have interactive confirmation, you can use -i parameter with rm command. Here’s a sample of deleting directory recursively with interactive confirmation.
$ rm -ri movie/
Using force deletion
Using force deletion mean that rm will remove all files without any confirmation, even the file is write-protected or not. Here’s some samples.
Remove file with write-protected access
We see that movie.list has acces read-only for owner, group owner and everyone. When we tried to remove it, rm will ask a confirmation about it, but the file is successfully deleted. Using -f parameter, rm will not ask any confirmation. Take a look at screenshot below.
But if the directory where the files is located is write-protected, then the file cannot be removed, even the file itself is not write-protected.
We always should be careful when we want to delete something. Once again, by default rm command will not ask any confirmation when it delete something. As usual, you can always type man rm or rm –help to display rm manual page and explore it more detail.