Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

jvscode [[at]] fastmail [[dot]] fm

Find broken symbolic links


sw_vers -productName    #  Mac OS X

man ln | less -p '-s'


/usr/bin/sudo -H -i  # sudo shell



# find broken symlinks to non-existing files & directories

open http://www.thomas-guettler.de/vortraege/tipps/tipps-und-tricks.html#link_4.18

#/usr/bin/find / -type l ! -execdir test -e '{}' \; -print | nl

/usr/bin/find -x / -type l ! -execdir test -e '{}' \; -ls | nl

# skip /dev directory
/usr/bin/find / -not \( -type d -path '/dev' -prune \) -type l ! -execdir test -e '{}' \; -ls | nl



# find broken symlinks to non-existing files & directories or another broken symlink

open http://www.commandlinefu.com/commands/view/2369/find-broken-symlinks-and-delete-them

# "Using the -L flag follows symlinks, so the -type l test only returns true if the link can't be followed, 
#  or is a symlink to another broken symlink."

/usr/bin/find -x -L / -type l -ls | nl

/usr/bin/find -L / -not \( -type d -path '/dev' -prune \) -type l -ls | nl    # skip /dev directory



# delete broken symlinks

#/usr/bin/find -x / -type l ! -execdir test -e '{}' \; -delete
#/usr/bin/find / -not \( -type d -path '/dev' -prune \) -type l ! -execdir test -e '{}' \; -delete

#/usr/bin/find -x -L / -type l -delete
#/usr/bin/find -L / -not \( -type d -path '/dev' -prune \) -type l -delete

List mounted volumes on Mac OS X


# See also:
# localvols in
# http://codesnippets.joyent.com/posts/show/1888


/bin/df -l | /usr/bin/sed -E -e 1d -e 's/^([^ ]+[ ]+)[^\/]+//' | /usr/bin/sort -u
/bin/df -l | /usr/bin/sed -E -e 1d -e 's/^([^ ]+[ ]+){5}//' | /usr/bin/sort -u

/bin/df -a | /usr/bin/sed -E -e 1d -e 's/^([^ ]+[ ]+)[^\/]+//' | /usr/bin/sort -u


declare IFS=$'\n'
#for volume in $(/bin/df -l | /usr/bin/sed -E -e 1d -e 's/^([^ ]+[ ]+)[^\/]+//' | /usr/bin/sort -u); do 
for volume in $(/bin/df -a | /usr/bin/sed -E -e 1d -e 's/^([^ ]+[ ]+)[^\/]+//' | /usr/bin/sort -u); do 
   echo "vol: $volume"
done
declare IFS=$' \t\n'



# Cf. Get A List Of Mounted USB Drives
# http://www.adminselfhelp.com/?p=220

system_profiler SPUSBDataType | grep "BSD Name:" | awk '{gsub (" BSD Name: ","");print}'
diskutil info disk1
diskutil info disk1s3

system_profiler SPUSBDataType | sed -E -n 's/ *BSD Name: *([^ ]*)/\1/p'

declare IFS=$'\n'
for device in $(system_profiler SPUSBDataType | sed -E -n 's/ *BSD Name: *([^ ]*)/\1/p'); do
   for usbdevice in $(diskutil info "$device" | sed -E -n 's/ *Mount Point: *(.*)/\1/p' | sed '/^ *$/d'); do
      echo "USB device mounted at: ${usbdevice}"
   done
done
declare IFS=$' \t\n'

List users with local home directory


dscl . -list /Users NFSHomeDirectory | sed -E -n -e '/[[:space:]]\/Users\//s/^([^[:space:]]+).+$/\1/p'
dscl . -list /Users NFSHomeDirectory | awk -F '/' '/[[:space:]]\/Users\//{print $NF}'
dscl . -list /Users NFSHomeDirectory | awk '/[[:space:]]\/Users\//{print $1}'

List & delete Safari history

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari history
function lsh() {
   /usr/libexec/PlistBuddy -c Print ~/Library/Safari/History.plist | \
      /usr/bin/awk '/^[[:space:]]+= /{sub( /^[[:space:]]+=[[:space:]]+/,""); print}' | \
      sed -E -n -e 's/^(.{1,100}).*$/\1/' -e '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
      #/usr/bin/sort -u | /usr/bin/nl
   return 0
}

lsh


# delete Safari history except ...
# ... for those domains specified by an egrep regex

function dshe() {

   declare array_indices historyplistfile index regex

   regex="${1:-$'\777'}"   # delete entire history if there is no argument $1; for \777 see man ruby
   historyplistfile="${HOME}/Library/Safari/History.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${historyplistfile}" | /usr/bin/awk '/^[[:space:]]+= /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}'" "${historyplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :WebHistoryDates:'${index}':title" "${historyplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :WebHistoryDates:'${index}'" "${historyplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${historyplistfile}" &>/dev/null

   return 0

}


lsh
dshe unix
lsh
dshe
lsh

List & delete Safari cookies

# for PlistBuddy see: http://codesnippets.joyent.com/posts/show/1484

# list Safari cookies
function lsc() {
   /usr/libexec/PlistBuddy -c print ~/Library/Cookies/Cookies.plist | \
      /usr/bin/awk '/^[[:space:]]+Domain = /{sub( /^[[:space:]]+Domain =[[:space:]]+/,""); print}' | \
      /usr/bin/sort -u | /usr/bin/nl
      #/usr/bin/awk '!x[$0]++' | /usr/bin/sed -n '1!G;h;$p' | /usr/bin/nl
   return 0
}

lsc


# delete Safari cookies except ...
# ... for those domains specified by an egrep regex

/usr/libexec/PlistBuddy -c Print ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :0' ~/Library/Cookies/Cookies.plist
/usr/libexec/PlistBuddy -c 'Print :1' ~/Library/Cookies/Cookies.plist


function dsce() {

   declare array_indices cookiesplistfile index regex

   regex="${1:-$'\777'}"   # delete all cookies if there is no argument $1; for \777 see man ruby
   cookiesplistfile="${HOME}/Library/Cookies/Cookies.plist"

   array_indices="$(/usr/libexec/PlistBuddy -c print "${cookiesplistfile}" | /usr/bin/awk '/Domain = /{x++;print x-1,$0}' | \
       /usr/bin/egrep -iv "${regex}" | /usr/bin/awk '{print $1}' | /usr/bin/sort -rn)"

:<<-'COMMENT'
   # test
   for index in ${array_indices}; do 
      echo $index; 
      #/usr/libexec/PlistBuddy -c "Print :'${index}'" "${cookiesplistfile}" 
      /usr/libexec/PlistBuddy -c "Print :'${index}':Domain" "${cookiesplistfile}" 
   done
COMMENT

   for index in ${array_indices}; do 
      /usr/libexec/PlistBuddy -c "Delete :'${index}'" "${cookiesplistfile}" 
   done

   /usr/libexec/PlistBuddy -c save "${cookiesplistfile}" &>/dev/null

   return 0

}


lsc

dsce 'xxx.com|xxx.org'

lsc

List whois servers on Mac OS X

defaults domains | tr ',' '\n' | nl
defaults domains | tr ',' '\n' | grep -i network
defaults find network | egrep '^Found'
defaults find whois | egrep '^Found'
defaults find server | egrep '^Found'

defaults read com.apple.NetworkUtility
defaults read ~/Library/Preferences/com.apple.NetworkUtility NUWhoisSelectedServer
defaults read com.apple.NetworkUtility NUWhoisServers | awk -F '"' '/"/{print $2}' | nl

defaults read com.apple.NetworkUtility NUWhoisServers | awk -F '"' '/"/{print $2}' | xargs -I '{}' \
     bash -c 'ipaddr="$(/usr/bin/dig +short {})"; printf "%s\n" "${ipaddr//[[:cntrl:]]/, }"'

defaults read com.apple.NetworkUtility NUWhoisServers | awk -F '"' '/"/{print $2}' | xargs -I '{}' \
     bash -c 'ipaddr="$(/usr/bin/dig +short {})"; printf "%s  --  %s\n" "{}" "${ipaddr//[[:cntrl:]]/, }"'

List the names & version numbers of applications

# lsregister
/usr/bin/sudo /bin/ln -is $(/usr/bin/locate lsregister | /usr/bin/head -n 1) /bin/lsregister 


# first rebuild the Launch Services database
lsregister -kill -r -f -all system,local,user     # Mac OS X 10.5
lsregister -kill -r -f -domain local -domain system -domain user


# print the lines between :path ... version: ...
time -p lsregister -dump | sed -E -n -e '/^.+path: .+\.app$/{N;N;N;p;}' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/{N;N;N;p;}' | nl

time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/p' | nl

time -p lsregister -dump | egrep -A 3 -i "path: +\/.+\\.app$" | nl


# See: Sed - An Introduction and Tutorial: Grouping with { and },
# http://www.grymoire.com/Unix/Sed.html#uh-35

# list application paths with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +(.+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+name:.*/d; p;}'
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+name:.*/d; s/[[:space:]]+path:[[:space:]]+(.+)/\1/; p;}'


# list application names with lsregister
time -p lsregister -dump | sed -E -n -e '/path: .+\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version:/s/^.+path: +\/?.*\/([^\/]+\.app)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+path:.*/d; p;}'
time -p lsregister -dump | sed -E -n -e '/path: .+\.app/,/name:/{ /.+path:.*/d; s/[[:space:]]+name:[[:space:]]+(.+)/\1/; /^[[:space:]]*$/d; p;}'


# list version numbers of applications with lsregister
time -p lsregister -dump | sed -E -n -e '/path: +.+\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl
time -p lsregister -dump | sed -E -n -e '/\.app$/,/version/s/^.+version: +(.*)$/\1/p' | nl



# list the names & version numbers of applications based on lsregister

function lsvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\n' file; do

         path="${file% -*}"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"
         #mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
         #   /usr/bin/awk -F '"' 'END {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${path}   --   ${mdls_version}"
         else
            printf "%s\n" "${path}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   else

      while read -d $'\n' file; do

         path="${file% -*}"
         bname="$(/usr/bin/basename "${path}")"
         lsregister_version="${file##*- }"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${path}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\n" "${bname}   --   ${lsregister_version}"
         fi

done < <(
/bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n"
)

   fi


: <<-'COMMENT'

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 '^[[:space:]]+path:[[:space:]]+([^[:space:]].*\.app)$' | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives with sed pattern matching across several lines (here matching the lines containing: path: ... :version ...)

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/^.+path: +(\/.+\.app)$|^.+version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/path:.+\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/sed -E -n -e '/\.app$/,/version:/s/path: +(\/.+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT


return 0

}


lsvers | nl
lsvers | egrep -i system | nl
lsvers -fp | egrep -i system | nl     # print full paths to applications
lsvers -fp | egrep -i '\/[^\/]*system[^\/]*$' | nl     # print full paths to applications


# check
app="VerifiedDownloadAgent.app"
app="Crash Reporter.app"
app="SyncServer.app"
app="SecurityAgent.app"
app="SystemUIServer.app"

lsregister -dump | egrep -A 3 "${app}$"

path="$(lsregister -dump | grep -A 3 "${app}$" | sed -E -n -e 's/^[[:space:]]+path:[[:space:]]+(\/.*\.app)$/\1/p')"
echo "$path"
mdls -name kMDItemVersion "$path"


#-------------------------------------------


# list the names and version numbers of specified applications using lsregister
function appversion() {

/bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/([^\/]*${@}[^\/]*\\.app)$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s   --   %s\n"


: <<-'COMMENT'

set -- System
echo "${@}"

time -p /bin/lsregister -dump | /usr/bin/egrep -i -A 3 "^[[:space:]]+path:[[:space:]]+\/?.*\/[^\/]*${@}[^\/]*\\.app$" | \
/usr/bin/sed -E -n -e 's/^[[:space:]]+version:[[:space:]]+(.*)$/\1/p' -e 's/^[[:space:]]+path:[[:space:]]+\/.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

# alternatives
time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i " +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e 's/version: +(.*)$/\1/p' -e 's/path: +\/?.*\/([^\/]+\.app)$/\1/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl


# alternative with sed pattern matching across several lines
# here: matching the lines between: path: ... :version ...

time -p /bin/lsregister -dump | /usr/bin/egrep -A 3 -i "path: +\/?.*\/[^\/]*${@}[^\/]*\\.app$" | /usr/bin/sed -E -n \
-e '/\.app$/,/version:/s/path: +\/?.*\/([^\/]+\.app)$|version: +(.+)$/\1\2/p' | \
/usr/bin/sed 's/ /\\ /g' | /usr/bin/xargs -n 2 printf "%s - %s\n" | nl

COMMENT

return 0
}


appversion mail
appversion finder
appversion safari

appversion system
appversion uiserver
appversion server
appversion window


#----------------------------------------------------------------------


# list names & mdls version numbers of applications (based on mdfind & com.apple.application-bundle)
function lsmdvers() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 'kMDItemContentTypeTree == "com.apple.application-bundle"wc')

   fi

   return 0

}


lsmdvers | nl
lsmdvers -fp | nl    # print full paths to applications

lsmdvers | grep -i system | nl && echo && lsvers | grep -i system | nl



# list the names and version numbers of specified applications using mdfind & com.apple.application-bundle
function appmdversion() {

   if [[ "$1" == '-fp' ]]; then     # full path option

      while read -d $'\000' file; do

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${file}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${file}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${2:-*}*'wc")

   else

      while read -d $'\000' file; do

         bname="$(/usr/bin/basename "${file}")"

         mdls_version="$(/usr/bin/mdls -name kMDItemVersion "${file}" 2>/dev/null | \
            /usr/bin/awk -F '"' '/kMDItemVersion/ {print $2}' 2>/dev/null)"

         if [[ -n "$mdls_version" ]]; then
            printf "%s\n" "${bname}   --   ${mdls_version}"
         else
            printf "%s\x21\n" "${bname}   --   No mdls version number specified"
         fi

      done < <(/usr/bin/mdfind -0 "kMDItemContentTypeTree == 'com.apple.application-bundle'wc && kMDItemDisplayName == '*${@:-*}*'wc")

   fi

   return 0

}


appmdversion play | nl
appmdversion -fp play | nl   # print full paths to applications

appversion play && echo && appmdversion play


#----------------------------------------------------


# experimental

function cmdversion() {

declare cmd last_modified vers

while [ $# -gt 0 ]; do

   cmd="$(/usr/bin/which ${1})"   # get the full cmd path

   # cmd was not found in $PATH
   if [[ -n "$(echo "$cmd" | /usr/bin/egrep "^no +${1} +in ")" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi
   #if [[ -n "$(echo "$cmd" | /usr/bin/egrep '^no +[^[:space:]]+ +in ')" ]]; then echo; echo "${cmd}"; echo; shift; continue; fi


   # handle special cases such as ls, echo, ...
   if [[ "${1##*/}" == "ls" ]] || [[ "${1##*/}" == "echo" ]] || [[ "${1##*/}" == "getopt" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"
      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"
      shift
      continue
   fi

   if [[ "${1##*/}" == "python" ]]; then
      vers="$(${cmd} -V 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi

   # cases that require sudo
   if [[ "${1##*/}" == "fibreconfig" ]]; then
      vers="$(/usr/bin/sudo ${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"
      shift
      continue
   fi


   if [[ -n "$(${cmd} --version 2>/dev/null)" ]]; then
      vers="$(${cmd} --version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>/dev/null)" ]]; then
      vers="$(${cmd} -version)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} --version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} --version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(${cmd} -version 2>&1 | /usr/bin/egrep -ao '([Vv]ersion:? *"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)')" ]]; then
      vers="$(${cmd} -version 2>&1)"
      printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${vers}"

   elif [[ -n "$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}" 2>/dev/null) 2>/dev/null)" ]]; then
      vers="$(/usr/bin/egrep -ao '(.{0,10}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,15}|.{0,10}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,15})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '(.{0,30}[Vv]ersion:? +"?[\.\_[:digit:]]+"?.{0,30}|.{0,30}[Vv]\.? +"?[\.\_[:digit:]]+"?.{0,30})' "${cmd}")"
      #vers="$(/usr/bin/egrep -ao '([Vv]ersion:? +"?[\.\_[:digit:]]+"?|[Vv]\.? +"?[\.\_[:digit:]]+"?)' "${cmd}")"

      last_modified="$(/usr/bin/stat -f $'last modified:   %Sm\n' "${cmd}")"

      if [[ -z "${vers}" ]]; then 
         printf "\e[1m%s\e[m:\n%s\n" "${cmd}" "${last_modified}"
         shift
         continue
      fi

      #printf "\e[1m%s\e[m (guess):\n%s\n" "${cmd}" "${vers}"
      printf "\e[1m%s\e[m (guess):\n%s\n%s\n" "${cmd}" "${vers}" "${last_modified}"

   fi

   shift

done

return 0
}


cmdversion bash
cmdversion /bin/bash

cmdversion sh java sed ls echo printf tr

cmdversion rm srm rmdir unlink kill killall

cmdversion read stat chown w tcl tk getopt getopts symlink ln locate

cmdversion chmod cp dd ed ssh

cmdversion bzcat openssl cat open alias uuidgen bc apropos man perl python ruby

cmdversion /sbin/fibreconfig


#-----------------------------------------------


# list the version numbers of dynamically loaded kernel extensions
man kextstat   
/usr/sbin/kextstat | /usr/bin/sed -E -n -e 's/^([[:space:]]+[^[:space:]]+){5}[[:space:]]+([^[:space:]]+)[[:space:]]+\(([^[:space:]]+)\).*$/\2  --  \3/p'

List manual pages and system commands on Mac OS X

bash --version
man --version

man builtin
help   # list builtin commands


alias mycmds="alias -p | /usr/bin/awk -F= '{print \$1}' | /usr/bin/sort; declare -F | /usr/bin/awk '{print \$NF }' | /usr/bin/sort"
mycmds


# For more information on Bash shortcuts see:
# - Bash Shell Shortcuts, http://linuxhelp.blogspot.com/2005/08/bash-shell-shortcuts.html
# - Bash Magic Tricks, http://gnubie.blogspot.com/2007/07/bash-magic-tricks_18.html

[TAB + TAB]   # hitting the TAB key twice will list all available commands
auto[TAB + TAB]   # list all available commands with name auto*


# cf. Terminal Productivity Tips,
# http://www.afp548.com/article.php?story=20070525141734763
#echo "set show-all-if-ambiguous on" >> ~/.inputrc    # ... then open new Terminal window ...
#[TAB]    # hitting the TAB key once will list all available commands
#auto[TAB]    # list all available commands with name auto*


export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec
export IFS=$' \t\n'

# For information on MANPATH see man man, man 5 man.conf
# and the specified MANPATH directories in: 
# sudo nano +40 /usr/share/misc/man.conf or 
# sudo nano +40 /private/etc/man.conf
# (make sure there is no MANPATH directory that is a symbolic link to another directory)

function mpath() { /usr/bin/man -W | /usr/bin/tr ':' '\n'; return 0; }
function mpath() { /usr/bin/man --path | /usr/bin/tr ':' '\n'; return 0; }   # alternative
function mpath() { /usr/bin/manpath | /usr/bin/tr ':' '\n'; return 0; }   # alternative
mpath

function path() { printf "%s\n" "${PATH}" | /usr/bin/tr ':' '\n'; return 0; }
function path() { printf -- "${PATH//:/\n}\n"; return 0; }   # alternative
path


man makewhatis
man whatis
man apropos
man periodic   # cf. nano /private/etc/periodic/weekly/500.weekly

# rebuild whatis database (index of manual pages)
# cf. http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/manpages.5.html
for dir in $(mpath); do find -x "${dir}" -type f -name whatis -ls; done
nano /usr/share/man/whatis
/usr/bin/sudo /usr/libexec/makewhatis.local $(/usr/bin/man --path)
/usr/bin/sudo /usr/libexec/makewhatis -v $(/usr/bin/man -W)   # alternative


# update locate database
# for a more secure locate see: /opt/local/bin/port info slocate
man locate locate.updatedb
/usr/bin/sudo /usr/libexec/locate.updatedb


# list manual pages
unset -f lsmans
function lsmans() {

   declare sudo find xargs basename sed
   sudo=/usr/bin/sudo
   find=/usr/bin/find
   xargs=/usr/bin/xargs
   basename=/usr/bin/basename
   sed=/usr/bin/sed

   for dir in $(/usr/bin/man -W | /usr/bin/tr ':' '\n'); do 
      #$sudo $find -x "${dir}" ! -type d 2>/dev/null
      $find -x "${dir}" \( -type f -or -type l \) -print0 2>/dev/null | $xargs -0 -n 3000 $basename
      #$find -x "${dir}" \( -type f -or -type l \) -print0 2>/dev/null | $xargs -0 -n 3000 $basename | $sed 's/\.[^\.]*$//'
   done

   return 0

}


mpath

lsmans
man -aW "*"

lsmans | wc -l
man -aW "*" | wc -l

man -aW  chmod
man -aW  "*chmod*"
lsmans | egrep -i chmod
apropos chmod
whatis chmod
locate -0 *chmod* | xargs -0 basename 
 
man -aW perl | nl
man -aW "*perl*" | xargs basename | nl
lsmans | egrep -i perl | nl
apropos perl | nl
whatis perl | nl

man -aW "*roff*" | xargs basename | nl
lsmans | egrep -i roff | nl
apropos roff | awk '{print $1}' | nl
whatis roff | nl

man -aW "*64*" | xargs basename | nl
lsmans | egrep -i 64 | nl
apropos 64 | awk '{print $1}' | nl

man -aW "*ssl*" | xargs basename | nl
lsmans | egrep -i ssl | nl
apropos ssl | awk '{print $1}' | nl

man -aW "*printer*" | xargs basename | nl
lsmans | egrep -i printer | nl
apropos printer | awk '{print $1}' | nl


lsmans | egrep -i '\.gz$'
lsmans | egrep -i util
lsmans | egrep -i sql
lsmans | egrep -i dns
lsmans | egrep "^ds[^A].*"
lsmans | egrep -i ssh
lsmans | egrep -i ssl
lsmans | egrep -i secur
lsmans | egrep -i ipsec
lsmans | egrep -i darwin
lsmans | egrep -i mac
lsmans | egrep -i apple
lsmans | egrep -i osx


#------------------------------


# list commands
unset -f lscmds
function lscmds() {

   declare sudo find xargs basename
   sudo=/usr/bin/sudo
   find=/usr/bin/find
   xargs=/usr/bin/xargs
   basename=/usr/bin/basename

   for dir in $(printf -- "${@:-${PATH}}" | /usr/bin/tr ':' '\n'); do 
      #$sudo $find -x "${dir}" \( -type f -or -type l \) -perm -555 2>/dev/null
      $find -x "${dir}" \( -type f -or -type l \) -perm -555 -print0 2>/dev/null | $xargs -0 -n 1000 $basename
      #$find -x "${dir}" \( -type f -or -type l \) -perm -555 -print0 2>/dev/null | $xargs -0 -n 1000 $basename
   done

   return 0

}


path

lscmds
lscmds | wc -l

lscmds /opt | egrep -i pdf
lscmds /usr | egrep -i pdf
#lscmds /opt | xargs -n 2000 basename | egrep -i pdf
lscmds /opt:/usr | egrep -i pdf

lscmds | egrep -i pdf | nl
apropos pdf | awk '{print $1}' | nl

lscmds | egrep -i roff | nl
apropos roff | awk '{print $1}' | nl

lscmds | egrep -i util
lscmds | egrep -i roff
lscmds | egrep -i html
lscmds | egrep -i "^ds.*"
lscmds | egrep -i x11
lscmds | egrep -i secur
lscmds | egrep -i file
lscmds | egrep -i mount
lscmds | egrep -i dns
lscmds | egrep -i ssh
lscmds | egrep -i ssl
lscmds | egrep -i system
lscmds | egrep -i darwin
lscmds | egrep -i mac
lscmds | egrep -i apple
lscmds | egrep -i osx