How To Delete Files And Folders Using Linux

Nautilus Delete Files
Nautilus Delete Files.
Was this page helpful?

Introduction

This guide will show you all of the different ways for deleting files using Linux. 

The easiest way to delete files is to use the file manager that comes as part of your version of Linux. A file manager provides a graphical view of the files and folders that are stored on your computer. Windows users will be familiar with an application called Windows Explorer which is in itself a file manager.

There are lots of different file managers for Linux but here are the ones most commonly installed:

  • Nautilus (aka Files)
  • Dolphin
  • PCManFM
  • Thunar
  • Caja

Nautilus is part of the GNOME desktop environment and is the default file manager for Ubuntu, Linux Mint, Fedora, and openSUSE.

Dolphin is part of the KDE desktop environment and is the default file manager for distributions such as Kubuntu and the KDE versions of Mint and Debian.

Thunar is part of the XFCE desktop environment and is the default file manager for Xubuntu.

PCManFM is part of the LXDE desktop environment and is the default file manager for Lubuntu.

Caja is the default file manager for the MATE desktop environment and comes as part of Linux Mint Mate.

This guide will show you how to delete files using all of these desktop environments and it will also show how to delete files using the command line.

How To Use Nautilus To Delete Files

Nautilus can be opened in Ubuntu by clicking on the file cabinet icon on the launcher.

You will be able to find Nautilus on Mint by clicking on the file manager in the quick launch bar or via the menu. Any distribution which uses the GNOME desktop environment will have the file manager within the activities window.

When you have Nautilus open you can navigate through the files and folders by double clicking on them.

To delete a single file right click on its icon and choose "Move To Trash". 

You can select multiple files by holding down the CTRL key whilst clicking on the file and then press the right mouse button to bring up the menu. Click on "Move To Trash" to move the items to the recycle bin.

If you prefer to use the keyboard then you can press the "Delete" key on your keyboard to send items to the trash can.

To permanently delete the files click on the "Trash" icon in the left panel. This shows you all of the items that have currently been deleted but still recoverable.

To restore a file click on an item and click the "Restore" button in the top right corner. 

To empty the trash can click on the "Empty" button in the top right corner.

How To Use Dolphin To Delete Files

The Dolphin file manager is the default file manager with the KDE environment. You can launch it by clicking on its icon in the menu.

The interface is very similar to that of Nautilus and the delete functionality is much the same.

To delete a single file right click on the file and choose "Move to trash". You can also press the delete key however this pops up a message asking whether you are sure you wish to move the item to the trash can.

You can stop the message appearing again by placing a check in a checkbox.

To delete multiple files select all the files you wish to delete by holding down the CTRL key and left clicking on the files. To move them to the trash can press the delete key or right-click and choose "move to trash".

You can restore items from the trash by clicking on the trash icon in the left panel. Find the item or items you wish to restore, right-click and then choose "restore".

To empty the trash right click on the trash option in the left panel and choose "empty trash".

You can permanently delete files without them going to the trash can in the first place by holding down the shift key and pressing the delete button.

How To Use Thunar To Delete Files

Most file managers follow the same theme when it comes to selecting, copying, moving and deleting files and folders.

Thunar is no different. You can open Thunar within the XFCE desktop environment by clicking on the menu and searching for "Thunar".

To delete a file using Thunar select the file with the mouse and right click. The main difference between Thunar and the two previously mentioned file managers is that both "move to trash" and "delete" are available on the context menu.

Therefore to send a file to the trash can choose the "move to trash" option or to permanently delete use the "delete" option.

To restore a file click on the "Trash" icon in the left panel and then find the file you wish to restore. Right click on the file and click on the "Restore" option in the menu.

To empty the trash right click on the "Trash" icon and choose "Empty Trash".

How To Use PCManFM To Delete Files

The PCManFM file manager is the default for the LXDE desktop environment.

You can open PCManFM by choosing the file manager from the LXDE menu.

To delete a file navigate through the folders and select the file you wish to delete with the mouse.

You can press the delete key to delete the file and you will be asked whether you wish to move the item to trash. You can also right click on the file and choose the "move to trash" option from the menu.

If you wish to permanently delete the file hold down the shift key and press the delete button.

You will now be asked whether you want to remove the file. If you hold down the shift key and press the right mouse button the menu option will now be displayed as "remove" instead of "move to trash".

To restore items click on the trash can and select the file or files you wish to restore. Right click and choose "restore".

To empty the trash right click on the trash can and choose "Empty Trash Can" from the menu.

How To Use Caja To Delete Files

Caja is the default file manager for Linux Mint MATE and the MATE desktop environment in general.

The Caja file manager will be available from the menu.

To delete a file navigate through the folders and find the file or files you wish to delete. Select the file by clicking on it and right click. The menu will have an option called "move to trash". You can also press the delete key to move the file to the trash can.

You can permanently delete the file by holding down the shift key and then pressing the delete key. There is no right click menu option for permanently deleting files.

To restore a file, click on the trash can in the left panel. Find the file to be restored and select it with the mouse. Now click on the restore button.

To empty the trash can click on the trash can and then the empty trash can button.

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

    Summary

    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.