How to Find all the Empty Files in a Directory in Linux

This short tutorial expands on a previous guide about using the find command in Linux, this time we use it to locate “empty” (zero byte) files.

The other day I was doing some cleaning on my Linux server and I noticed that over time people had created and left empty files. So I decided to create a shell script that would run through the system and let me know how many files like this existed, along with their path.

Here’s a simple command that will allow you to run a similar search:

# find -L /home/stewiegriffin -maxdepth 1 -type f -size 0
/home/stewiegriffin/temp.log
/home/stewiegriffin/brainstorm_notes.txt
/home/stewiegriffin/mail.log
/home/stewiegriffin/niptuck.html

What the above shown command does is that it searches for files that have a size of 0. By default, the find command excludes symbolic files, so we use the -L option to include them. The option maxdepth tells the command to search only in the main directory and not go into sub directories. type -f tells the command to only look at regular files.

If you run the same command without the maxdepth 1 option it will search for empty files in all the directories inside of /home/stewiegriffin. You can also play with the options maxdepth and mindepth if you want to regulate the depth of the search. This is really very helpful if you have a lot of levels of directories and a lot of files to go through.

Of course, if you want you can also reverse this search and ask the command to look for all non-empty files. All you need to do is use the above command and add an exclamation mark in the size parameter:

# find -L /home/stewiegriffin -maxdepth 1 -type f ! -size 0
/home/stewiegriffin/apache.log
/home/stewiegriffin/how_to_log_your_actions.txt
/home/stewiegriffin/maillog.log
/home/stewiegriffin/niptuck01.html
/home/stewiegriffin/index.html
/home/stewiegriffin/test.php

Just be careful while running this. It may find many, empty files.

If you didn’t catch it when we first published it, be sure to check out our guide on using the find command in Linux.


If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for simplehelp.net


Home » Linux » How to Find all the Empty Files in a Directory in Linux

Leave a Comment

Your email address will not be published.