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.
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 Deep Data Recovery...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.
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:
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
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:
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
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:
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:
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.
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 Deep Data Recovery; 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.
Deep Data Recovery 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:
Note: Linux Recovery is a new feature of Deep Data Recovery, included in the latest version. You can contact our online customer service to get the installation package.
Step 1. Install and Launch Deep Data Recovery 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, Qiling 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.
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, Deep Data Recovery, a data recovery tool, can assist you in recovering lost files.