Ubuntu

How do you Delete a File in Ubuntu 22.04 Using Terminal?

How do you delete a file in terminal

In Ubuntu, files and directories can be deleted using either the Graphical User Interface (GUI) or the Command Line Interface (CLI). Using the CLI to delete files is more powerful as with CLI we can run additional scripts while deleting a file or directories.

This article explains how files can be deleted in Ubuntu using the Terminal.

How do you Delete a File in Ubuntu 22.04 using Terminal?

Files can be deleted in Ubuntu using different methodologies. Deleting a file or directory will remove the reference to that file or directory.

Files can be deleted in Ubuntu Terminal using the methods mentioned below:

  • Using “rm” Command
  • Using “shred” Command
  • Using “trash-cli” Command

Method 1: Use the “rm” Command to Delete a File

The “rm” stands for “remove”. It is used to remove a file or a directory in Ubuntu using the command:

rm <filename>

Use the file you want to delete in place of the <filename>. Here in the directory, there are three files:

To delete the file “named1.pdf”, the command will be:

rm named1.pdf

This will remove or delete the file from the directory:

Using “rm” to Delete Files With a Specific Name

Apart from providing the name of a file, the “rm” command can also delete files with specific names. For this purpose, utilize the following syntax:

rm *<filename>*

Having files in the directory with the same name can be deleted at once using the rm command. In the example illustrated below, we have three files “same1.txt”, “same2.txt” and “same3.txt” with “same” being the common name:

To delete all three use the command:

rm <strong>*same*</strong>

All three files will be deleted:

Using “rm” to Delete Files of Specific Type

With the “rm” command, a specific type of file can be deleted. The example below showcases three ODT files with the extension “.odt”:

To remove these files with the extension specified in the “rm” command:

rm *.odt

This will remove all the files having an extension “.odt”, as demonstrated below:

Method 2: Use the “shred” Command to Delete a File

The “shred” command is more powerful and useful as the files once deleted or removed cannot be recovered. “shred” overwrites the file’s content multiple times before removing it. This example below utilizes the “shred” command to remove the “named2.pdf” file:

shred -u named2.pdf

The “-u” will ensure to delete the file after rewriting it:

Once the shred command executes, it will overwrite and make two new files. When you will open the directory again these files will also be removed:

Method 3: Use the “trash-cli” Command to Delete a File

The “trash-cli” command will ensure the safe deletion of files by moving those files to the Trash first rather than deleting them permanently. “Trash-cli” packages are not included in Ubuntu and have to be installed manually using the command:

sudo apt-get install trash-cli

You can now delete a file using the “trash-cli” command. The example below deletes the file “named3.pdf” using the trash command:

trash named3.pdf

It will delete the file from the directory:

To verify if the file is moved to trash, open the Trash Application in Ubuntu:

In the Trash, you can see the file exists there, after removing it using the “trash” command:

Bonus Tip – Removing a Directory from Ubuntu

Using the “rm” command, a directory can also be deleted. To remove a directory from Ubuntu, use “rm” or “rmdir”. The “rmdir” command deletes an empty directory. The “rm” command deletes a directory and all the subdirectories as well as files.

Using “rm” to Delete a Directory

Consider the following directory “Folder” having files inside of it:

To remove the whole directory named “Folder” with all of its content. To do that, open the Terminal using “Ctrl+Alt+T”, and navigate to the Documents folder in the Terminal:

To remove the “Folder” directory use the command:

rm -r Folder

Here “-r” is for the recursive operation:

Using “rmdir” to Delete a Directory

The “rmdir” command deletes an empty directory. In the below-explained example, an Empty Folder named “EmpFolder” exists in the Document Directory:

To delete this empty folder, use the command:

rmdir EmpFolder

This will remove the folder from the directory:

Conclusion

To delete a file in Ubuntu 22.04 using Terminal, first, open the terminal, and use the “rm”, “shred” or “trash-cli” commands. The terminal offers more power to delete a file or a folder and the “rm” command is used mostly for deleting a file. This article discussed how we can remove specific files and folders through the Ubuntu terminal using different commands.

Similar Posts