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
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
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.