Simple Script to Find All Commands

For discussions about programming and projects not necessarily associated with Porteus.
Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Simple Script to Find All Commands

Post#1 by Bogomips » 02 Apr 2016, 23:39

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
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

Jack
Contributor
Contributor
Posts: 1857
Joined: 09 Aug 2013, 14:25
Distribution: Porteus and Nemesis
Location: USA

Re: Simple Script to Find All Commands

Post#2 by Jack » 03 Apr 2016, 00:28

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.
Last edited by Jack on 03 Apr 2016, 00:48, edited 1 time in total.
I just like Slackware because I think it teach you about Linux to build packages where Ubuntu is like Windows you just install programs you want.

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Simple Script to Find All Commands

Post#3 by Bogomips » 03 Apr 2016, 00:47

^ More, I think. :Search:
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

Jack
Contributor
Contributor
Posts: 1857
Joined: 09 Aug 2013, 14:25
Distribution: Porteus and Nemesis
Location: USA

Re: Simple Script to Find All Commands

Post#4 by Jack » 03 Apr 2016, 01:44

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?
I just like Slackware because I think it teach you about Linux to build packages where Ubuntu is like Windows you just install programs you want.

donald
Full of knowledge
Full of knowledge
Posts: 2061
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

Re: Simple Script to Find All Commands

Post#5 by donald » 03 Apr 2016, 06:18

@ 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..

Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

Re: Simple Script to Find All Commands

Post#6 by Bogomips » 03 Apr 2016, 12:03

Thanks, donald. If I never did this would not have found out about compgen. 8)
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: Simple Script to Find All Commands

Post#7 by Rava » 16 Apr 2016, 07:16

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.
Cheers!
Yours Rava

Post Reply