TL;DR
To remove files and directories in Linux, you can use these five methods:
- To remove an individual file, navigate to its location using
cd
, and delete it withrm myfile.txt
. - If you want to remove an empty directory, simply use
rmdir mydirectory
. - To remove directories and all their contents, including files and subdirectories, use
rm -r mydirectory
after navigating to the desired location using thecd
command in the Terminal. - If you want to delete multiple files and directories simultaneously, navigate to their location and execute
rm file1.txt file2.txt mydirectory
. - To delete files and directories based on patterns using wildcard characters, launch the Terminal and run
rm *.txt
to remove files and directories matching the pattern.
Explore the guide below to know the step-by-step process to efficiently remove files and directories in Linux, along with seven important precautions.
Removing files and directories in Linux is not just about tidying up your system—it’s also important for optimizing performance and freeing up storage. You can do this in the top five ways using the Linux CLI. But this process comes with its own set of risks. That’s why I’ll also highlight seven important precautions to ensure you avoid accidental deletions and lose your critical data.
⚠️ Caution: Before Deleting Files and Directories in Linux
Deleting files and directories in Linux can lead to permanent data loss or system instability if not done carefully. Follow these precautions to ensure safe deletion and prevent unintended consequences.
How to Remove Files and Directories in Linux
To remove files and directories in Linux, you can use the rm
command for individual files, rmdir
for empty directories, and rm -r
for directories and their contents. To delete multiple files or directories, simply specify their names after the rm
command. Apart from these commands, you can add wildcard characters to delete files and directories based on certain patterns.
Here’s the detailed guide for each of these methods:
1. Removing Individual Files
Removing individual files in Linux is a very common and simple task. It lets you delete only a specific file without affecting other files or directories on your Linux system. To remove a specific file, you can follow these steps:
- Right-click and click the Open in Terminal option from the context menu.

- Navigate to the location where the file is located using the
cd
command.

- Delete the file using the
rm
command followed with the file name:
rm filename.txt
- Next, run the
ls
command to confirm the deletion of the file from the current directory.

Remember to be careful while using the rm
command, as it permanently removes the file without any prompt for confirmation.
2. Removing Empty Directories
The purpose of removing empty directories in Linux is to clean up the file system by getting rid of directories that do not contain any files or subdirectories. It helps to maintain a well-organized directory structure by eliminating unnecessary empty directories. Removing empty directories can be done by using the rmdir
command. Here’s how you can use this command:
- Launch the Terminal and run the
ls
command to view the files:
ls
- Next, execute the
rmdir
command, followed by the directory name that you want to delete.
rmdir mydirectory
- The
rmdir
command removes only empty directories. That is, if a directory contains files or other directories, it will not be deleted. You can verify it again using thels
command. Your final output should look like this:

3. Removing Directories and Their Contents
To remove directories and all their contents, including files and subdirectories, you can use the rm
command with the -r
option. Follow these steps to use this method:
- Launch the command prompt and navigate to the directory using the following command.
cd directoryname
- Execute the following command to remove directories and their content:
rm -r directoryname
- Adding the
-r
option with therm
command recursively removes the specified directory and its contents.

4. Removing Multiple Files and Directories
This method lets you remove multiple files and directories in Linux with a single command. It saves time and effort when dealing with large-scale file and directory deletions. Here are the steps to follow to remove multiple files and directories simultaneously in Linux:
- Enter the command line interface and navigate to the location where the files and directories are located.
cd directoryname
- Run the following command to remove certain files or directories from your Linux system:
rm filename1.txt filename2.txt directoryname
- Once the command is executed, it will remove the specified files and directories from your Linux system.

5. Deleting Files and Directories Based on Patterns
Deleting files and directories based on patterns using wildcard characters in Linux serves the purpose of selectively removing specific types of files or directories that match a given pattern or file extension. To delete files and directories based on patterns using wildcard characters in Linux, follow these steps:
- Launch the command prompt by pressing Ctrl + Alt + T.

- Run the following command to delete files based on a certain pattern:
rm *.zip
- The wildcard character
*
matches any sequence of characters, while the .zip specifies the desired file extension. Therm
command with the specified pattern will delete all the files that match that pattern in the current directory.

7 Precautions When Removing Files and Directories in Linux
When removing files and directories in Linux, it’s important to take certain precautions and follow some best practices. This will let you ensure that the process is carried out accurately and safely. Here are seven precautions to keep in mind:
- 🔍 Verify items: Double-check file and directory names and paths using
ls
before deletion to ensure you’re targeting the correct items. - 💾 Backup important files: Always create backups of critical data using
cp -r /path/to/source /path/to/backup
to safeguard against accidental deletions. - ⚠️ Understand consequences: Deleting with
rm
is permanent and cannot be undone, ensure files or directories are no longer needed before proceeding. - ❗️ Be cautious with wildcards: Exercise care when using wildcards (
*
,?
) with commands likerm
to avoid unintentionally deleting unrelated files. - 🔑 Consider permissions: Verify file permissions with
ls -l /path/to/files
, usesudo
if necessary to ensure proper deletion rights. - ⚠️ Test in a safe environment: Before deleting, practice in a test directory to observe the outcome without risking important data.
- 💾 Backup system configuration: Prioritize backing up system files to prevent potential system issues arising from file or directory removal.
In a Nutshell
In this guide, I have covered methods to delete files and directories in Linux, like using rm
, rmdir
, and rm -r
. I have also discussed best practices such as verifying item names, creating backups, and testing in safe environments to prevent data loss.
For more insights, consider:
- Exploring how to run binary files in Linux, which helps you execute programs directly and manage applications efficiently.
- Learning different ways to concatenate files in Linux to streamline your file management and data organization.
- Understanding how to get the absolute file path in Linux, which will improve your navigation and scripting skills for better system management.
Frequently Asked Questions
How can I permanently delete files and directories in Linux using tools?
shred
command. It overwrites the file’s contents before removing it, ensuring the data is securely deleted. By using this command, you can significantly reduce the chances of recovering any information from the deleted file.Can I remove files and directories in Linux from different locations at once?
rm file1.txt /path/to/directory2 mydirectory3
will delete file1.txt
from the current directory, directory2 located at /path/to/
, and mydirectory3
from the current directory. However, ensure you target the correct files and directories for removal, as this operation is irreversible.Can I recover a file or directory after removing them with rm?
rm
command to delete a file or directory in Linux, it cannot be recovered. This is because this command is designed to remove data without any chance of retrieval permanently. When executed, the system immediately frees up the allocated space, resulting in the effective removal of the data. To prevent accidental data loss, it is crucial to back up your important files and directories regularly. In addition to that, you should double-check the items you intend to delete before using the rm
command. This will help avoid the unintended removal of critical data.