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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 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.
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