How to Delete in Linux Terminal rm Command

Linux Remove DeleteMany 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 rm command.

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.

The 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.

Now the 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 | grep copy` which will remove all the files that ls | grep 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 rm command.

Comment Linux tutorial or Leave a Trackback

8 Comments

  1. slender
    Posted August 14, 2009 at 6:16 am | Linux tutorial comment

    pliz provide a command to delete a record in linux.

  2. Posted August 21, 2009 at 10:19 am | Linux tutorial comment

    What are you wanting to delete? As the tutorial says you can delete anything on the filesystem with rm -rf filename

  3. Rampspost
    Posted September 16, 2009 at 7:25 am | Linux tutorial comment

    Can you give a command, wich is useful for deleting temporary files in ALL user profiles in windows (with livecd).

    I mean paths like:
    C:\Documents and Settings\user1\Local Settings\Application Data\Mozilla\Firefox\Profiles\sf1u1syx.default\Cache
    C:\Documents and Settings\user2\Local Settings\Application Data\Mozilla\Firefox\Profiles\tyrwertr.default\Cache
    C:\Documents and Settings\etc\Local Settings\Application Data\Mozilla\Firefox\Profiles\wetytyuy.default\Cache

    and

    C:\Documents and Settings\user1\Local Settings\Temp
    C:\Documents and Settings\user2\Local Settings\Temp
    C:\Documents and Settings\etc\Local Settings\Temp

    and so on…

  4. Posted September 22, 2009 at 11:10 am | Linux tutorial comment

    You would first want to mount the ntfs partition using ntfs-3g so you have read/write permissions to ntfs partitions. ntfs-3g /dev/your-c-partition /media/ntfs Then you can download this script file I wrote for you. Edit the script, you will need to change $MNTPNT="/media/ntfs" to where ever your ntfs partition is mounted. Then make sure you chmod ugo+x delete-windows-temp-files.sh and then run ./delete-windows-temp-files.sh

    Hope this helps, if you need more help please let me know. Have a great day!

  5. Moss
    Posted September 23, 2009 at 2:55 pm | Linux tutorial comment

    What do I do if the rm -rf function doesn’t work? I keep getting permission denied messages for several files linked to the RealPlayer I downloaded.

  6. Posted September 29, 2009 at 2:16 pm | Linux tutorial comment

    Most likely those files are owned by another user. You can run ls -l in that directory and you will see who owns the files and what the permissions are. You will see something like this -rw-r--r-- 1 max max 543504 Sep 19 15:40 test.avi the order is drwxrwxrwx .... user group .... the d is if it is a directory, r is read, w is write, x is execute, first set of rwx is user permissions, second is group, third is all other users. If you do not have permissions then su to root to delete them or su to root and use chown -R your-username your-group /path/to/files to change permissions to your user.

  7. Hawa
    Posted March 14, 2011 at 6:12 am | Linux tutorial comment

    Would u pliz provide a method to delete a record in linux files. i have been trying to do it but c’nt find the command

  8. Posted March 20, 2011 at 12:24 am | Linux tutorial comment

    Hawa,
    If you are trying to delete a file or folder use rm -Rf /path/to/file

One Trackback

  1. […] rm – shell command in Linux to remove files|directories. […]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*