Delete Specific Directories Recursively in Ubuntu 20.04

Linux is a very powerful operating system, especially for rich command-line functionality. Command-line utilities are best for server administration and server management. In this tutorial, we will learn how to delete specific directories recursively in Linux, and the used machine is Ubuntu 20.04 LTS.

Linux Shell/Bash Terminal

The Linux terminal mainly based on shell/bash programming language – which is a powerful scripting language having all the basic to advanced programming functionalities.

That’s why Linux shell/terminal supports advanced logical commands beside all the system’s plain command. For example, we will now delete all the similar folders or files in a specified directory (including child dir) using bash/shell logic. Let’s see how we can do that.

Delete Specific Directories Recursively

To delete multiple directories with same name, run the following command:

$ sudo find /path/to/folder/. -name 'dirname' -exec rm -rf {} \;

In the above command, replace the directory path with your own path and specified the directory name you want to delete recursively with dirname.

  1. Notice the dot ( . ) after the folder path. This is important. Because it indicates to look all the sub-directories inside the specified directory.

Using the above approach we can also delete specific files recursively instead of folder. Just specify the filename you want to delete recursively then run the command. Boom! All the specified files will be deleted from all the sub-categories.

Posted in