Do you need control over your Ubuntu to delete file? Deleting files is a common task in your daily life as an admin, whether getting rid of unwanted junk files or simply clearing up space on your hard drives.
Consider today your lucky day, as this tutorial will show you many ways to delete files in Ubuntu.
Read on and clean up your system in a jiffy!
Prerequisites
This tutorial comprises hands-on demonstrations, which require an Ubuntu machine. This tutorial uses Ubuntu 20.04, but the steps should be similar to other versions of Ubuntu.
Deleting Files via the GUI (Graphical User Interface)
If you like seeing the files right before you as you delete them, the GUI is your best option. The GUI (the most common method) lets you select files and delete them in a few clicks.
To delete files via the GUI:
1. Open your file manager and navigate to the file’s location to delete.
2. Next, delete your target file with either of the following:
Accidentally deleting a file can be nerve-racking. But no worries. Press CTR+Z to undo the delete action.
3. Select and delete multiple files in one go with either of the following:
- Hold the Shift key as you select the files, then delete them. This method selects files that are next to each other. If you select two files far from each other, all files in between are also selected.
- Hold the Ctrl key as you select the files, then delete them. Unlike the Shift key, this method lets you select specific files to delete.
But be careful not to drag the mouse as you select the files; otherwise, you will create a copy of the selected files to the exact location — more files instead of less.
Once deleted, files are not actually gone from your machine. Instead, the files are moved to the Trash folder, where you can recover them if needed.
To deselect a file, while holding down the Ctrl or Shift key, click on the file to exclude.
Once deleted, files are not actually gone from your machine. Instead, the files are moved to the Trash folder, where you can recover them if needed.
4. Look for and double-click the Trash icon on your Desktop, which opens the Trash folder in your file manager.
The Trash folder is located in the home directory. But the Trash icon is usually found on the Desktop for easy access.
Delete files as you did in step three, as shown below.
Take caution when deleting files in the Trash, as you cannot recover permanently deleted files. But if you accidentally deleted important files you were supposed to restore, there are file recovery tools you can use to get the files back.
5. Lastly, right-click the Trash icon and select Empty Trash from the context menu, as shown below.
This method is best if you wish to go ballistic and delete everything in the Trash. Keeping lots of files in the Trash can clog up your system and slow it down.
When prompted, click Empty Trash, as shown below, to permanently delete all the files in the Trash.
Running the rm
Command to Delete Files
Perhaps you prefer to delete files over a command-line environment than clicking through the GUI. Lucky for you, the rm
command lets you delete files via your terminal.
But be careful, though, as deleting files with the rm
command is like deleting files from the Trash folder — gone forever.
One solution to prevent accidentally losing your files as you run the rm command is to use version control such as Git. But this topic is beyond the scope of this tutorial.
The basic syntax of the rm
command is as follows where:
options
(optional) – Let you modify the behavior of therm
command. You will learn about some commonly used options later in this tutorial.path/to/filename1/filename2/filename3
– The actual paths to the files you want to delete. Note that you can delete more than one file in one go, so long as you specify the file paths separated by spaces.
rm options path/to/filename1/filename2/filename3 ...
You will go through each of the following sections to see how the rm
command works.
Deleting Files from a Specified Directory
The most basic form of the rm
command is deleting regular files, like text files, from a specified directory.
1. Open your terminal, and run the following rm command to delete a file (ubuntu-delete-file-demo.txt) located in the Documents directory.
rm /Documents/ubuntu-delete-file-demo.txt
As shown below, the command provides no output since the files are deleted silently, which is the default behavior of the rm command.
2. Now, run the ls command below to list all files in the Documents directory.
ls -l /Documents/
As you can see below, the ubuntu-delete-file-demo.txt file no longer exists.
3. Lastly, run the below command to delete the file with verbose (-v) output.
The -v option helps if you do not want to keep running the ls command each time you delete a file. This option tells the rm command to print a verbose output.
rm -v /Documents/ubuntu-delete-file-demo.txt
As shown below, the rm command displays the filename being deleted. This behavior can be helpful when you want to know exactly what the rm command is doing.
Deleting Files with Spaces in Filenames
You have successfully deleted files from a specified directory. But obviously, some filenames will have spaces in between.
To get a better view of how deleting files with spaces in filenames:
1. Run the following command to try and delete a file called ubuntu delete file demo.txt in the Documents directory, which contains several spaces.
rm -v /Documents/ubuntu delete file demo.txt
In Linux, spaces are used to separate multiple arguments. Since the filename you specified has spaces in between, the shell interprets the filename as four different files: /Documents/ubuntu, delete, file, and demo.txt.
If the rm command cannot find these files, you will get the following error messages.
2. Now, run the below command to delete a file that contains spaces in its name. The double quotes tell the shell to treat the entire file name as a single argument.
rm -v ~/Documents/"ubuntu delete file demo.txt"
Alternatively, use an escape character, the backslash (\), which tells the shell to treat the next character literally. In this case, the shell will treat the spaces as literal spaces instead of argument separators.
rm -v ~/Documents/ubuntu\ delete\ file\ demo.txt
Setting a Confirmation Prompt for Deleting Files
Deleting files can be catastrophic if you delete the wrong ones. Why not set a confirmation prompt? Doing so ensures you are not deleting files you are not supposed to.
Run the following command to prompt a confirmation (-i) before deleting a file (ubuntu-delete-file-demo.txt) from the Documents directory.
rm ~/Documents/ubuntu-delete-file-demo.txt -vi
When prompted, type Y and press Enter to confirm deleting the file.
Deleting All Files by File Extension
Need to speed up deleting files? Specifying file names with the same file extension can be tedious. Why not use a wildcard character (*) instead? This wildcard character lets you delete files that match a specific pattern.
This behavior can help delete many files that share the same naming convention. But in this case, you will delete files with the same extension.
Run the below command to delete all files (*) that end in the .txt file extension in the Documents directory.
rm -v ~/Documents/*.txt
Now, run the following command to delete all (*) files text files (.txt) that contain the word demo in the Documents directory. Piping the grep command lets you narrow the search for the files before deleting them.
rm -v ~/Documents/*.txt | grep -i demo
Below, you can see that the only ones deleted are text files with filenames that contain the word demo.
Removing a File with the unlink
Command
Enjoying how you can delete files in Ubuntu? But mind you that the rm command is not the only way to delete a file in Linux. The unlink command lets you delete a specified file from the system. Why? The unlink command removes a file’s symbolic link, making the filename no longer associated with any data.
The syntax for the unlink command is as follows, where you only need to specify the filename of the file to delete.
unlink filename
The unlink command is similar to rm but has a few essential differences:
- Does not support any options except for the –help (shows the help page) and –version(shows the version number) options. As a result, you cannot control how the unlink command behaves when deleting a file.
- Does not support specifying multiple file names. You can only delete one file at a time.
Run the below unlink command to delete a file (ubuntu-delete-file-demo.txt) in the Documents directory.
unlink ~/Documents/ubuntu-delete-file-demo.txt
The unlink command deletes the file without any confirmation or output, as shown below. Be careful when using this command, as it permanently deletes a file, which is irreversible.
Conclusion
Storage space plays a big part in keeping your system in its tip-top shape, and deleting unwanted files every now and then lets your system breathe fresh air. Luckily, in this tutorial, you have learned different ways to delete a file using the GUI and command-line environment.
At this point, you should be confident enough to clean your storage space with all the junk files that have piled up so far.
But manually deleting files all the time can be unproductive at times. Why not automate file deletions with scripts or by creating cron jobs instead?