Deleting File and Directory in Ubuntu Linux via 5 Commands

General Linux Topic

July 1, 2023 by Liosa F

Deleting File and Directory in linux

Optimal management of Linux requires familiarity with comprehensive Linux commands. Deleting useless files and directories is one of the users’ daily tasks to free up storage space and organize files. Deleting unnecessary files is a routine and simple operation in other operating systems, such as Windows, but Linux beginners may face challenges in deleting files and directories. It’s time to turn this challenge into a simple task by learning about different Linux commands to delete files and directories. This article will introduce 5 Linux commands to delete unused files and directories.

Different methods to delete file and directory in Linux

Linux supports the graphical user interface and the command line, freeing the user to choose the appropriate platform to apply settings. Although some users prefer GUI for Linux settings and configuration, Linux terminal offers more flexibility and options in settings. If you get familiar with Linux commands, you will never be separated from the Linux terminal. If you want to play a greater role and have more control over the operating system’s settings, the Linux terminal is the ideal platform for various purposes. That’s why programmers and developers prefer Linux Terminal to Windows and buy Linux VPS to get better performance from their operating system.

The commands we will introduce in this article to delete files in Linux are used in all Linux distributions and have the same function. Also note that the files that you will delete using the commands of this article will not be found in the Trash (unlike the Linux GUI where the deleted files can be recovered from the Trash, It isn’t easy to recover files that are deleted with Linux commands).

Do not worry that if you mistakenly delete files and directories using Linux commands, your information will be lost forever because the reference of deleted files through the terminal will be removed from the file system but not immediately and permanently removed from the storage device. Linux has a solution for this problem, and you can recover deleted files using different commands; We already taught you how to recover deleted files in previous articles.

There are many ways to delete files in Linux; in the following, you will learn the most common Linux commands, including rm, shred, unlink, find, and trash-cli, to delete files and directories.

Usingrmcommand to delete file in Linux

rm(remove) is one of the most dangerous Linux commands and the most common way to delete files and directories in Linux. Before teaching how to use the rm command, I recommend using it carefully because it permanently deletes files and directories, and it is not easy to recover the removed files. The rm command is versatile and is used to delete files and directories in Linux. The main syntax of the rm command is as follows:

rm <options> <filename_or_directory>

The rm command supports many options to modify or extend its behavior for various purposes, including the following:

  • -i (interactive deletion): This option is used to receive a confirmation prompt before deleting the file so that the user confirms the deletion of the file.
  • -f (forced deletion): all files and directories are deleted using this flag without displaying a confirmation prompt.
  • -v (verbose mode): This flag displays an explanation of the process of deleting files and mentions the names of the files currently being deleted.
  • -r (recursive deletion): It is used to delete the current directory and the contents of the directory (files and subdirectories).
  • -d: This flag removes empty directories.
  • -q: used for suppressing all warnings.

Note: displaying an error message after executing the rm command indicates a problem; otherwise, if you do not receive the output after running the rm command, do not worry about the problem because the output is usually not displayed after the successful execution.

How to use the rm command and its options to delete file and directory

In the following, we discuss how to use the rm command for different purposes.

Deleting a single file using the rm command

The rm command helps you to delete multiple files or a single file in Linux. To delete a single file using the rm command, run the following command:

rm <path_to_the_file>

For example:

rm file1.txt

You don’t need to type the file path to delete the file in your current directory; you can delete the desired file in the current directory by typing the file name.

Note: To delete a write-protected file, before deleting the file, you will be asked to delete the file, to which you must type “y” and then press “Enter.”

Deleting multiple files using the rm command

The rm command allows the user to delete multiple files located in different directories by specifying the path of the files. Also, if files are in the current directory, you can delete them by simultaneously specifying the files’ names through the rm command. For this, it is necessary to have specific write permissions. To delete multiple files using the rm command, use the following basic syntax:

rm <path_to_the_file_1> <path_to_the_file_2> <path_to_the_file_3>

For example:

rm  file1.txt file2.txt  file3.txt

Deleting the file using the rm and wildcards

Wildcards are special characters that delete files and directories by recognizing a specific naming pattern. Wildcards make the delete function easier for Linux users by automatically matching file and directory names. First, we will introduce you to the wildcards options, then teach you how to use the rm command along with wildcards with examples. Types of wildcards are:

* characterWith this option, any number of files that match a specific string will be deleted. Wildcards here means matching any type of character at any position in the file name. For example, the rm file*.txt command deletes any files which names start with "file" and end with ".txt." (For example, files "file1.txt", "file2.txt", and " file3.txt" are deleted from the directory.)
? characterthe "?" character is used to match the single character in the file name. For example, suppose you want to delete files which names end in" .txt" and have three other arbitrary letters; you must execute the rm ?.???.txt command. Also, by running the rm fi?e.txt command, the files that start with "fi" and end with "e", and there is a desired character between them, are deleted, and "?" will match any character between "fi" and "e".
[] characterused to match the specified string in the brackets. For example, to delete the files "file1.txt", "file2.txt", and "file3.txt", you can execute the rm file[1-3].txt command. By executing this command, files which names start with the file and then include numbers (between 1 and 3) will be deleted, so Wildcard, in this case, means matching any number between 1 and 3.

To use wildcard in combination with rm command, the basic syntax is as follows:

rm <wildcard>.<extension>

Note: To ensure the correctness of the file name when deleting files and to prevent the wrong deletion of important files, we recommend executing the ls command with wildcards before the rm command with wildcards. For example, before running the rm*.txt command to delete the files that match wildcards, execute the ls *.txt command and check the correctness of the files’ names that match wildcards.

Removing the file by displaying the prompt using the rm command

If you don’t have enough permission, the rm command to delete write-protected files will display a prompt to confirm the deletion of the file; this possibility is not available to delete other files. If you want to receive a prompt to confirm file deletion before deleting the desired file, you must execute the following command:

rm -i <path_to_the_file>

Deleting the file without displaying the prompt using the rm command

If you do not want to receive a prompt when deleting all files, even write-protected files in Linux, and you will be annoyed by receiving the confirmation prompt for deletion, the following command is helpful for you:

rm -f <path_to_the_file>

In order not to get the “Permission Denied” error after running the previous command, you can run the previous command using root/sudo permission:

sudo rm -f <path_to_the_file>

When using the rm command to delete files, you can use the rm command options to customize the process of deleting files and directories.

Note: Avoid running ‘sudo rm -R /‘ or ‘sudo rm -r /‘ commands with root privileges, as they will irreversibly delete all root files and directories.

Removing a directory using the rm/rmdir command

The rm command is also helpful in removing directories. You can use rm and rmdir commands to delete a directory. To delete a directory using the rmdir command, first, navigate to the directory you want to delete with thecdcommand (for example,cd ~/my_directory).

Before deleting the directory, it is better to check the content of the directory you want with thelscommand and make sure that the directory you want to delete is correct:


Then enter the following command to delete the directory you want:

rmdir < directory name>

You can also use the following command to delete the directory and its contents:

rm -r < directory name>

After executing the rm -r command, you must answer “y” to the confirmation request to delete the directory and press Enter.

To make sure that the directory you want is deleted, the ls command is used:

ls ~/

The contents of the parent directory will be displayed by running the previous command. If you do not find the directory you want in the output, you can make sure to delete the directory you want.

2. Using theshredcommand to delete file in Linux

Using different Linux commands to delete files makes it possible that the file content is not permanently deleted from the storage device, and you can access the file content in the system memory again using various deleted data recovery tools. Different commands to delete the file, such as rm, are assigned to the memory block, and the content of the deleted file is still in the system memory. If you intend to permanently delete a file that cannot be recovered even by the most advanced file recovery tools, the shred command is the ideal command to delete files in Linux permanently.

Another feature of the shred command is that it allows the user to overwrite the file’s contents before deleting it. To delete the file after overwriting several times, run the following command:

shred -u filename

The main syntax of the shred command for permanent and irreversible deletion of Linux files is as follows:

shred -uz <file_name>

3. Using theunlinkcommand to remove a file in Linux

Another command that you can use to remove files is the unlink command. The unlink command may not be able to support different purposes in deleting files like rm because it does not have additional options to modify its behavior. To delete Linux files using the unlink command, run the following command:

unlink <file_name>

If you don’t get output by executing the unlink command, ensure the file deletion operation is successful.

4. Using thetrash-clicommand to delete file in Linux

If you are looking for a way to access and restore the file after deletion, we suggest using the trash-cli command. Because the trash-cli command keeps the deleted file in the trash folder, therefore, the trash-cli command can be introduced as the least risky and safest way to delete files. To use the trash-cli command to delete files, first, install it through the following command:

sudo apt-get install trash-cli

Then use the following syntax to delete files in Linux:

trash <file_name>

5. Using thefindcommand to remove a file in Linux

 The find command is helpful in finding the location of the file to delete, which is combined with the rm command. As a result, the desired file is searched using the find command, and after it is found, it is deleted by the rm command. To delete a file in Linux through the find command, run the following command :

find . -name "<filename>" -exec rm {} \;

For example:

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


Unlink, unlikerm, does not support additional options to extend its behavior and does not allow the user to delete multiple files simultaneously.

To permanently delete a file in Linux, select the files you want to delete and press the "Shift + Delete" keys together from the keyboard. Another method is by right-clicking on Trash and clicking on the 'empty trash' option from the drop-down menu, you can permanently delete files and free up storage space for use.

Deleting files in Linux requires "write permission" so that you have enough privileges to change the file. This error is because you deleted files in Linux without "write permission."


Although deleting files in Linux seems simple, it is challenging for some users, especially beginners. In this article, we have taught five common ways to delete files and directories in Linux so that you can reach your goal with any method that is more convenient for you. The rm command is the easiest and most widely used method to delete files and directories, but before using various Linux tools to delete files, make sure that the file and directory names are correct and check the contents of the files and directories you want so that you don’t accidentally delete your important files permanently.

Also, before deleting some files, it is necessary to have root privileges, otherwise, you will not be able to delete the file. You should avoid running the commandssudo rm -rf /*andsudo rm -rbecause it will have unpleasant consequences.

Thank you for staying with us until the end of this article. If you know another way to delete files in Linux, share it with other users and us in the comments section.



Rate this docs


​ ​ ​ ​

About the Author Liosa F

Love, life, peace and technology are all for the wise man

Leave a Reply

Your email address will not be published. Required fields are marked *