Many Linux users get confused when they are in a Linux terminal and trying to delete files of folders. So in this Linux tutorial, I will cover how to delete files in Linux terminal! The Linux delete command is another basic command that is used everyday while in a Linux terminal. There are only 2 options that I use on a regular basis with the Linux
The usage of the Linux
rm command is
rm [options] [file|dir]. The 2 options I use most is
rm -r [dir] and
rm -f [file]. Now to explain these options for the Linux delete command and also give you some examples on how to remove a file with Linux.
rm -f [file] option is if you are wanting to force a file to be removed. This will not ask you “are you sure you want to remove [file]”. It will just delete the file regardless. So use this option for the Linux
rm command only if you know you want all of the file(s) deleted.
rm -r [dir] option is if you are wanting to delete a directory in Linux. The
rm -r option is to specify to remove recursively, meaning a directory and it’s contents.
You can use these 2 options for the Linux delete command together as well. You could use the command
rm -rf [dir] to delete a directory forcefully and recursively. DO NOT be fooled into running
rm -rf /,
rm -rf /* as this will remove all of your files and folders. Some people may come off as they are trying to help you with Linux and be a dick and tell you to run
rm -rf /* which you can then say bye bye to your files and folders.
You can also use
rm in a script type command after a pipe
| so you can delete filenames of the output. You can also use
rm to remove files that are produced from another command or script. Here is an example of how to remove a file found after
grep in Linux. Say if you have a folder
/home/max/images/family and in this directory you have 20 images that have the word copy in the title because you accidentally highlighted these 20 files, went to drag them into another folder in a GUI, and let go too early and created copies. So now you want to remove these copies, you would first
cd to that directory
cd /home/max/images/family now you can run
rm -f `ls |
copy` which will remove all the files that
copy matches. This same process could be done easier by running
rm -f *copy* instead, but these are just examples.
I hope reading this Linux tutorial on the Linux
rm command has taught you more than you already know and that you can continue reading through this Beginner Linux Tutorial website and learn even more about the Linux operating system! Have a great day and remember DO NOT run
rm -rf /* or any variation of it. I will write a section on Beginner Linux Tutorial explaining more of the commands that can harm your system to watch out for when people are trying to help you with Linux so that you will not be a victim of this. If you are unsure if a command will harm your system, you can always do a quick reference check by searching the net for that command, or even looking at the man pages for that command to see what it is that command/options will do. You can read your manpages by typing
man [command] like
man rm will bring up the manual for the Linux