Typically, when wanting to delete multiple subdirectories within a large directory on Linux, you would have to use the rm command multiple times. However, there's a faster alternative to accomplish this task. While computer users have various methods to delete files and directories on their systems, such as using Ccleaner or creating .bat files to delete files and directories, on Linux, we can use the Command Prompt, also known as CMD, to delete directories or their subdirectories contained within them.
Effortlessly delete multiple subdirectories on Linux using CMD
For example, let's say we have a directory named htg containing 5 subdirectories. To delete 3 of them, typically we would use the rm command 3 times.
This process can be shortened by combining the 3 rm commands together.
To execute, type the following command line and press Enter (change the names of the directories to the ones you want to delete on your machine).
rm -r ~/Documents/htg/{done,ideas,notes}
Each word within the parentheses represents an extension, appended to the preceding part (/Documents/htg/).
The command above can be broken down into /Documents/htg/done, /Documents/htg/ideas, and /Documents/htg/notes, which are paths to the directories to be deleted.
After entering the command, all 3 of these subdirectories have been deleted (illustrated below)
The -r part is added to the rm command when wanting to delete a directory rather than just a file. If the command lacks the –r part, the system will report an error unable to delete the directory.
If all 3 subdirectories you want to delete are empty, you can use the rmdir command as follows:
rmdir ~/Documents/htg/{done,ideas,notes}
If not all subdirectories you want to delete are empty, the system will only delete empty directories and report an error cannot delete with non-empty directories.
Here, Mytour just guided how to delete multiple subdirectories using a single command on Linux. This saves you time instead of having to use the delete command multiple times for each directory.
Source: howtogeek
