How to remove all directories inside a specific directory?

ยท ยท 228 words ยท 2 minute read

How to remove directories in Terminal ๐Ÿ”—

To remove all directories inside a specific directory, you can use the rm command with the -r (recursive) option. This option ensures that the command removes directories and their contents recursively. Here is the command you can use:

rm -r /path/to/your/directory/*/

This command will remove all directories and their contents inside /path/to/your/directory/.

Make sure that you include a / at the end to tell that you want to delete directories not files.

rm -r /path/to/your/directory/*/
                               ^
                             # this slash

If you want to skip the confirmation prompts for each deletion, you can add the -f (force) option:

rm -rf /path/to/your/directory/*/

This command will forcefully remove all directories and their contents inside /path/to/your/directory/ without asking for confirmation.

Be Cautious ๐Ÿ”—

Before deleting or removing any file or directory, try it with ls first to see which file/directory you are about to actually delete.

ls -A /path/to/your/directory/*/

All the directories shown as a result of the above command, will be deleted if you replace ls -A with rm -r. So you can be sure.

I hope this post helps you. If you know a person who can benefit from this information, send them a link of this post. If you want to get notified about new posts, follow me on YouTube , Twitter (x) , LinkedIn , Facebook , Telegram and GitHub .

Share: