Ubuntu

How to Delete a File in Ubuntu

Removing or deleting a file in a Linux system is a fundamental task that every user wants to perform this action frequently. Deleting unnecessary files creates an extra space on your system that a user can utilize for installing a new package or can save new files.

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.

Methods to Remove or Delete Files in Ubuntu

Using the following two different methods, you can delete or remove a file in the Ubuntu Linux system:

  • Delete or remove a file in Ubuntu using Terminal
  • Delete a file using the Graphical user interface

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.

Method 1: Delete or Remove a File in Ubuntu Using Terminal

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.

How to Use the Unlink Command to Delete a File?

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:

$ unlink file-name

Example

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:

$ unlink demo.txt

Delete or Remove a File Using the rm Command

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:

$ rm path/to/file.txt

Example

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:

$ rm /home/sam-pc/Downloads/demo.txt

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.

Delete multiple files using the rm command

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:

$ rm testfile1.txt testfile2.txt testfile3.txt

Delete multiple similar files using the wildcard (*)

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:

$ rm *.txt

Options with rm Command

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.

How to Use rm Command Options?

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:

$ rm -i file-name

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’.

$ rm -f file-name

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:

$ rm -fv *.txt

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.

Method 2: Delete a File Using the Graphical User Interface

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’.

Conclusion

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.

About the author

Samreena Aslam

Samreena Aslam holds a master’s degree in Software Engineering. Currently, she's working as a Freelancer & Technical writer. She's a Linux enthusiast and has written various articles on Computer programming, different Linux flavors including Ubuntu, Debian, CentOS, and Mint.