Code Snippets

“Good programmers know what to write. Great ones know what to rewrite (and reuse)”
– Eric S. Raymon “The Cathedral and the Bazaar

One of the best qualities that a good coder or admin can have is that of code reuse. Why reinvent the wheel when you already chipped away at it several years ago? Many people consider themselves great programmers or guru admins, however, I think everyone has something to learn, including myself. First and foremost I have added this code/commands here for my own reuse so I won’t forget them. They are stored centrally here to make my life easier when searching (many of which end up incorporated into my .bashrc). So if you think they are too simplistic or elementary, oh well, they’re for me, not you! But, if they are of use to someone, then great!

At one point or another I have found all of these entries useful. Where applicable I have added links to the original author from whom I have either modified code or received insight into creating a solid function, block, etc. Free free to use them or abuse them as you need. Contact me should you have suggestions, comments, or criticisms.

shell: Find all files greater than 50MB

view gist

(Added 06/15/2016) As I am testing on my personal virtual infrastructure I create VM’s that have very small disks to save space on my space contrained servers. I usually iterate very fast with my testing so I destroy them quickly after creation. Occasionally I’ll keep them around, the disks will fill, and then I’ll need to prune files so I can continue using them. So after many times of coming up with this command (I hate using prune with find!), I decided I needed to keep it around so that I don’t have to write it up again.

find / -path /proc -prune -o -path /dev -prune -o -type f -size +50M -exec ls -lh {} \; | awk '{ print $5 "\t: " $NF}' | sort --human-numeric-sort -r

NOTE: To change the size of files you’re looking for, modify the +50M to the desired size, e.g. +101M, for files over 101MB.

Example Ouptut:

100G    : /var/lib/docker/devicemapper/devicemapper/data
116M	: /var/cache/yum/x86_64/7Server/rhel-7-server-rpms/gen/primary_db.sqlite
102M	: /usr/lib/locale/locale-archive
64M   	: /sys/devices/pci0000:00/0000:00:0f.0/resource1
61M	    : /root/tcpdump_20150103.tgz

shell: View current connections with hostnames

view gist

(Added 06/16/2016) Sometimes it’s nice to see current connections and then resolve them to a hostname. I’ve used this to troubleshoot several systems.

netstat -tunlap | awk '{print $5}' | grep [0-9] | egrep -v '(127|0).0.0.*|::1'| awk -F ':' '{print $1}' | sort -u | xargs -n 1 host

Example Output on Ubuntu 14.04.4 LTS: domain name pointer domain name pointer domain name pointer
Host not found: 3(NXDOMAIN) domain name pointer

NOTE: It is possible that you may see the following error, in that case run the command as root.

(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.)