Remove files and directories with rm command

Share this post to your friends !

This entry is part 4 of 4 in the series Linux commands: Part 4 - Manipulating files and directories
Remove files and directories with rm command
5 (100%) 1 vote[s]

WriteBash - 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-01 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-02 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.

Continue reading the series«« Previous part: Create directories with mkdir command
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.