How To Delete Files Using The Linux rm Command

How To Delete Files Using Linux
How To Delete Files Using Linux.


This guide shows how to delete files and folders using the Linux command line.

The command used to delete files is called "rm".

As well as learning the basic command for removing files you will be shown all of the available switches and why they are useful.

How To Remove A File Using The Linux Command Line

The basic syntax for removing a file using the Linux terminal is as follows:

rm /path/to/file

For example imagine you have a file called file1 in the /home/gary/documents folder you would type the following command:

rm /home/gary/documents/file1

There is no warning asking you whether you are sure so you need to be very sure that you have typed in the path to the correct file or the file will be deleted.

You can remove multiple files simply by specifying them as part of the rm command as follows:

rm file1 file2 file3 file4 file5

You can also use wildcards to determine which files to delete. For example to delete all of the files with the extension .mp3 you would use the following command:

rm *.mp3

It is worth pointing out at this stage that you need to have the necessary permissions for removing the files otherwise you will get an error.

You can elevate permissions using the sudo command or switch to a user with permissions to delete the file using the su command.

How To Get An "Are You Sure" Message When Deleting Files Using Linux

As mentioned in the previous section the rm command doesn't ask for confirmation before deleting the file.

It just does it indiscriminately.

You can provide a switch to the rm command so that it asks you whether you are sure before deleting each file. 

This is of course fine if you are deleting one file but if you are deleting hundreds of files it will become tiresome.

rm -i /path/to/file

For example if you want to remove all the mp3 files in a folder but you want to confirm each removal you would use the following command:

rm -i *.mp3

The output from the above command will be something like this:

rm: remove regular file 'file.mp3'?

To delete the file you have to press either Y or y and press return. If you don't want to delete the file press n or N.

If you want to be prompted whether you are sure you want to delete files but only when more than 3 files are to be deleted or when deleting recursively you can use the following syntax:

rm -I *.mp3

This is less intrusive than the rm -i command but of course if the command was going to delete less than 3 files you would lose those 3 files.

The output from the above command would be something like this:

rm: remove 5 arguments?

Again the answer has to be y or Y for the removal to take place.

An alternative to the -i and -I command is as follows:

rm --interactive=never *.mp3

rm --interactive=once *.mp3

rm --interactive=always *.mp3

The above syntax is more easily read and states that you will either never be told about the deletion which is the same as not supplying a switch to the rm command, you will be told once which is the same as running rm with the -I switch or you will be told always which is the same as running the rm command with the -i switch.

Removing Directories And Sub-Directories Recursively Using Linux

Imagine you have the following folder structure:

  • home
    • gary
      • documents
        • accounts
          • 2010
            • file1
          • 2011
            • file1
            • file2
          • 2012
            • file1
          • 2013
            • file1
          • 2014
            • file1
          • 2015
            • file1
            • file2
            • file3

If you want to delete the accounts folder and all the sub-folders and files you have to use the following switch:

rm -r /home/gary/documents/accounts

You can also use either of the following two commands:

rm -R /home/gary/documents/accounts

rm --recursive /home/gary/documents/accounts

How To Remove A Directory But Only If It Is Empty

Imagine you have a folder called accounts and you want to delete it but only if it is empty. You can do this using the following command:

rm -d accounts

If the folder is empty then it will be deleted but if it isn't you will receive the following message:

rm: cannot remove 'accounts': directory not empty

How To Remove Files Without An Error Appearing If A File Does Not Exist

If you are running a script you might not want an error to occur if the file or files that you are trying to remove doesn't exist.

In this instance you can use the following command:

rm -f /path/to/file

For example you can use this command to remove a file called file1.

rm -f file1

If the file exists it will be removed and if it doesn't you won't receive any message saying that it didn't exist. Ordinarily without the -f switch you would receive the following error:

rm: cannot remove 'file1': no such file or directory


There are other commands you can use to remove files such as the shred command which will prevent any recovery of the file.

If you have a symbolic link you can remove the link using the unlink command.

More From Us