Spring cleaning is not only necessary for homes, Your Linux Mint 20 file system also needs to be decluttered regularly. An organized file system will save you or the system administrator a lot of headaches in the long run.
In this article, you will learn how to remove your directories to organize your file system.
Pre-requisites
- Linux system, I'll use Linux Mint 20 here.
- Terminal access
- A user account with sudo privileges.
Note: All the methods used in this tutorial will remove the directory and its files permanently and can not be recovered, so make sure to back up the important files and directories before proceeding.
Find Your Current Working Directory
First of all, you will need your directory name/path to follow this tutorial. In case you do not know your current directory, run the following command in the terminal to find out your directory path.
pwd
List Down Directories/Files in Your Current Directory
List down all the directories and files in your current directory and verify the deleted directories after every with the following command.
ls
Remove Directories Using rmdir Command
To delete the directory, run the following command.
rmdir <directory full path>
Note: The rmdir command will only delete the empty directories. Otherwise, it will give the following error output.
Remove Directories Using rm Command
The rm command can delete any directory, unlike the rmdir command which only works for empty directories.
To remove the directories, use the following command syntax with the -r option.
rm -r <directory full path>
You can see in the above snippet that test1 is deleted and not listed in the directory.
To delete multiple directory at once, run the following command.
rm -r <directory full path> <directory full path> ...
If you want confirmation on deleting a directory, run the rm command with the āIā option.
rm -rI <directory full path>
Remove Directories Using find Command
If you are aiming at deleting multiple directories and do not know exactly where targeted files are at. Or simply do not want to list long commands with a long line of argument then you should use the find command to simultaneously search directories for target and delete them using one command.
For example, use the following command to delete all the directories with a certain name pattern.
find . -type d -name '<name pattern>' -exec rm -r {} +
Note: Remember to set your directory name pattern before running the above command.
Similarly, run the following command to delete all the empty subdirectories in a particular directory.
find <directory path> -type d -empty -delete
How to solve the permission denied error?
If any directory or file in the directory has restrictions like read-only etc, You will get the following outcome with a permission denied msg.
In that case, you simply need to run the respective command with a sudo/root privileged account.