Deleting files from your Linux/Ubuntu operating system is a basic task performed by every user on a daily basis. This deletion or removal of unnecessary files is performed to make extra space for the important files and to install some important tools of daily use. 

There may be a chance that a new user may feel the difficulty of using the Linux operating system and not perform the task of deleting the files. Therefore this article gives you an overview to perform the task of deletion in the Linux/Ubuntu environment.

How to Delete a File in Linux/Ubuntu Terminal?

In the Linux/Ubuntu operating system, most tasks are performed by the terminal. It is a robust tool used by developers to run their programming scripts. However,  this article is on the deletion of files by using the terminal. There are two basic ways to delete the files from the Linux/Ubuntu operating system.

  • Using the Terminal to delete the files
  • Using the GUI method to delete the files

Method 1: Using the Terminal to Delete the Files

There are two basic commands available in the Linux/Ubuntu terminal for the deletion and removal of files. 

  • unlink” command to delete the files
  • rm” command to delete the files

“unlink” Command to Delete the Files

The “unlink” command is used to delete single files from your Linux/Ubuntu operating system. To perform this task open the terminal by using the shortcut key “Ctrl+Shift+T” from your keyboard and execute the below given command.

unlink filename

As shown in the figure below, when I list the files in the “My_files” directory it contains five files. After performing the task of deletion using the “unlink” command the file with the name “Test.odt” has been deleted from the directory.

If we use the “unlink” command to delete multiple files then it sends back the error message of the “extra operand”. Therefore for the deletion of multiple files, we use the “rm” command. 

“rm” Command to Delete the Files

The “rm” command is used in the Linux/Ubuntu terminal to delete a single file, or multiple files or delete all the files in a directory at a time. To use the rm command in Ubuntu, first, open the terminal and execute the below-given command.

rm path/to/files

In the above command 

  •  “rm” is used to delete the file. 
  •  “path/to/file” is the path where your file is located.

The results of the above command are given in the below figure. In this result, we list the before and after deletion results. The result clearly depicted that the “rm” command has successfully removed the “Video.mp4” file from the “My_files” directory.

Note: For the protected files your terminal shows the confirmation messages for the deletion of files, by pressing “y” your files will be deleted, and if you do not want to delete them press “n”. 

Delete More than One File

To delete more than one file, use the rm command followed by the name of the files that need to be deleted. The command to be executed in the Linux/Ubuntu terminal is given as

rm file_1 file_2 file_3

In the above command “file_1”, “file_2” and “file_3” are three files that are going to be deleted. The below figure is the result of the above command. The result clearly shows that three files are removed from the “My_files” directory. 

Use Wildcard to remove the files

Now for the removal of files with the same extension such as “.txt”, “.mp4”, and “.odt” etc, you can execute the below-given command in the Linux/Ubuntu terminal.

rm *.odt

In the above command “*” wildcard allows the “rm” command to delete all the files with “.odt” extension in the current directory. The below figure is the output result of the above command, by executing the command the files with “*.odt” extension will be deleted from the directory.   

Delete the files with the same name

Now for the deletion of the files with the same name in a directory execute the below given command in the Linux/Ubuntu terminal.

rm *filename*

In the above command “rm” command finds all the files with the name mentioned in between * * and deletes them. The “filename” is the name of the file that is going to be deleted. The below-given results show that there are three files with the name “Test”. By executing the above command all three files will be deleted. When we listed the directory after deletion it shows only one file.   

Use “rm” Command with Different Flags

Now before the removal of any files you want to show the confirmation of deletion, then use the “-i” flag with the “rm” command. The command to be executed for this purpose is given as.

rm -i filename

In the above command “-i” with the “rm” command gives the output a message for the confirmation of deletion. The below results show that when executing the above command to delete a file a message will appear that asks for confirmation of deletion, to delete it type “y” and press enter if you do not want to delete it then type “n” from the keyboard and press enter.

Now use the “-f” flag with “rm” command to delete the protected file without asking for the confirmation of deletion. The command to be executed for this purpose is given as.

rm -f filename

In the above command “-f” forces the “rm” command to directly delete the protected file without asking for confirmation of the deletion. The below results show that when executing the above command for the deletion of a protected file it directly deletes it without asking for the confirmation of deletion.

Now use the “-v” flag with the “rm” command to delete the files. The command to be executed is given as. 

rm -v filename

In the above command the use of “-v” with the “rm” command is to print the name of the deleted file. The result of the above command is given below. 

Now use the “-fv” flag with the “rm” command to delete the file from your Linux/Operating system. The command to be executed for this purpose is given as.

rm -fv filename

By using the “-fv” flag with the “rm” command the results show that it prints the name of the protected file that you are deleting. The below result is obtained by executing the above command. 

Now use the “-r” flag with the “rm” command to remove all the files and subdirectories from a directory without deleting it. The command to be executed for this purpose is given as.

rm -r files/to/*

In the above command 

  • -r” is used to delete all the files and sub-directories in the current directory.
  • files/to/*” is the path to the files in the directory.

 The below result is obtained by executing the above command. The result clearly shows that the use of “-r” deletes all the files and subdirectories in the “Files” directory.  

Now use the “-d” flag with the “rm” command to remove the directory. The command to be executed is given as.

rm -d directory name

In the above command

  •  “-d” with the “rm” command used to delete the directory. 
  • directory name” is the name of the directory that is going to be deleted. 

The result of the above command is given below. The use of “-d” with the “rm” command is to perform the function to delete the empty directory.

Now use the “-rf” flag with the “rm” command to delete the non-empty files. The command to be executed for this purpose is given as.

rm -rf directory name

In the above command “-rf” check for the non-empty directories and delete them. The result of the above command is given in the below figure. There are four files in the “Data1” directory. This result clearly shows that the “-rf” with “rm” delete the non-empty directory.

Method 2: Using the GUI Method to Delete the Files

This is another way to delete the files from your Linux/Ubuntu operating system. In this method, you must have the writable permission on your files. The step to delete the files from the Linux/Ubuntu operating system by using the GUI method is given as.

Step 1: Firstly, you navigate the files or directory you want to delete from your operating system.

Step 2: Now select the file you want to remove or delete and right-click on it. From the dropdown option click on “Move to Trash”. In this way, your file will move to the trash folder.

Step 3: To completely remove the file, clear your trash folder, for this, you right-click on your trash folder, and from the dropdown box options click on “Empty Trash”.

Conclusion

To delete or remove the files or directories from your Linux/Ubuntu operating system there are two methods available such as Using the Terminal to delete the files and Using the GUI method to delete the files. In the first method, there are two main commands “unlink” and “rm”. “Unlink” is used to delete only one file at a time and the use of the  “rm” command is very vast. This command is used to delete multiple files at a time. You can also use this command with different flags to delete the files and directories in a manageable way.