Linux / Unix: Find and Delete All Empty Directories & Files

Linux / Unix: Find and Delete All Empty Directories & Files

To find and delete all empty directories and files on a Linux or Unix-like system, you can use the find command with the -empty and -delete options.

For example, to find and delete all empty directories in the current directory and its subdirectories, you can use the following command:

refer to‮‬:lautturi.com
find . -type d -empty -delete

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

To find and delete all empty files in the current directory and its subdirectories, you can use the following command:

find . -type f -empty -delete

This will search for empty files in the current directory and its subdirectories, and delete them.

To find and delete both empty directories and files in the current directory and its subdirectories, you can use the -o option to specify multiple search criteria. For example:

find . -type d -empty -o -type f -empty -delete

This will search for both empty directories and empty files in the current directory and its subdirectories, and delete them.

You can also use the -path option to specify a specific directory or pattern to search. For example, to find and delete all empty directories in the /home/user/documents directory, you can use the following command:

find /home/user/documents -type d -empty -delete

This will search for empty directories in the /home/user/documents directory, and delete them.

You can find more information about the find command and its options in the find documentation or by running the find --help command.

Note: Be careful when using the find command with the -delete option, as it can permanently delete files and directories from your system.

Created Time:2017-10-29 22:08:59  Author:lautturi