Howto find (true) executables in directories? (Solved)

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

Howto find (true) executables in directories? (Solved)

#1 Post by goingnuts »

I need to create a list of executables - but no symlinks or shell scripts or libraries should be included - only "real" programs.
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 '*'`"
I did try with "find ."+ different settings but cant get rid of the shell scripts or libraries so:
Anyone got ideas how to improve this?

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#2 Post by MochiMoppel »

Try

Code: Select all

BINARIES=$(file --mime-type /bin/* /usr/X11R6/bin/* | sed -n '/x-executable/ s/:.*$//p')
If you prefer awk:

Code: Select all

BINARIES=$(file --mime-type /bin/* /usr/X11R6/bin/* | awk -F: '/x-executable/ {print $1}')

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#3 Post by goingnuts »

Thanks! You got me on the right track. I have version 4.10 of "file" so I have no "--mimetype" flags to set but below solved it:

Code: Select all

BINARIES=$(file bin/* usr/X11R6/bin/* | grep  'ELF 32-bit LSB executable' | cut -d ":" -f1)

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#4 Post by amigo »

I'll post some shell code here which replaces the 'file'. You won't need all of it, but I can tell you that using bash 'read' is much quicker than calling the file-->grep combination.

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
All you really need there is to read the first four chars of each file, if it has ?ELF at the start then it is binary.

Very nice to see you rummaging around here my friend!

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#5 Post by vovchik »

Dear All,

Some time ago I wrote a little prog in Bacon to return the "magic" of a file. It also distinguishes between symlinks, scripts and ELF. The syntax is:

Code: Select all

magic filename magic_number
where the magic_number is 0 to 20. 0 shows largely what file shows, and 16 returns the mime type. Maybe it will be useful. THe archive contains a Tahr 32-bit binary, the Bacon source and a test script.

With kind regards,
vovchik
Attachments
magic.tar.gz
(11.45 KiB) Downloaded 117 times

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#6 Post by amigo »

while read -r -n 64 HEADER <<<$1
case $HEADER in
?ELF) echo 'is elf';;
esac

The above will be faster than calling any exterior program, by about 30 times. Not a big deal for a couple of items, but /usr/bin holds lots more than a couple.

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#7 Post by goingnuts »

Thank for all the different solutions! Nice to have options to choose from and tools for other tasks as well.

Post Reply