Bash script - find dirs with non zero bytes content
how would I go about finding all dirs in my working dir that contain at least a non zero byte file using a Bash script ? This :
开发者_运维技巧find . -maxdepth 1 -type d -size +1c | sort
does not seem to work
How about this:
find . -maxdepth 2 -type f -size +1c -exec dirname {} \; | sort | uniq
This goes one level deeper, looking for nonempty files, then gets the parent directory of anything it finds, then removes duplicates.
Not sure if understanding fully. If I have ./lvl1/lvl2/file (file is non-empty) and lvl1 contains only empty files and the directory lvl2, should lvl1 appear in the output?
I assumed you want it to. Think this works:
find . -mindepth 2 -type f -size +0 | cut -d/ -f2 | uniq
find looks in all sub-directories of working directory to see if there is a non-empty file anywhere inside of it. cut so we only see the level name of interest. uniq since it is unlikely a directory only contains 1 non-empty file.
EDIT: biggest thing slowing it down is probably (didn't do any tests lol) that find continues to look in a directory after finding a file of size >0 (we should be able to stop looking at this point). can call find on each subdir and then have find exit when it sees the first match.
for DIR in `find . -mindepth 1 -maxdepth 1 -type d`; do
find "$DIR" -type f -size +0 -print -quit
done | cut -d/ -f2
can drop call to uniq here (since there will only be 1 result for each top level dir). I don't think doing anything to get rid of the cut will help much.
another thing is that you might want to change that this looks at regular files with it not looking at directories or something (it will skip over a bunch of stuff). er, instead of "-type f" think about using "! -type d"
about to fall asleep so it is entirely possible I missed something/did something stupid xD
I use this, converse grep because empty directories appear as 4.0K
du -h --max-depth=0 */ | grep -v 4.0K
Edit:
without max-depth
and using summary
du -sh */ | grep -v 4.0K
Note if you have dot directories you want to include make sure you have set
shopt -s dotglob
精彩评论