Question: How do I locate empty directories that doesn’t contain any files? Also, how do I find all empty files ( zero byte files ) in Linux?
Answer: You can use unix find command to get a list of all empty files and directories as explained below.
Also, refer to our earlier articles about unix find command examples – part 1 and find command examples – part 2.
Find empty directories in the current directory using find -empty
find . -type d -empty
Use the following command to remove all empty directories under the current directory.
find . -type d -empty -exec rmdir {} \;
Note: It is not recommended to remove empty directories from /etc/ or any other system directories.
Find empty files in the current directory using find -empty
find . -type f -empty
Note: Typically empty files are created by some programs as place holders, or as lock files, or as socket files for communication.
How many empty files are located under the current directory (and sub-directories)?
To count the number of empty files under current directory, pipe the find command to wc -l
find . -type f -empty | wc -l
How many non-empty files are located under the current directory (and sub-directories)?
find . -type f -not -empty | wc -l
Note: Find option -not reverts the option that follows it.
In all the above examples, replace the ( . ) dot with any other directory-path under which you would like to search the files.
Mommy, I found it! — 15 Practical Linux Find Command Examples tutorial explains find command’s maxdepth, mindepth, and invert match.
Comments on this entry are closed.
Very good way to delete it.
Why you don’t use the option “-delete” to delete the directories?
really nice..very useful
Hi folks,
Any one can tell me How to delete all the sudirectory text files from main directory? in UNIX.
I appreciate your valuable answers. Thanks
G.Ramesh
thanks for the trick 🙂
me love geekstuff :X
And how do you delete folders which have just empty folders in them
This is what I do for directories that are completely empty at the first level:
for x in `find . -maxdepth 1 -type d -empty`
do
/bin/rm -r -f $x
done
@Andy:
You could just use
find . -maxdepth 1 -type d -empty -exec rm {} \;
… or you use the option “-delete” as already mentioned above by Tester2.
How would you best handle the escaping of spaces in the paths?
Something along the lines of:
find . -type d -empty | sed -e ‘s/ /\\ /g’ | xargs rmdir
which doesn’t work for me on OS X…
@Chris:
The option you’re looking for is -print0, this will null-separate the output from find. Then you use -0 in xargs, like so:
find ./ -type d -empty -print0 | xargs -0 -I{} rmdir “{}”
Of course, using “find . -type d -empty -delete” avoids the problem completely.
Should probably use the -depth argument to delete to descend all the way down the directory tree before removing empty directories,
find . -depth -type d -empty -exec rmdir {} \;
@chris can you use single quotes on the find path spec ‘{}’, this works for me on bash under linux,
# find /tmp -depth -type d -empty -exec rmdir -v ‘{}’ \;
rmdir: removing directory, `./this is a test’
All,
How to delete NULL file from the multiple files/folders in single go.