How To Delete a File In Linux

By | 2019-01-10

There are two main ways to delete a file on a Linux based operating system. Keep in mind that removing a file as described in this guide just removes references to the underlying storage system. Forensic tools may be able to recover their contents. There are ways to delete files that prevent their recovery, but that is a topic for another guide.

Desktop Environment

If you are using a desktop environment, you can usually right-click on the file’s icon and select delete from the menu that appears. If it is greyed out or you get a permission denied error, then you likely don’t have write access to the directory containing the file. Some desktop environments, such as MATE, have an option in the right-click menu to open a directory as administrator. If you open the directory containing the file as an administrator, you should be able to delete the file.

delete_file_mate

Right-Click and select Delete

Notice how there is also a Move to Trash option. If you don’t have a Delete option, choose this. Just remember to empty your trash at some point. If you right-click on the trash icon on the desktop, there is usually an empty trash option.

If you don’t have a delete option, most desktop environments have a setting that will add it.

open as administrator

Right-click on the directory icon and select Open as administrator

Command Line

If you are using the command line, you can use the rm program. To use rm, pass it the path(s) to the file(s) you wish to delete as arguments.

tyler@desktop:~/delete$ ls
delete_me delete_me2 delete_me3
tyler@desktop:~/delete$ rm delete_me 
delete_me2  delete_me3
tyler@desktop:~/delete$ rm delete_me2 delete_me3
tyler@desktop:~/delete$ ls
tyler@desktop:~/delete$

If you get a permissions error with rm, you can use sudo or su to run rm as root, the Linux superuser.

tyler@desktop:~/delete$ ls
su_delete  sudo_delete
tyler@desktop:~/delete$ rm sudo_delete su_delete 
rm: remove write-protected regular empty file 'sudo_delete'? y
rm: cannot remove 'sudo_delete': Permission denied
rm: remove write-protected regular empty file 'su_delete'? y
rm: cannot remove 'su_delete': Permission denied

Using sudo:

Depending on how your system has sudo configured, you may be prompted to enter your password. The password it is asking for is your user password, not the root password.

tyler@desktop:~/delete$ sudo rm sudo_delete 
tyler@desktop:~/delete$ ls
su_delete

Using su:

You will most likely be prompted for a password. Enter the root password.

tyler@desktop:~/delete$ su -c "rm su_delete"
Password: 
tyler@desktop:~/delete$ ls
tyler@desktop:~/delete$

I also have a guide covering deleting directories in Linux.