BASH tricks

2016-07-02

Just a bunch of bash tricks i’ve picked up.

Save a backup copy of a file with a datestamp.

1
cp script.sh script.sh.$(date +%Y%m%d%H%M)

Generate a random string maybe for api key or password.

1
cat /dev/urandom | env LC_CTYPE=C tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1

Find empty directories.

1
find . -mindepth 1 -maxdepth 1 -type d -empty

List processes being used by remote terminals and then show the files that may be opened by them. Good for tracking down what another ssh user might be doing .

1
for i in $(ps aux |grep pts |awk '{print $2}');do lsof -p $i;done

grep out IP addresses from a log file.

1
grep -E -o '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)' access.log

Fping to show unused IP’s on a subnet.

1
fping -r1 -g 192.168.1.0/24 2> /dev/null | grep unreachable | cut -f1 -d' '

arping a /24 subnet to find alive hosts.

1
for i in `seq 1 254` ; do arping -c 1 192.168.1.$i | grep reply ; done

Comments: