I have the following which exclude symlinks but not shell scripts:
Code: Select all
BINARIES="`$(which ls) -F bin/* usr/X11R6/bin/* | grep "*$" | tr -d '*'`"
Anyone got ideas how to improve this?
Code: Select all
BINARIES="`$(which ls) -F bin/* usr/X11R6/bin/* | grep "*$" | tr -d '*'`"
Code: Select all
BINARIES=$(file --mime-type /bin/* /usr/X11R6/bin/* | sed -n '/x-executable/ s/:.*$//p')
Code: Select all
BINARIES=$(file --mime-type /bin/* /usr/X11R6/bin/* | awk -F: '/x-executable/ {print $1}')
Code: Select all
BINARIES=$(file bin/* usr/X11R6/bin/* | grep 'ELF 32-bit LSB executable' | cut -d ":" -f1)
Code: Select all
#!/bin/bash
# /usr/bin/_file
# Gilbert Ashley
# possibly the fastest way to get a file type!
FILE=$1
get_header () {
local IFS=
#FILE=$1
#while read -r -n 17 HEADER ; do
while read -r -n 64 HEADER ; do
echo "$HEADER"
return 0
done < $FILE
}
HEADER=$(get_header $FILE)
# hexdump -C will let you see something similar to read -r
#echo ${HEADER:17} |hexdump -C
get_file_type() {
case "$HEADER" in
\#!*) #echo "shell script"
case "$HEADER" in
\#!*/bin/sh*) echo "shell script" ;;
\#!*/bin/ash*) echo "ash script" ;;
\#!*/bin/bash*) echo "bash script" ;;
\#!*/bin/csh*) echo "csh script" ;;
\#!*/bin/ksh*) echo "ksh script" ;;
\#!*/bin/perl*) echo "perl script" ;;
\#!*/bin/python*) echo "python script" ;;
#\#!*/bin/*|\#!*/sbin/*)
\#!*/env*)
set -- $(echo $HEADER)
echo "$2 script"
;;
\#!*)
Guess="${HEADER##*/}"
echo "$Guess script"
;;
esac
return 0
;;
?ELF*) echo -n "ELF"
#case "$HEADER" in
#HEX=$(echo $HEADER |hexdump -c)
#HEX=$(echo $HEADER |hexdump -x)
#HEX=$(echo $HEADER |od -x)
#printf %dc "'$HEX'"
#printf %*x "'$HEX'"
#echo "'${HEX:15}'"
#HEX=$(dd if=$FILE bs=1c count=1 skip=16 2> /dev/null |od -x)
#HEX="$(od -c -j 16 -N 1 $FILE)"
#case "$HEX" in
# "0000000 0001"*) echo " relocatable object" ;;
# "0000000 0002"*) echo " executable" ;;
# "0000020 002"*) echo " executable" ;;
# "0000000 0003"*) echo " shared object" ;;
# "0000000 0004"*) echo " shared object" ;;
#esac
# expensive re-examination!
HEX="$(od -c -j 16 -N 1 $FILE)"
#echo "'$HEADER'"
case "$HEX" in
"0000020 001"*) echo " relocatable object" ;;
"0000020 002"*) echo " executable" ;;
"0000020 003"*) echo " shared object" ;;
"0000020 004"*) echo " shared object" ;;
esac
#esac
;;
?7zXZ*) echo "xz compressed data" ;;
BZ*) echo "bzip compressed data" ;;
GIF8*) echo "GIF image data" ;;
?PNG*) echo "PNG image data" ;;
BM*) echo "PC bitmap data" ;;
'/* XPM */'*) echo "XPM image data" ;;
HPHP48-*) echo "HP48 binary" ;;
%*) case "$HEADER" in
%%HP:*) echo "HP48 text" ;;
%PDF-*) echo "PDF document" ;;
%!*) echo "PostScript document text" ;;
esac
return 0
;;
II\**) echo "TIFF image data, little-endian" ;;
M*)
case "$HEADER" in
MM?\**) echo "TIFF image data, big-endian" ;;
MThd*) echo "Standard MIDI data" ;;
MZ*) echo "MS-DOS executable (EXE)" ;;
esac
return 0
;;
PK*) echo "Zip archive data" ;;
R*)
case "$HEADER" in
Rar!*) echo "RAR archive data" ;;
RIFF????WAVE) echo "Microsoft RIFF, WAVE audio data" ;;
RIFF*) echo "Microsoft RIFF" ;;
esac
return 0
;;
'*** '* ) echo "diff patch file" ;;
# missing stuff:
# HTML,XML
# check for hex signatures to detect some file types
*)
HEX=$(echo $HEADER |hexdump -x)
# dd if=$FILE bs=1c count=2 skip=0 2>/dev/null |od -x
# od -N2 -x $FILE
case $HEX in
'0000000 8b1f'*) echo "gzip compressed data" ;;
'0000000 02f7'*) echo "TeX DVI file" ;;
'0000000 d8ff'*) echo "JPEG image data" ;;
*) echo "Unknown HEX=$HEX"
echo $HEADER |hexdump -C
;;
esac
return 0
;;
esac
}
get_file_type
Code: Select all
magic filename magic_number