Page 1 of 1

Simple Script to Find All Commands

Posted: 02 Apr 2016, 23:39
by Bogomips
Intrigued by a question on minichat, set about working this out. The script allcom.sh should hopefully cover 99% of commands. The output is a file of commands, including paths, to stdout. Invocation is clear from the two examples given with formatted output. The default has all commands up to a length of 8 characters.

Enjoy!

Code: Select all

guest@porteus:~$ sh allcom.sh 1
/usr/bin/w
guest@porteus:~$ sh allcom.sh 2 | column -x
/usr/bin/w	/usr/bin/ar	/usr/bin/as	/usr/bin/bc	/usr/bin/cp
/usr/bin/dc	/usr/bin/dd	/usr/bin/df	/usr/bin/du	/usr/bin/id
/usr/bin/js	/usr/bin/ld	/usr/bin/ln	/usr/bin/lp	/usr/bin/ls
/usr/bin/mc	/usr/bin/mv	/usr/bin/nl	/usr/bin/nm	/usr/bin/od
/usr/bin/pg	/usr/bin/pr	/usr/bin/ps	/usr/bin/rm	/usr/bin/sg
/bin/sh		/bin/su		/usr/bin/tr	/usr/bin/ul	/usr/bin/vi
/usr/bin/wc	/usr/bin/xx	/usr/bin/xz	/usr/bin/7z

Code: Select all

guest@porteus:~$ sh allcom.sh 3 | column -x
/usr/bin/w		/usr/bin/ar		/usr/bin/as
/usr/bin/bc		/usr/bin/cp		/usr/bin/dc
/usr/bin/dd		/usr/bin/df		/usr/bin/du
/usr/bin/id		/usr/bin/js		/usr/bin/ld
/usr/bin/ln		/usr/bin/lp		/usr/bin/ls
/usr/bin/mc		/usr/bin/mv		/usr/bin/nl
/usr/bin/nm		/usr/bin/od		/usr/bin/pg
/usr/bin/pr		/usr/bin/ps		/usr/bin/rm
/usr/bin/sg		/bin/sh			/bin/su
/usr/bin/tr		/usr/bin/ul		/usr/bin/vi
/usr/bin/wc		/usr/bin/xx		/usr/bin/xz
/usr/bin/7z		/usr/bin/act		/usr/bin/awk
/usr/bin/bat		/usr/bin/bzz		/usr/bin/cal
/usr/bin/cat		/usr/bin/cgm		/usr/bin/cjs
/usr/bin/cmp		/usr/bin/col		/usr/bin/cut
/usr/bin/cvt		/usr/bin/c44		/usr/bin/dir
/usr/bin/dwp		/usr/bin/env		/usr/bin/eqn
/usr/bin/esd		/usr/bin/fmt		/usr/bin/gpg
/usr/bin/grn		/usr/bin/gtf		/usr/bin/idn
/usr/kerberos/bin/ksu	/usr/bin/ldd		/usr/bin/lex
/usr/bin/lpq		/usr/bin/lpr		/usr/local/bin/man
/usr/bin/mcd		/usr/bin/mdu		/usr/bin/mev
/usr/bin/mmd		/usr/bin/mrd		/usr/bin/net
/usr/bin/pic		/usr/bin/ptx		/usr/bin/pwd
/usr/bin/raw		/usr/bin/rev		/bin/rpm
/usr/bin/scp		/usr/bin/sed		/usr/bin/seq
/usr/bin/ssh		/usr/bin/sum		/usr/bin/tac
/usr/bin/tar		/usr/bin/tbl		/usr/bin/tee
/usr/bin/tic		/usr/bin/toe		/usr/bin/top
/usr/bin/tty		/usr/bin/usm		/usr/bin/vte
/usr/bin/who		/usr/bin/xdm		/usr/bin/xev
/usr/bin/yes		/usr/bin/zip		/usr/bin/7za
/usr/bin/7zg		/usr/bin/7zr
The question as to whether Porteus has more commands than Sackware and vice versa, could probably be answered by running the same script under Slackware, then using the sorted output of the distros, from up to a command length of 8, together with unique. Sed could also be used to get rid of the paths, but probably there is no need for this. Also one needs to bear in mind that root has different commands available to it than those of an ordinary user.

Code: Select all

#!/bin/bash
# Simple Script to Find All Commands	(Bogomips 03.04.2016)
# Invocation: sh allcom.sh <Length Limit of Commands>
# Default is a limiting length of 8. At a limit of 3 slowing down starts.
# Reccomend running when machine idle
# l = Limiting Length of Command
# i = # of cols		c = Column Offset
# j = Iterator over Vector v	m = Vector: Offset of v for given Column
com(){
	local j; let j=0;
	if ((c<i+1)); then
		while ((j<n)); do
			# echo $i $j $c
			let m[c++]=j++; com;
		done
		let c--; return;
	fi
	# End of Line: String the Command
	cmd=""; for ((k=0; k<i+1; k++)); do
	cmd+=${v[m[k]]};
	done
	which $cmd 2>/dev/null; let c--;
	return;
}
cmd(){
unset v;for a in {a..z} {0..9}; do v+=($a); done
# l = Length of Command Limit
# i = # of cols		c = Column Offset
let n=${#v[*]};
unset m; let l=${1:-8};
for ((i=0; i<l; i++)); do  let c=0; com; done
}
cmd $1

Re: Simple Script to Find All Commands

Posted: 03 Apr 2016, 00:28
by Jack
Great job I'm running as Guest right now, but are they same or more for root?
EDIT:
It is slow cause it still finding the commands.

Re: Simple Script to Find All Commands

Posted: 03 Apr 2016, 00:47
by Bogomips
^ More, I think. :Search:

Re: Simple Script to Find All Commands

Posted: 03 Apr 2016, 01:44
by Jack
It still working.

My question is it is doing /bin and /usr/bin but will it do the /sbin and /usr/sbin too? If not can you build a script for the other commands?

Re: Simple Script to Find All Commands

Posted: 03 Apr 2016, 06:18
by donald
@ Bogomips
You can also use the bash built-in compgen

compgen -c will list all the commands you could run.
compgen -a will list all the aliases you could run.
compgen -b will list all the built-ins you could run.
compgen -k will list all the keywords you could run.
and many more....see the man pages..

Re: Simple Script to Find All Commands

Posted: 03 Apr 2016, 12:03
by Bogomips
Thanks, donald. If I never did this would not have found out about compgen. 8)

Re: Simple Script to Find All Commands

Posted: 16 Apr 2016, 07:16
by Rava
Sadly, functions are not to be given with compgen -f since that lists files in PWD.

You have to use
compgen -A function
instead...

Still, neat trick.