Q&A – All You Wanted To Know About rm Command In Linux

By | 23/08/2013

The rm command is a double-edged sword that can simply do its work when used carefully but can also cause close to irreparable damage when used carelessly.  Most of the tutorials explain either the basics of rm command or its advanced features and usage. But, I think it is important to explain the basic usage, advanced usage and the prominent careless mistakes that people do while using this command — all in one article.

In this article, I have tried to present enough meat on rm command so that all types of users — from a newbie to an experienced Linux command line user — are benefited from it.

 

The rm Command Examples In Linux

Q1. I don’t know how to use rm command. Can you please explain with a basic example?

Ans. The rm command is very easy to use. To delete a file, just pass the file name as an argument to rm command.

Here is an example :

rm-1

So you can see that the rm command can be used to delete a file easily. One thing that you should keep in mind is that you should have write-permissions of the parent directory to delete a file inside that directory.

Here is another example:

rm-1-1

So you can see that the user could not delete the file user_file as the parent directory of this file ie MLB_user was created by some other user.

If you still want to delete a file kept in a directory for which you do not have write-permissions then you have to acquire root privileges to delete that file. This can be done through sudo or su. For more information, read our article on sudo vs su.

Q2. How to delete directories through rm?

Ans. To delete a non-empty directory, just use option -r or -R with rm command.

Here is a directory that contains two files :

rm-2

Now, to delete this directory, use -r or -R option with rm command.

rm-2-1

So you can see that the directory got deleted.

Now, suppose there is a requirement to delete only empty directories then you can use -d option with rm command to make sure that the rm deletes only empty directories.

Here is an example :

rm-2-2

So you can see that -d option makes sure that the rm command doesn’t delete any non-empty directory.

Q3. How to make sure that the rm command prompts before deleting file(s)?

Ans. To make sure that the rm command prompts before deleting file(s), use -i option.

Here is an example :

rm-3

So you can see that the rm command in this example used -i option and wild-card character *.  This wild-card character indicates everything present in this directory. Once the command was executed, the rm command prompted for all the files before deleting them. User can specify Y or y to confirm the deletion and N or n to defer the deletion.

The rm command also provides an option -I through which the command prompts once before deleting more than 3 files or when deleting subdirectory.

For example, suppose the directory dir_new contains some directories and files.

rm-3-1

Now, use -I option with rm command to delete everything in the parent directory dir_new.

rm-3-2

So you can see that the rm command confirmed once before deleting all the contents in the parent directory.

Q4. How to stop rm command from producing errors for non-existent files or directories in output?

Ans. There can be times when it is required that the rm command silently ignores a non-existent file or directory. To fulfil this requirement, just use -f option with the rm command.

Here is an example where rm produces the error on stdout :

rm-4

Now, here is the same example when rm command is executed with -f option :

rm-4-1

So you can see that no warning or error was produced in the output.

Q5. The rm command cannot delete files having names that begin with a hyphen (-) or a space. What to do in these cases?

Ans. The rm command can successfully delete these type of files, provided it is used in correct way and with correct options.

To delete a file having name that begins with a hyphen (-), just use double hyphen (–) before the file name. The double hyphen (–) indicates to the command that anything after it that uses a single hyphen (-) is not a command line option for the command but rather an argument for the command.

Here is the wrong way to delete a file named -file1 :

rm-5

Here is the correct way to delete the same file :

rm-5-1

 

Similarly, if there is a file having a name that begins with a space, then use single quotes around the name while passing it as argument to rm command.

Here is the wrong way to delete such a file :

rm-5-2

Here is the correct way to delete such a file :

rm-5-3

 

Q6. How rm command works with soft-links and hard-links in Linux?

Ans. Depending upon how it is being used, the rm command does impact both links and original files or directories.

Lets first understand the impact on soft links. Suppose there is a soft link named sym_link that points to a directory named orig_dir.

rm-6

Now, try deleting the link through rm command in the following way :

rm-6-1

This deletes the symbolic link sym_link but does not delete the directory orig_dir (that it points to).  Same is true in case symbolic link points to a file.

But, if you use rm command in the following way :

rm-6-2

Then most of the POSIX complaint systems would give this error and do nothing but there could be some systems that would delete the directory pointed by the symbolic link if you use rm command this way. For more explanation on this, see the discussion here.

Now, let’s come to hard-links. The only important point to remember while deleting a file is that if there are multiple hard links present for this file then there is not much that the rm command can do in this case. If you even try to delete the original file through the rm command but if there exists even a single hard link to it then the file can easily be accessed through that hard link. So, the rm command in this case just reduces reference count.

Q7. How to resolve Argument list too long error?

Ans. Sometimes, when you try to run the command rm -rf * over a directory that contains a huge list of files and sub-directories then you might get this error because rm command is not able to handle such a huge list arguments when shell expands and replaces * with names of all the files and sub-directories on command prompt.

To get rid of this error, either try executing :

find . -name "*" -print0 | xargs -0 rm

or try executing :

find . -name "*" -exec rm -f {} \;

Both these commands let the rm command to process and remove files in batches.

Q8. Despite of knowing everything about the rm command, I still accidentally delete some important files/directories while using the rm command. Is there a way to restrict rm from deleting some important file(s) or directories?

Ans. Unfortunately there is no straight forward way to do this with rm command. Some hacks that you can do are :

  • Try aliasing rm = rm -i. This will make sure that rm command always prompt before deletion.
  • Try aliasing the rm command to a script that just moves the files to trash. You can empty trash any-time you want.
  • Try using the trash command.
  • There exists a wrapper known as safe-rm that lets you create a configurable blacklist of files and directories that should never be removed. Just try it out and see if it can solve your problem.

Q9. How to make sure that I do not accidentally delete everything by typing rm -rf /* instead of rm -rf ./* ?

Ans. Well, my suggestion would be to make sure that you do not have root privileges while playing around with rm command. This will automatically prevent the blunder from happening but if you are a system administrator who plays around with dangerous commands as a root user or by using sudo, then I highly recommend you to go through the discussion – how-do-i-prevent-accidental-rm-rf. The discussion provides awesome insights into the details of this problem and its solution.

 

NOTE – Enjoyed the article? Want to read more command line tips and tricks? Read our articles on 20 Interesting Command Line Tips And Tricks and Funny Linux Commands.

3 thoughts on “Q&A – All You Wanted To Know About rm Command In Linux

  1. bjd

    Glad to see you didn’t give the example of aliasing rm to ‘rm -i’.
    That will, for the better part, give a (false) sense of security, but will,
    some time –may it be long, may it be short–, some time, some
    other system, theirs, or ours — that will bite, and draw blood —
    and ire.

    Reply
  2. Himanshu

    [On Behalf of David]

    With your two examples:
    find . -name “*” -print0 | xargs -0 rm
    find . -name “*” -exec rm -f {} \;

    the first one is a *LOT* more efficient because it’ll pass as many command line arguments to each rm invocation before it runs out of room and starts the next one. The second one will run one rm command per file. There is another way to do this that is just as efficient as the xargs example:

    find . -name “*” -exec rm -f {} +

    Reply
  3. Sari

    Hi
    Suppose i have one dir in my home directory /home/sarayu/
    unit1 is under sarayu
    unit1 has –> unit1.1 and unit1.2
    unit1.1—>file1.txt file2.txt file3.txt
    right now i am in unit1 directory
    > /home/sarayu/unit1 : from here i want to copy all file from unit1.1 to unit1.2 after i wanna remove unit1.1 directory from the same place

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *