If you recently switched over from another platform to Ubuntu, you’re likely still deleting files via the GUI as it feels familiar to your previous OS.
This is fine, but you’ll eventually encounter cases where you can’t delete a file this way (mostly due to permissions issues). It’s also not ideal if you want to delete a lot of files together.
As such, we’ll cover different ways to delete files in Ubuntu in this article. The most popular method is rm, while beginners will find that trash is a better fit for their file deletion needs.
Rm is the standard way to remove files and directories in Linux. Let’s start with files. To delete a file named test.txt from the current directory, use
Or use the full file path like so
To delete multiple files from the current directory, specify their filenames like so
rm test.txt tast.txt tost.jpg
You can also use the * wildcard to delete multiple files at once. This is useful when dealing with a very large amount of files. Some examples are listed below.
rm *.tar– Delete all files with the .tar extension.
rm t*– Delete all files starting with the letter t.
rm Test*– Delete all files starting with the string ‘Test’.
rm *test*– Delete all files with ‘test’ in the filename.
Rm is easy to mess up with, and handling multiple files together with wildcards can make mistakes even more likely. And rm doesn’t issue warnings when deleting non-write-protected files. The -i option can be useful here if you want a confirmation prompt before removing every file.
rm -i *.tar
If you want to delete a directory, you can specify the path and use the recursive option like so
rm -r /home/anup/Desktop/testfolder
Finally, if any errors prevent you from deleting a file/directory, add the force option as shown below. Do exercise caution with -rf though. You can easily delete the wrong directory as you won’t see any warning prompts.
rm -f /home/anup/Desktop/testimage.png
rm -rf /home/anup/Desktop/testfolder
Unlink deletes a link to a file, decreasing the file’s link count by 1. Assuming there are no more hard links to the file and no process is currently accessing the file, the file is deleted. It’s a safe and easy way to delete individual files.
To delete a file named test in the current directory, simply use
Or you can specify the full path like so
As we said, stories of people deleting files, directories, or even entire disks accidentally with rm aren’t uncommon. For beginners, we recommend installing the trash-cli utility. It’ll move deleted files and folders to the trashcan instead of permanently deleting them.
First, install it with
sudo apt install trash-cli
The syntax is similar to the previous two utilities.
Delete Files via the GUI
If you’d rather not use the terminal, deleting files graphically usually works too. Simply select the file and press the Delete key, or right-click the file and select Move to Trash.
By default, trashed files on Ubuntu are permanently deleted after 30 days. You can right-click the Trash icon and select Empty Trash to delete them immediately. You can also press Shift + Delete when deleting to permanently delete files.