UNIX One Liners

....and the occasional short script.

Things that I have occasionally found useful.

Looking for last accessed files

This was part of my reporting so that I could try and get people to remove some data from there storage space on our HPC. This one-liner reports in GB the amount of storage taken up by files that haven't been accessed in over 1 year.

NOTE: I think there may be a bug in this, will need to check.

find . -atime +365 -exec ls -ltr '{}' \; | sh | awk '$NF {c+=$5} END {print c/1073741824 " GB"}'

To get user data, run this in the directory containing users home directory as root.

#!/usr/bin/bash for i in `ls -1`; do find $i -atime +365 -exec ls -ltr '{}' \; | \ awk '"'$NF'" {c+="'$5'"} END {print \"$i \" c/1073741824 \" GB\"}' done

Spliting data

This assumes a stream of one column data that one may wish to reformat as a float with 3.d.p. and then takes every 266 and makes that a row. Therefore if there were 266*266 values in the file, you would get a 266*266 matrix.

cat file | awk '{printf "%.3f\n", $i}' | xargs -n 266

Merge two files

Assume you have two files where the rows represent the same entry, but the columns are in different files....so we'd like to join them.

pr -m -t -s" " file1 file2 | gawk '{print $0}'

Check a bunch of servers for almost full partitions

Assumes you have set up the ssh keys so you don't need to enter the password.

#!/usr/bin/bash THRESHOLD=90 for i in server1.domain.net server2.domain.net server3.domain.net; do ssh -q root@$i 'df -hP' | \ grep -v Filesystem | \ sed 's/%//g' | \ awk '{ if($5>='"$THRESHOLD"') print "Warning: Low disk on ""'"$i"'" $5}' done

DVD to iso, and back again

dd if=/dev/dvd of=myiso.iso cdrecord -v -dao speed=1 dev=/dev/dvd myiso.iso