Deleting a file in Linux is a fundamental task that every user will find themselves doing frequently. If you’ve recently installed Ubuntu Linux and are wondering how to delete files, we’ve got you covered in this guide.
In this tutorial, we’ll show how to delete files through GUI and command line on Ubuntu Linux. You’ll also see how permissions play a role in what files you’re able to delete or not.
In this tutorial you will learn:
- How to delete files via GNOME GUI
- How to delete files via command line
- How permissions determine which files you can delete
|Category||Requirements, Conventions or Software Version Used|
|Other||Privileged access to your Linux system as root or via the
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – requires given linux commands to be executed as a regular non-privileged user
How to delete file via GUI
The process for deleting a file on Linux, through the graphical interface, is going to vary a little depending on the desktop environment that you have installed. But the only real difference you’ll see is that some of the menus look a little different.
Once you know how to delete files on one GUI, you’ll have mastered them all. In the steps below, we’re using the GNOME desktop environment, which is the default for Ubuntu and most likely what you have installed already.
- Right click on the file you wish to remove, and click “move to trash.” On some desktop environments, the option may simply be called “delete” or something similar. Alternatively, you can highlight the folder and click
Deleteon your keyboard.
- The file has not yet been permanently deleted, but has rather been moved to the trash bin. If we change our mind about deleting the file, we can recover it from the bin. To permanently delete the file, along with any other contents you may have moved to the trash bin, right click on the trash bin icon and press “empty trash.”
- You may come across some files that you don’t have the option to delete. This is the case for system files or files that are owned by other users on the system. If you try to right click these files to delete them, you’ll notice that the option doesn’t appear. If you’re still determined to delete the file anyway, you should use the root accound via command line. We’ll show you how in the next section.
How to delete file via command line
rm command (short for “remove”) is used to delete files on Ubuntu. The most basic form of the command is to simply specify the location of a file in your command. You can either use the absolute path or relative path to the file.
$ rm /path/to/example.txt
The above command will permanently delete
example.txt from the system, assuming that the user has proper permissions on the file. Just like with the GUI method, you normally can’t delete system files or those that belong to other users, unless you have write permissions on the file or directory.
Depending on the permissions of the file and the directory in which it resides, you may see a confirmation prompt that asks if you want to delete the file. You’ll have to answer “yes” to proceed with the deletion.
$ rm example.txt rm: remove write-protected regular file 'example.txt'? yes
You may notice how we don’t get much room for error, like we do with the GUI method. There is no trash bin for the command line. To make things a little less risky, we could also use the -i (interactive) option, which will ask us for verification before deleting.
$ rm -i example.txt rm: remove regular file 'example.txt'? yes
If you have a file that you want to delete without any confirmation, you can use the
-f (force) option to forcefully delete it. Be careful with this one, as it suppresses warnings and will basically delete anything you tell it to, even if doing so is harmful to the system.
$ rm -f example.txt
If you need to delete a file but don’t have the proper permissions to do so, you can always use the root account to delete it. Just preface your command with
$ sudo rm /path/to/example.txt
You can also remove multiple files at once. In this example, we delete three different files in a single command.
$ rm file1.txt file2.txt file3.txt
Or just use a wildcard.
$ rm file*.txt
In this guide, we saw how to delete files on Ubuntu Linux through GUI and command line. We also learned about how file permissions can affect our ability to delete files, and how to bypass them. This is a common task that all users should master. As usual, the command line method offers us a bit more control over the process, but both methods are equally viable. Use whichever one is more convenient for you.