Remove files and directories with rm command

by Daniel Pham
Published: Updated:
This entry is part 4 of 4 in the series Linux commands: Part 4 - Manipulating files and directories

The rm command is used to delete files or folders in Linux. Using the rm command is quite simple, this article will talk about it. It can be very simple but when used be careful. Why is that? I will say in the next part of the article.

Use the rm command

The rm command structure is as simple as:

rm [OPTION] item ...

This item, you can understand it is a file or a directory, can also be many files or folders.

Normally, when you type rm to delete a file or folder, there will be a confirmation prompt before the deletion. You can use the -f option to skip asking for confirmation before deleting.

Options are supported

The rm command supports a number of options as image below.

Remove files and directories with rm command
All options that rm command supported.

You can type below command to show all the options of rm command:

rm --help

Usually the two most commonly used options are -f and -r. Use -f to ignore the deletion before deleting and use -r if you want to delete a directory containing content (not an empty directory).

Remove files and directories with rm command
Examples of using rm command.

You can see the difference in the example image above. When using the -f option, the rm command will skip asking you if you are sure to delete the file. And when you want to delete a non-empty directory, you must use the -r option otherwise it will not be deleted.

Be careful when using the rm command

You know, in Linux, you can use wildcards. With rm as well, you can delete multiple files at the same time through the wildcard. For example when you type the command below:

rm -f *.log

That means you will delete all the files that end in .log in the current directory.

Read moreMove and rename in Linux with mv command.

But if you type wrong, just a space in front of .log then you will have big trouble. As follows:

rm -f [space] *.log

This will delete all the files in the current directory and notify you that there is no .log file at all.

The most dangerous joke. The following command is widely used to make jokes with newcomers to Linux. This command will completely destroy your operating system. Never run that command.

rm -f /*

Conclusion

This tutorial shows you how to use the rm command, which is quite simple. Besides, you have to be extremely careful when using the rm command because once you delete the data, it will be very difficult to restore as it was before.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Create directories with mkdir command

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.