DEV Community

abbazs
abbazs

Posted on • Updated on

How to find all the empty files?

Here is how to find empty files in the current directory:

find . -maxdepth 1 -type f -empty 
Enter fullscreen mode Exit fullscreen mode

-type f --> filter only files
-empty --> filter only empty
-maxdepth --> how many levels of sub directories to search?


To find all empty files and folder

find . -empty
Enter fullscreen mode Exit fullscreen mode

To delete the empty files:

find . -maxdepth 1 -type f -empty -delete
Enter fullscreen mode Exit fullscreen mode

Use it carefully. It will delete all the empty files like __init__.py files also.

To delete files:

find . -maxdepth 1 -type f -name *.py -exec rm -rf '{}' '+'
Enter fullscreen mode Exit fullscreen mode

Use it carefully, it will delete all python files, even if they are not empty.

Top comments (0)