Linux how do I remove all empty directories?

http‮//:s‬www.lautturi.com
Linux how do I remove all empty directories?

To remove all empty directories in a given directory and its subdirectories on a Linux system, you can use the find command with the -empty option and the -delete action.

Here's the basic syntax for the find command:

find path -options action

To find and delete all empty directories under the current directory, you can use the following command:

find . -type d -empty -delete

This will search the current directory and its subdirectories for directories that are empty, and delete them.

You can also use the -depth option to search directories in a depth-first manner, which can be helpful if you want to delete empty directories before their parent directories.

find . -depth -type d -empty -delete

It's important to note that the find command does not remove directories that are not empty, even if the directories contain only empty directories. To delete a directory and all of its contents, you can use the rm command with the -r option to delete directories recursively.

rm -r path

It is generally a good idea to be cautious when using the find and rm commands, as it is easy to delete files or directories that you did not intend to delete. It is always a good idea to double-check the command and the directories that will be deleted before running the command.

Created Time:2017-10-30 10:17:40  Author:lautturi