How to find all empty files and folders in a specific directory including files which just look empty but are not?
Solution 1
From man find
-empty File is empty and is either a regular file or a directory.
So to find both empty files and directories it is sufficient to do
find ~/lists -empty
To indicate the type, you could use the %y
output format specifier
%y File's type (like in ls -l), U=unknown type (shouldn't happen)
e.g.
find ~/lists -empty -printf '%y %p\n'
or make use of an external program like ls
, which includes a --classify
option
-F, --classify
append indicator (one of */=>@|) to entries
i.e.
find ~/lists -empty -exec ls -Fd {} \;
If your definition of 'empty' is expanded to include files containing only whitespace characters, then it becomes more complicated - and more computationally intensive, since now you need to actually open at least any non-empty files and examine their contents. The most efficient way I can think of off the top of my head would be something like
find ~/list \( -empty -o \( -type f -a ! -exec grep -qm1 '[^[:blank:]]' {} \; \) \) -exec ls -Fd {} \;
(either empty, OR a file AND grep does not detect at least one non-blank character). Likely there is a better way though.
Solution 2
From ~/list
folder:
find . -empty -type d
for listing empty directories and
find . -empty -type f
for listing empty files.
find . -type f -exec bash -c 'if [ `cat "{}" |wc -w` -eq 0 ]; then echo "file - {}";fi' \; -or -empty -exec bash -c "echo dir - {}" \;
for listing empty folders and files including whitespaces and empty lines
Related videos on Youtube
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
Let's say that in my folder
~/list
I have a large amount of folders each with their own sub-folders and sub-sub-folders etc, and there are lots of files almost on each level. However some of these files and folders are empty, so how can I recursively search through them all to find the empty files and folders? And then have them displayed in a list of file locations so that I know where each one is (it should also be clear which is a file and which is a folder because not all files have file extensions). I am running Ubuntu GNOME 15.10 with GNOME 3.18.Please note that it would also be very useful if it would also tell me if a file looked empty but wasn't (for instance if it had spaces or linebreaks in it or something). And would differentiate between a real empty file and one that just looks empty in the output.
-
Admin over 8 yearsAn empty file, is a file with no characters? (could be spaces, linebreaks and tabs)?
-
Admin over 8 years@JacobVlijm: Well, previously I was thinking just files which are 0 bytes, but now that you mention it, someone might have been crazy enough to give me a file with spaces or linebreaks or something, so I would like to detect those too, but I would like for the output to differentiate between an actual empty file, and one that just looks empty.
-
-
Admin over 8 yearsAnd will this recursively list sub-folders and their contents as well?
-
EdiD over 8 yearsthis will recursively show empty files and folders
-
Admin over 8 yearsSorry, I missed something out, please look at the edited version of my question. It shouldn't be too much to add.
-
Admin over 8 yearsSorry, I missed something out, please look at the edited version of my question. It shouldn't be too much to add.
-
kos over 8 years
[ `grep -c [[:alnum:]] "{}"` -eq 0 ]
will let through files containing only characters not in the[A-Za-z0-9]
set. -
EdiD over 8 years@kos - probably graph instead alnum will do the job
-
kos over 8 years
[:alnum:]
would let through files containing only characters not in the[A-Za-z0-9]
and[!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]
set. The only correct way to do that check would be checking whether every character is in the[:space:]
set, or checking whether a character not in the[:space:]
set is found like steeldriver did. -
Alexis Wilke about 4 yearsToo bad it doesn't work with softlinks. It probably uses the size of the softlink instead of the size of the destination and a softlink itself is never empty since it must have a path to another file (it's at least 1 character).