Over time the number of files on a Linux machine can become untidy and slow down your computer. It's not uncommon for directories in /user/bin or /bin that contain a single file or binaries that you no longer need. Removing these may make administration tasks easier or free up some disk space.
I know this job is fundamental for command-line lovers, but some of us newbies face problems in doing this task daily, and it becomes a real pain.
Quick Navigation to Remove/Delete Directory in Linux
Are you looking to learn how to delete directories in Linux? If you are, then this article is for you. I will walk you through every step to deleting directories in Linux and discuss some directory problems and solutions.
This article is meant to give you a place to visit and find the answer you've been looking for in the Linux directory commands. The command examples are not just listed but explained in detail. Have a look around. If you have any questions, let us know in the comment section.
Workable Solutions | Step-by-step Troubleshooting |
---|---|
Fix 1. Remove Directory with rm | Enter rm [options] [file or directory name]...Full steps |
Fix 2. Remove a Directory with rmdir | Enter rmdir [options] [directory name]...Full steps |
Fix 3. Delete a Directory with Find | Base on a given expression which applies the command...Full steps |
Fix 4. Removing All Empty Directories | Find / -dir -type d -empty -delete...Full steps |
Recover data with Data Recovery Software | Download EaseUS Data Recovery Wizard...Full steps |
In a few steps, you can remove/delete a directory in Linux. Below are the best ways we have explained using rm, rmdir, and find commands to help you remove or delete the directory in Linux.
Fix 1. Remove a Directory in Linux with rm Command
Step 1. Run the command below.
The rm command in Linux deletes files and directories. It uses the following syntax:
rm [options] [file or directory name]
Note: To eliminate more than one file or directories using the rm command, add multiple record or listing names, separated via blank spaces.
The unique rm command options consist of:
- -f: Forces the removal of all documents or directories.
- -i: activates for affirmation before removing.
- -I: prompts once before putting off greater than three documents or removing recursively.
- -r: removes directories and their content recursively.
- -d: eliminates empty directories.
- -v: presents a verbose output.
- --assist: presentations help textual content.
- --version: presentations the command model.
Important: Trying to use the rm command with no options to eliminate listing effects in a mistakes message: An error message will show up when you try to delete a directory using the rm command without alternatives.
Step 2. Deleting an empty guide using the rm command
In case you need to remove an empty listing, add the -d flag to the rm command:
rm -d example
If you want to remove a directory with a hyphen at the start, follow the below mentioned format:
rm -- [directory name] or rm ./[directory name] syntax
The below example indicates that the rm command with the -d flag eliminates the example directory:
Deleting a directory with subdirectories
Use the -r flag to delete a listing incorporating subdirectories and documents.
The picture below shows the tree hierarchy of the example directory, which incorporates Dir1 and Dir2 subdirectories, with multiple textual content files in each:
An example of a directory and report hierarchy using the -r flag eliminates the complete directory, consisting of subdirectories and files, at the same time as the -v flag lists each step of the procedure as the output:
rm -r -v instance
Recursively getting rid of a couple of directories, subdirectories, and files using the rm command
The -i choice shows a prompt asking you to confirm directory removal. Type Y and press enter to affirm.
rm -d -i example
Fix 2. Remove a Directory in Linux with the rmdir Command
The Linux rmdir command gets rid of empty directories. The command makes use of the below-mentioned syntax:
rmdir [options] [directory name]
The rmdir command consists of the following alternatives:
- --ignore-fail-on-non-empty: does not display an error message while looking to get rid of a non-empty directory.
- -p: eliminates the directory in conjunction with its parent within the hierarchy
- -v: provides verbose output.
- --help: shows textual content.
- --version: shows the command version.-
To eliminate those directories using the rmdir command, add them in reverse order of hierarchy. Using the -v alternative, It lists each step of the process as the output:
rmdir -v example/test example
Getting rid of multiple directories with the rmdir command
A less complicated method of doing this is to apply the -p choice with the subdirectory's name. It eliminates both the subdirectory and its hierarchical discern:
rmdir -p -v example/test
Getting rid of a subdirectory and its discern using the rmdir command
The rmdir command allows you to use wildcards to dispose of more than one directory with comparable names. For instance, if you need to get rid of directories named Example1, Example2, and Example3:
rmdir -v example*
Also read: How to recover deleted files from EXT4
Fix 3. Delete a Directory in Linux with Find Command
Find is a Linux command-line utility that helps to search for documents and directories. The search is primarily based on a given expression which then applies the command on every matched document or listing.
The maximum common state of affairs is the find command to delete directories primarily based on a pattern. As an instance, to delete all directories that cease with _cache in the current running listing, you will run:
find. -typed -name '*_cache' -exec rm -r {} +
let's examine the command above:
- /dir - recursively search in the current operating directory (.).
- -type d - restricts the hunt to directories.
- -name '*_cache' - search only directories that cease with _cache
- -exec - executes an outside command with non-obligatory arguments; in this situation, this is rm -r.
- {} + - appends the located files to the stop of the rm command.
Fix 4. Removing All Empty Directories
If you want to remove all empty directories in a directory tree, you can run the following command. In simple words, the command will remove everything inside the folder, including find files and subfolders.
find / -dir -type d -empty -delete
Here is a reason behind the options used:
- /dir : recursively search within the /dir directory.
- -type d : restricts the search to directories.
- -empty : restricts the search handiest to drain directories.
- -delete : deletes all discovered empty directories within the subtree.
- -delete : can delete empty directories.
Use the -delete option with a severe warning.
The find command line is evaluated as an expression. If you upload the -delete option first, the command will delete the entirety under the beginning factors you targeted. Constantly test the command first without the -delete option and use -delete as the final alternative.
Recover Deleted Directory in Linux with Data Recovery Software
To recover deleted directories in Linux using data recovery software is by far the easiest and most convenient way of recovering deleted directories. You can retrieve the deleted files in Linux using EaseUS Data Recovery Wizard; it is a one-stop solution to all your problems. The tool is easy to use and time-saving. It is compatible with all versions of Linux, including MX Linux, Manjaro, Linux Mint, Ubuntu, Debian, and more.
EaseUS Data Recovery Wizard can recover all deleted files, pictures, documents, and videos after deleting, formatting, partition loss, OS crash, virus attack, and other data loss scenarios. It covers various features like Deleted file recovery, Formatted Recovery, Raw Recovery, and Nas data recovery.
You can follow the steps below to complete the recovery:
Step 1. Install and Launch EaseUS Data Recovery Wizard on your Windows PC. Choose "Linux Recovery" on the left panel.
Step 2. This software will detect your IP and Port automatically. You need to input the user name and password. If the IP address or Port is not right, you can correct it manually. Then, click "Connect Now".
Step 3. After successfully connecting to your Linux device, EaseUS recovery software will start scanning the disk to find all lost and deleted files. When the scan process completes, click "Filter" > "Type" to specify lost file types.
Step 4. You can preview the scanned files first. Then, select the target data and click the "Recover" button to restore lost files in Linux.
The Bottom Line
With rm, rmdir Command and find commands; you may delete directories primarily based on unique standards speedily and efficiently. Deleting directories is a smooth and straightforward process. However, it would be best if you were careful not to delete vital records. In case you have lost, EaseUS Data Recovery Wizard, a data recovery tool, can assist you in recovering lost files.
Was This Page Helpful?
Brithny is a technology enthusiast, aiming to make readers' tech lives easy and enjoyable. She loves exploring new technologies and writing technical how-to tips. In her spare time, she loves sharing things about her game experience on Facebook or Twitter.
Approved by Evan Galasso
Evan Galasso is a digital forensics and data recovery engineer with over 10 years of experience in the field. He presents opinions on the current state of storage media, reverse engineering of storage systems and firmware, and electro-mechanical systems of SSDs and HDDs.
Related Articles
-
How to Use TestDisk to Recover Data on Windows/Mac
Dany/Dec 16, 2023
-
Windows Cannot Find Ms-Settings: How to Fix the File System Error
Dany/Nov 09, 2023
-
Undo DiskPart Clean: Recover Data/Partition after DiskPart Clean
Tracy King/Nov 08, 2023
-
Recover Deleted NetFlix Profile & History
Jean/Nov 08, 2023