How to locate scripts in the PATH

Simple shell script that will iterate through the directories in the PATH variable and print only files identified as scripts.

#!/bin/sh
# print every script in PATH

IFS=:
for directory in $PATH; do
  for filename in $directory/*; do
    # print more information
    # sample output: /usr/bin/discover-config: POSIX shell script, ASCII text executable, with very long lines
    # filetype=$(file -L $filename | sed  -ne "/.*:.*script.*/p");

    # print less information
    # sample output: /usr/bin/discover-config: POSIX shell script
    filetype=$(file -L $filename | sed  -ne "s/\(.*: .* script\).*/\1/p")

    if [ -n "$filetype" ]; then
      echo "$filetype"
    fi
  done
done

Sample output.

$ sh locate_scripts.sh
/usr/local/bin/bayes.rb: Ruby script
/usr/local/bin/jekyll: Ruby script
/usr/local/bin/kramdown: Ruby script
/usr/local/bin/listen: Ruby script
/usr/local/bin/posix-spawn-benchmark: Ruby script
[..]
/bin/znew: POSIX shell script
/usr/games/cmail: Perl script
/usr/games/espdiff: Bourne-Again shell script
/home/milosz/bin/atom.sh: POSIX shell script
/home/milosz/bin/jekyll.sh: POSIX shell script
You can also play with MIME media types to recognize file types.

Please read file manual page for more information.

About Milosz Galazka

Milosz is a Linux Foundation Certified Engineer working for a successful Polish company as a system administrator and a long time supporter of Free Software Foundation and Debian operating system.