How to Remove Files and Directories in Linux [5 Best Methods]

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

To remove files and directories in Linux, you can use these five methods:

  1. To remove an individual file, navigate to its location using cd, and delete it with rm myfile.txt.
  2. If you want to remove an empty directory, simply use rmdir mydirectory.
  3. To remove directories and all their contents, including files and subdirectories, use rm -r mydirectory after navigating to the desired location using the cd command in the Terminal.
  4. If you want to delete multiple files and directories simultaneously, navigate to their location and execute rm file1.txt file2.txt mydirectory.
  5. 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: 

  1. Right-click and click the Open in Terminal option from the context menu.
open terminal remove files and directories in linux
  1. Navigate to the location where the file is located using the cd command.
navigating to directory
  1. Delete the file using the rm command followed with the file name: 
rm filename.txt
  1. Next, run the ls command to confirm the deletion of the file from the current directory.
removing individual files

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: 

  1. Launch the Terminal and run the ls command to view the files:
ls
  1. Next, execute the rmdir command, followed by the directory name that you want to delete.
rmdir mydirectory
  1. 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 the ls command. Your final output should look like this:
removing empty directory

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:

  1. Launch the command prompt and navigate to the directory using the following command.
cd directoryname
  1. Execute the following command to remove directories and their content:
rm -r directoryname
  1. Adding the -r option with the rm command recursively removes the specified directory and its contents.
removing directory with 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: 

  1. Enter the command line interface and navigate to the location where the files and directories are located.
cd directoryname
  1. Run the following command to remove certain files or directories from your Linux system:
rm filename1.txt filename2.txt directoryname
  1. Once the command is executed, it will remove the specified files and directories from your Linux system.
removing multiple files

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: 

  1. Launch the command prompt by pressing Ctrl + Alt + T.
launch the command prompt shortcut
  1. Run the following command to delete files based on a certain pattern:
rm *.zip
  1. The wildcard character * matches any sequence of characters, while the .zip specifies the desired file extension. The rm command with the specified pattern will delete all the files that match that pattern in the current directory.
removing files with specified extensions

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 like rm to avoid unintentionally deleting unrelated files.
  • 🔑 Consider permissions: Verify file permissions with ls -l /path/to/files, use sudo 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:

    Frequently Asked Questions

    How can I permanently delete files and directories in Linux using tools?

    To permanently delete files in Linux, you can use the 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?

    Yes, you can remove files and directories in Linux from different locations simultaneously by specifying their names or paths in a single rm command separated by spaces. Simply navigate to the parent directory, execute the rm command, and provide the names or paths of the items you want to remove. For example, running 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?

    Once you use the 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.

    Why is it important to permanently delete files and directories?

    Permanent deletion of files and directories is crucial for maintaining data security and privacy. Simply deleting files using regular methods may not completely remove the data from the storage device. By securely deleting files and directories, you can prevent unauthorized access or recovery of sensitive information, ensuring that it is permanently and irreversibly removed from your system.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    How to Get the Absolute File Path in Linux [6 Easy Ways]

    Next Post

    12 Best Ways to Use Grep Command in Linux 

    Read next