If you are a new Ubuntu user and don’t know how to delete a file or multiple files in Ubuntu using the command line or via the GUI, we will cover in this article all details about how to remove or delete a file in Ubuntu. It is important to note that you need certain writable permissions to delete a file in the Ubuntu Linux system.
Using the following two different methods, you can delete or remove a file in the Ubuntu Linux system:
Important Note: You must be extra careful before deleting a file from your Ubuntu system. Once the file is deleted from the system, it would be hard to recover its content.
Using this method, you can remove a file using the command line application ‘Terminal’. Two Linux commands are available to delete or remove a file in Ubuntu distribution. First is the ‘Unlink’ command but, you can delete only a single file using this command.
The other most popular is the ‘rm’ command shortly used for “remove”, which is used to delete a file in the Ubuntu Linux system. Using the ‘rm’ command, you can delete multiple files at once from your Ubuntu system.
To delete or remove a file using the unlink command, type the unlink command followed by the name of the file which you want to delete. The syntax of the unlink command is mentioned below:
For example, we want to remove a text file with the name ‘demo.txt’ from the Ubuntu system. Using the above ‘unlink’ command, you can easily delete this file from your system as follows:
Using the rm command, you can delete single or multiple files in the Ubuntu system. To delete a file using the ‘rm’ command, use the following syntax:
For example, we want to remove a text file from the system ‘Downloads’. To do this task, use the rm command followed by the file path as follows:
If you delete a write-protected file from the Ubuntu system, the prompt displays on the terminal screen for confirmation, enter ‘y’ and then press the ‘Enter’ key. Otherwise, the file will be deleted from your system without displaying any confirmation prompt.
You can also delete multiple files from the Ubuntu system using the ‘rm’ command. To remove multiple files, you must have certain write permissions on the files. To remove or delete multiple files from your system, use the ‘rm’ command and specify the file names which will be separated by a space as follows:
You can also remove the multiple match file in the current directory using the regular expansions or wildcard(*).
For example, to delete all ‘.txt’ files from the current working directory. Execute the rm command by using the wildcard with the .txt extension as follows:
The following options can use along with the rm command:
Options | Description |
---|---|
-f | The ‘-f’ option deletes all read-only files immediately from your system without displaying any confirmation prompt. |
-i | The ‘-i’ option prompts a user for confirmation before deleting or removing a file. |
-v | The ‘-v’ option displays the file names on the terminal screen as the file is being processed or deleted from the system. |
-I | The ‘-I’ options show user prompts whenever the user attempts to delete three files at a time. This option also can be used when you are deleting files recursively. |
-R or -r | The both options ‘-R’ and ‘-r’ delete the current directory along with all subdirectories and files. |
-q | The ‘-q’ option suppresses all warnings. However, the error messages are still displayed on the screen. |
We discussed a few examples here related to ‘rm’ command options, which are given below:
Type the ‘rm’ command followed by the option ‘-i’ that each time confirms before deleting a file:
If you delete a file without displaying a user confirmation prompt, even if the files are write-protected then, use the ‘rm’ command followed by the option ‘-f’.
You can also combine rm command options for deleting a file. For example, to delete all matched files from the current directory without prompt a verbose. Use the ‘rm’ command and also mention the ‘-fv’ option as follows:
Important Note:
Never execute the ‘sudo rm -R /’ or ‘sudo rm -r /’ commands as root or normal user on your system. These commands are harmful to your data because these commands delete or remove all root directory files and will also delete all mounted volumes. Don’t even try to run the ‘sudo rm -rf /*’ command on your system. It does blunders with your system.
To delete a file using the GUI, you should have writable permissions on the file and you must be logged in as a root user on your Ubuntu system. By following few simple steps, you can easily delete a file or multiple files from the Ubuntu system:
Go into the Ubuntu file system and right-click on a file that you want to delete from your system.
To delete the selected file, choose ‘move to trash’ from the displayed dropdown list.
To permanently remove from the ‘Trash’ folder, right-click on the Trash and then choose the ‘empty trash’.
We learned how to delete files in the Ubuntu Linux system. Moreover, we have also seen some files we cannot delete using GUI but these type of files you can delete via the command line by executing commands as a root user.
Welcome to the Ubuntu Weekly Newsletter, Issue 873 for the week of December 29, 2024…
Have WiFi troubles on your Ubuntu 24.04 system? Don’t worry, you’re not alone. WiFi problems…
The following is a post from Mark Shuttleworth on the Ubuntu Discourse instance. For more…
I don’t like my prompt, i want to change it. it has my username and…
Introduction: A Fragile Trust The Ruby ecosystem relies heavily on RubyGems.org as the central platform…
Asset management is the process of managing and maintaining a company’s assets to maximize their…