0

I want to find out all scripts with a specific shebang line. Specifically, I want all files that match the following criteria:

  • It's mostly a plain text file (stuffs created by gzexe don't look very friendly)
  • The 1st line contains solely #!/bin/sh or #! /bin/sh (with a space)

I would like to do this with find, sed and grep (file available).
File names are useless, because some scripts don't have extensions or even have wrong extensions. Also a something.sh may have a shebang line of #!/bin/bash which is also not what I wanted.

Besides, sometimes I would come across a file like this:


#!/bin/sh
blah.blah.blah...

The 1st line is empty and the shebang is located at the 2nd line, which is not what I wanted.
I am able to find shebang lines with find|grep but I don't know how to find lines specifically on the 1st line of a file.
Thanks for any help in advance.

  • 1
    So do you need to match the #! on the second line, or only the first? – Stephen Rauch Apr 2 '17 at 5:00
1

If you have GNU grep

grep -rIzl '^#![[:blank:]]*/bin/sh' ./
0

If you don't care about which line the #!/bin/sh occurs in a file, then you can try:

find -type f -exec bash -c 'grep -r "^#!.*\/bin\/sh" $1 1> /dev/null && echo $1' _ {} \;

0
for f in ./*; do
   [ -f "$f" ] && [ -x "$f" ] &&
      file -b "$f" | grep -wq text &&
         head -n 1 "$f" | grep -qP '^#!\s*/bin/sh' &&
            printf '%s\n' "$f"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.