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

rm command

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

rm same extension

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/

Remove empty directory

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/

Remove directory

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/

Interactive remove

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

Force remove

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.

Force remove

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.

Force remove

Conclusion

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.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: