I'm writing a quick tool to inspect the contents of a node.js node_modules folder or python virtualenv for native dependencies. As a quick first approximation to this I wrote the following command.
find . | xargs file | awk '/C source/ {print $1} /ELF/ {print $1}'
I'm okay with false positives but not false negatives (e.g. files literally containing the string ELF or C source can be marked suspicious.), but this script also potentially breaks on long file names (because xargs will split them) and file names containing spaces (because awk will split on whitespace) and file names containing newlines (because find uses newlines to separate paths).
Is there a way to filter the paths generated by find by seeing if the output of file {} (possibly with some additional options to remove the path entirely from the output of file) matches a particular regular expression?
findand iterate F over that list, then you canecho "$F"whenever the output offile $F | grep ...returns success. – Prem Apr 06 '16 at 19:22findonly; usefilewith-bso as to usegrepwith anchor e.g.find . -type f -exec sh -c 'file -b "$0" | grep -q "^ELF\|^C source" && printf %s\\n "$0"' {} \;– don_crissti Apr 06 '16 at 19:25-exec fileis not enough, but-exec sh ...allows a whole small script to be executed for each file, withfinditself doing the iteration !! – Prem Apr 06 '16 at 19:32{}argument to the inner script become$0instead of$1in the body of the script?sh -c "echo $0" hi, for instance, prints/bin/bashon my machine. – Greg Nisbet Apr 06 '16 at 19:39find -exec file {} +... solution is still better than what I originally posted, but doesn't directly address the whitespace problem. What's the right thing to do in terms of packaging that as an answer? – Greg Nisbet Apr 06 '16 at 20:03findwill contain at least one slash will definitely help. But see my answer below; you can easily modify the-printflag in the first command to another-execoperator and thereby handle any special character filenames safely for whatever you need to do. – Wildcard Apr 06 '16 at 23:43