​​How to Delete a File in Linux/Ubuntu Terminal?


Deleting files is a day-to-day task of a Linux user. A file is usually deleted from the system either when it is not required or to free up space. Linux/Ubuntu enables a user/system administrator to delete files via Terminal and Graphic User Interface (GUI). A file can be deleted by numerous commands via Terminal, like deleting a file based on specific criteria, permanently and securely deleting a file, etc.

This article will demonstrate the below-listed methods to delete a file via Linux/Ubuntu Terminal:

 

How to Delete a File in Linux/Ubuntu Terminal?

In Linux/Ubuntu systems, a file is deleted via unlink, rm, shred, wipe, and srm commands. Among these commands, the shred, wipe, and srm commands delete a file securely and permanently. Let’s practice all these commands one by one:

 

Method 1: Using the “unlink” Command

The unlink command enables users to delete a single file via the Linux/Ubuntu Terminal. The unlink command is pre-installed in Ubuntu/Linux. Here are its use cases:

 

Use Case 1: Delete a File in the Current Directory

To delete a file File1.txt in the current directory, execute the following command:

unlink File1.txt

 

Use Case 2: Delete a File From a Specific Directory

To delete a file File1.txt in a specific directory, e.g., the home directory, run the following command:

unlink /home/linuxgenie/File1.txt

 

Method 2: Using the “rm” Command

The rm (remove) command deletes files and directories via Linux/Ubuntu Terminal. The rm command does not delete the file(s) permanently from the file system, it just releases the index node (inode) occupied by the file. After all, it keeps the content in memory until another file overwrites that memory.

The rm command is pre-installed in Ubuntu and can be used in numerous ways:

 

Use Case 1: Delete a File in the Current Directory

To delete a file File1.txt in the current directory, execute the following command:

rm File1.txt

 

Use Case 2: Delete a File From a Specific Directory

To delete a file File1.txt from a specific directory, e.g., the home directory, use the path of the file with the rm command:

rm /home/linuxgenie/File1.txt

 

Use Case 3: Force Delete a File

Use the “rm” command with the “f” flag to forcefully remove the write protected file:

rm File1.txt
rm -f File2.txt

Two files File1.txt (using simple rm command), and File2.txt (using rm -f command) are forcefully deleted from the system.

 

Use Case 4: Delete a File With a Confirmation Prompt

By default, the rm command only prompts for write-protected files. However, with its “i” flag, you can get the prompt for any file type:

rm -i File1.txt

The File1.txt is only deleted if the user presses “Y” at the prompt.

Alternatively, use the “I” flag to force the rm command to get the confirmation prompt for three(3) or more files:

rm -I *.txt

The files File1.txt, File2.txt, File3.txt, File4.txt, and File5.txt are successfully deleted after the confirmation prompt.

 

Use Case 5: Delete a File With Verbose

The “rm” command does not write/put some text on the terminal by default. However, if it is used with the v (verbose) option, it shows the completion status as shown below:

rm -v File1.txt

The file File1.txt has been successfully deleted along with an explanation of what is being done by the rm command.

 

Use Case 6: Delete File With Spaces in File Name

A file sometimes be named with white spaces. The “rm” command tackles these white spaces either using the name with single/double quotes or using a backslash:

rm "Linux Genie.txt"
rm 'Linux Tutorials.txt'
rm Test\ File.txt

The files: “Linux Genie.txt”, “Linux Tutorials.txt”, and “Test File.txt” are deleted successfully.

More information about file names with white spaces can be seen in this post.

 

Use Case 7: Delete Multiple Files in the Current Directory

The following command deletes multiple files in the present working directory:

rm File1.txt File2.txt File3.txt File4.txt File5.txt

 

Use Case 8: Delete Files Using Wildcards

Wildcards are special characters in Linux/Ubuntu that are typically used to substitute a string to identify a particular naming pattern. Wildcards are popular in Linux/Ubuntu and can be used in various commands including the rm command as demonstrated in the following sections:

  • ? Character

The ? character matches a single character in file names. To delete files via the ‘?’ wildcard, run the following command:

rm -v File?.txt

  • [ ] Character

The [ ] character matches a single character or a set of characters mentioned within the square brackets ([ ]). To delete files via the ‘[ ]’ wildcard, run the following command:

rm File[123].txt

Where the wildcard character ‘[ ]’ will only match File1.txt, File2.txt, and File3.txt in the present working directory.

The files File1.txt, File2.txt, and File3.txt have been successfully deleted.

  • * Character

The * character matches a single character or a set of characters in file names. The command below deletes files via the ‘*’ wildcard:

rm File*.txt

Where the wildcard character ‘*’ in File*.txt will match all the ‘.txt’ files that start with ‘File’ in the present working directory.

 

Use Case 9: Delete Files by Extension Type

To delete files by a particular extension type in the current directory, execute the following command:

rm *.txt

Additionally, to delete multiple files of a particular extension, e.g., in the Directory1 in the home directory, run the following command:

rm /home/linuxgenie/Directory1/*.txt

 

 

Use Case 10: Delete Files by Name Pattern

The grep command is a powerful Linux/Ubuntu command used to filter out string patterns. The combination of the grep command and the rm command can be utilized to filter out files with a certain name pattern and delete those files. To delete files with name patterns, execute the following command:

rm -v *.txt | grep -i File

Use Case 11: Using the “find” With “rm” Commands

A combination of the find command with the rm commands extends the rm commands capabilities. The find command enables multiple possibilities to optimize the search and removal for files discussed below:

  • Delete Files in the File System by Name

The following command finds and deletes all the files of a particular naming pattern in the system, where {} is a place holder for the matching file name(s) in this case. The -exec executes the rm command on all the matching conditions and \ indicates the end of the -exec flag:

find -name "File*.txt" -exec rm {} \;

  • Delete Empty Files in the File System

The following command deletes all empty files in the file system, where type f searches for all files in the file system:

find -type f -empty -print -delete

The empty files File6.txt, File7.txt, and File8.txt have been successfully deleted.

  • Delete Files in the File System by File Permissions

The following command finds and deletes files in the file system by permissions, where the -prem is the permissions option while 640 indicates the read and writes privileges for the user and read privileges for the group:

find -name "*.txt" -perm 640 -delete

That two files, File2.txt and File3.txt, with -rw-r—– permissions, i.e., both files have read and write privileges for the user and read privileges for the group are deleted successfully.

 

Method 3: Using the “shred” Command

The shred command overwrites the files. It deletes a file when used with the ‘u’ option. This makes it extremely difficult to recover a file. The shred command is pre-installed in Ubuntu.

The following shred command uses the ‘n‘ to overwrite the file four (4) times. Next, overwrite the file with zeros using the ‘z‘ option and then delete the file File1.txt:

shred -zvu -n 4 File1.txt

 

Method 4: Using the “wipe” Command

The wipe command overwrites the files with random patterns several times before deleting the file. Thus, making it difficult for hackers and data thieves to recover the file.

The wipe command is not pre-installed in Ubuntu. To install the wipe tool, execute the following command:

sudo apt install wipe

To permanently and securely delete a file File1.txt via the “wipe” command, run the following command:

 

wipe -rfi File1.txt

Where:

  • r: it allows to remove/delete the complete directory tree.
  • f: force delete a file by disabling the confirmation query.
  • i: Enables the verbose mode.

 

Method 5: Using the “srm” Command

The srm command securely deletes the file after overwriting and renaming the file. The srm command is not pre-installed in Ubuntu. To install the “srm” tool, execute the following command:

sudo apt install secure-delete

The following command permanently and securely deletes a file File1.txt, where ‘z’ wipes the last file-write with zeros instead of random data using the “wipe” algorithm:

srm -vz File1.txt

Bonus Tip: How to Delete File in Linux/Ubuntu via GUI?

To permanently delete a file in Linux/Ubuntu via GUI, first select the file by using a keyboard or a mouse. Then select “Move to Trash” from the drop-down menu:

Next, launch the “Trash” by double-clicking the “Trash” icon:

The “Trash” directory contains all the files that are “Moved to Trash” by the user. Select the file to be deleted with the help of a keyboard or a mouse and select “Delete from Trash” from the drop-down menu:

There will be a prompt for confirmation. Press “Delete” to delete the file permanently:

Finally, the file is permanently deleted, which can be verified by checking the Trash directory as shown below:

Note: To delete multiple files, select multiple files and delete them as per the process shown above.

That’s all about deleting a file in Linux/Ubuntu systems.

Conclusion

A file can be deleted by multiple methods, for example via unlink, rm, shred, wipe, and srm (secure-delete) commands. The shred, wipe, and srm commands delete files securely and permanently by first overwriting the files before removing them. On the other hand, the files deleted by the unlink and rm commands stay in the memory till they are overwritten by other data. Additionally, the unlink command only deletes a single file while the rm command deletes multiple files. This article presented five (5) methods to delete a file in Linux/Ubuntu systems.

 

Print Friendly, PDF & Email