Removing a file in a Linux system is a fundamental task that users frequently carry out. Deleting these files creates extra spaces in your system that users can use to install new packages or save new files. If you're an Ubuntu user who doesn't understand how to delete a file or multiple files in Ubuntu, you're in luck. We'll explain how to delete files in Ubuntu using the command line or the GUI (Graphical User Interface). It's crucial to note that you need some permissions to delete a file in the Ubuntu Linux system; let's take a look.
Part 1. How To Delete a File in Ubuntu Using Graphical User Interface (GUI)
To delete a file using the GUI, you need to have writeable permissions on the file. Additionally, you have to be logged in as a root user on the Ubuntu system before you can delete a file through this process. The GUI option is best for you if you want to see the files before you delete them. This is because the GUI allows you to select and delete files in just a few clicks. Below are the simple steps to delete a file in Ubuntu through GUI.
Step 1: Open your file manager and navigate to the file's location before you delete it.
Step 2: You can select multiple files to delete. Hold the Shift key as you choose the files, then delete them. This method allows you to select files next to each other. Alternatively, you can hold the Ctrl key as you choose the files, then press the 'Delete' button.
Step 3: Once deleted, files are not gone from the machine. Rather, they're moved to the Trash folder to be recovered.
Step 4: Locate the Trash folder and choose Empty Trash at the top of the menu.
This action would automatically empty all the files stored in the Ubuntu Trash folder, ensuring that the files are gone permanently.
Part 2. How To Delete a File in Ubuntu Using Terminal
Another remove file Ubuntu method you can use is through the Terminal. There are two effective commands that you can use to delete a file in Ubuntu: the
rm commands. The
rm command is quite popular and removes multiple files from a folder or directory. On the other hand, the
unlink command works to remove a single file at a time.
Remove a File in Ubuntu Using the rm Command
The rm command is effective for deleting directories and files in Ubuntu. It is a powerful command and should only be used cautiously, as files deleted through this method can be difficult to recover. You can use the following options alongside the
- '-f'-This option deletes all read-only files from your system without requesting confirmation.
- '-i'-This option prompts users for confirmation before removing or deleting a file.
- '-v'-This option ensures the system displays the file names on the terminal screen while it is being deleted from the system.
- '-I'-This option also shows the user prompt whenever you're trying to delete more than three files simultaneously. It's best used when you're deleting files recursively.
- '-r' or '-R'-This option deletes the current directory alongside all files and subdirectories.
- '-q'-This option suppresses every warning that might pop up. However, you'll still find the error messages displayed on the screen.
rm command, you can either delete single or multiple files in the Ubuntu system. To delete a file in the Ubuntu system using the
rm command. Below is the syntax to get it done:
$ rm path/to/file.txt
An example is if we want to remove a text file from the downloads folder. To do this, follow the steps below.
Step 1: First, open the Terminal and then run the rm command below
$ rm /jome/sam-pc/Downloads/demo.txt
Step 2: The prompt will ask you to confirm your actions to delete a write-protected file from Ubuntu, enter 'y', and then press the 'Enter' key. If the file isn't protected, the file will be deleted from your system without displaying a confirmation prompt.
Step 3: If you want to delete a single file with the command rm, run the below command.
Step 4: If you want to delete multiple files simultaneously, list the file names right after the rm command. You need to separate file names by space.
rm DeleteMe.odt DeleteMe2.odt
Step 5: If you also want to remove all files with a specific file name, then you should run the command below.
Step 6: If you have a folder containing various file types and want to delete all the files with this file type, enter the rm command closely followed by an asterisk and the file type. An example is:
This would automatically delete all files that end with .odt.
Step 7: Delete all the files and subfolders in a specific folder. Enter the command below to complete the process.
rm -r files/*
In this case, it will delete all the files in the folder 'files.' The folder itself will remain, but it will be empty.
Remove Files in Ubuntu Using the unlink Command
Another Ubuntu delete file method you can use when deleting files through the Terminal is the
unlink command. It allows you to delete a specified file from the system. How does it do this? The unlink command eliminates a file's symbolic link, ensuring the filename is no longer associated with any data.
To delete or remove a file using the
unlink command, type the
unlink command followed by the name of the file you would like to delete. The syntax of the
unlink command is stated below:
unlink command is similar to
rm, you should note some crucial differences. The
unlink command doesn't support any option apart from the
-help (shows the help page) and
-version (shows the version number) options. Due to this, you can't control how the unlink command behaves during the deletion process.
unlink command doesn't support multiple file names; you can only delete one file at a time.
Part 3. What To Do if You Accidentally Delete a File in Ubuntu?
If you mistakenly delete a file in Ubuntu, you can recover the file with Linux data recovery software like Wondershare Recoverit. Wondershare Recoverit is selected as the Leader in Data Recovery by G2 users because it is easy to navigate and assists users with hundreds of data loss scenarios. Some of the top features offered by Wondershare Recoverit include:
- It works with all mainstream Linux distributions like Linux Mint, Ubuntu, Manjaro, Debian, etc.
- It helps recover data from 500+ loss scenarios like disk formatting, power outage, lost partition, virus attack, and more.
- Its simple interface allows you to recover data from Linux hard drives with just a few clicks.
Below is a clear video tutorial on recovering deleted files from Ubuntu using Recoverit Linux Recovery.
Alternatively, you can follow the steps outlined below to recover deleted files in Ubuntu.
Step 1: Launch the program after the installation and navigate to NAS and Linux Linux Recovery.
Step 2: Now, provide the required information to connect the data recovery tool to your Linux device. Once you provide the information, press 'Connect' to proceed.
Step 3: Once the remote connection is established, Recoverit will begin an automatic scan to look for the deleted files in your Ubuntu system. You can filter the results by date created or modified, file types, and more. You can also stop the scan midway if you find the desired file.
Step 4: Once the scan is complete, you can preview the files to ensure they're what you want. After confirming, click 'Recover' and choose a new location to store your recovered files.
Beyond Wondershare, you can explore many other options when looking to recover deleted files in Linux. However, Recoverit saves you time.
This guide taught us how to delete files in the Ubuntu Linux system using GUI and the Terminal. Since storage space plays a big part in keeping your system in the best condition, these methods will help you delete unwanted files easily. You can also recover deleted files from Ubuntu using Wondershare Recoverit if needed.