Skip to content


How to find all the empty files in a directory

Linux

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 return many, many results.

Posted in Linux.

Get Simple Help tutorials just like this one in your email inbox every day - for free! Just enter your email address below:

 

You can always opt out of this email subscription at any time.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.