8 Ways to Delete Files in Linux such as Ubuntu, Debian, Kali, etc

Unlike Windows systems, Linux distros are widely used with the command line instead of GUI, therefore one should know how to delete files using the terminal. However, before deleting a file or folder using the command line, you must be cautious because the system will delete them permanently instead of sending them to the recycle bin. This tutorial uses various methods and command-line techniques to confidently and efficiently delete files on a Linux system.

1. Using the rm Command:

The most commonly used command to delete files in the Linux system is ‘rm‘. It can be used to delete a single or multiple at once. The syntax to use this command line tool is:

rm [options] file1 file2 ...

For Example:

rm yourfile.txt

Options to use with it:

-i: Prompt before every removal.
-r or -R: Remove directories and their contents recursively.
-f: Forcefully remove files without confirmation.

Note: Be cautious with the -f option, as it will remove files without confirmation, and there is no way to undo the operation.

2. Deleting Files with Wildcards:

Inside a folder or directory, you would want to delete similar files but all together. In that case, we can use the wildcards that can delete a file with the same extension or pattern.

For example, we want to delete all the files in our current directory with the “.txt” extension. So, to do that, we can use the wildcard like given below:

rm *.txt

This command removes all files with the .txt extension in the current directory.

3. Removing Directories:

Instead of deleting a single file or one by one, we can delete the whole directory and its all content by using the ‘rm‘ command with the ‘-r‘ option.

Example:

rm -r directory_name

The above command will remove the directory named directory_name and all its contents.

Unlike the ‘rm‘ command, the ‘unlink‘ can be used to remove a single file only instead of multiple simultaneously. Although, this is functionally similar to ‘rm‘ if anyone doesn’t want to use that then can go for this one.

Example:

unlink myfile.txt

5. Deleting Files Interactively with Find:

If you want the system to find a file and delete that with the command you prefer such as rm then can use the “Find” with the -exec option for interactive file deletion.

Usage:

find /path/to/search -name "filename" -exec rm -f {} \;

Explanation of the components:

  • /path/to/search: Replace it with the path of the folder where you want to start the search.
  • -name "filename": Replace “filename” with the name or pattern of the files you want to delete.
  • -exec rm -f {} \;: This part executes the rm -f command on each file found. {} is a placeholder for the file name, and \; marks the end of the -exec command.

For Example:

We want our system to find all files with the .log extension in our current directory and delete them using the “rm”  command with the -i option to get interactively prompts before removing each file.

find . -name "*.log" -exec rm -i {} \;
Deleting Files Interactively with Find

6. Removing Files by File Type with find:

Use the find command to delete files of a specific type.

Example:

find . -type f -name "*.json" -delete

This command finds and deletes all files with the .json extension. Whereas, the -type option in the find command is used to specify the type of the file you are searching for. There are multiple type options are available here are those:

  • -type b: Block special file.
  • -type c: Character special file.
  • -type d: Directory.
  • -type p: Named pipe (FIFO).
  • -type f: Regular file.
  • -type l: Symbolic link.
  • -type s: Socket.
  • -type D: Door (created by some door-enabled systems).

7. gio command-line utility:

gio is a command-line utility in Linux and a part of the GNOME desktop environment. We can use it to access various functionalities related to the GNOME Virtual File System (GVFS) such as opening files and URLs, listing directory contents, mounting and unmounting volumes, querying file information, trash operations, and more… Apart from that, we can use Gio for copying, moving, and deleting files.

gio can perform file operations similar to cp, mv, and rm. As it provides a trash/recycle bin feature, for example (for GNOME):

gio trash myfile.txt

The above command sends the file to the trash, allowing for potential recovery. Whereas to use the gio for copying or moving, the syntax is:

gio copy source-file.txt destination-directory/
gio move source-file.txt destination-directory/

8. Emptying the Trash to remove files:

If you have used the gio command to delete the files, but as it moves the deleted content to the Trash/recycle bin, hence to empty and remove the all files in it, here is the command.

Example (for GNOME):

gio trash --empty

This permanently deletes all files in the trash.

Conclusion:

Understanding the commands available to delete files on Linux is quite important whether you prefer the simple ‘rm ‘ or the ‘Find‘. Because the command line method is quite powerful and can delete your files permanently without asking you twice. Hence, choose the command that suits you and always be cautious, especially when using commands that permanently delete files.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.