James Barnett System Administrator

Quick Tips for Linux

Get a list of packages installed on a certain date
rpm -qa --queryformat "Package: %{NAME} \t Version: %{VERSION} \t Installed: %{INSTALLTIME:date}\n" | grep $_Date
Note: uses dates like: 11 Jun 2012
Get a list of packages installed this month
rpm -qa --queryformat "%{NAME}-%{VERSION}.%{RELEASE} INSTALLED: %{INSTALLTIME:date}\n" | grep "$(date +"%b %Y")"
Find the 10 largest directories (non-recursive)
du -s /$_PATH/* | sort -gr | head | cut -f2 | xargs du -sh
WARNING: This breaks on directories/files names with space use
Find how much disk space a particular file type is using
find $_PATH -name '*.$_FILE_TYPE' -type f -print | xargs du -hc | grep total
Get a list of usernames & their assigned shell
cat /etc/passwd | grep /home | egrep -v 'noshell|false|nologin' | cut -d: -f1,7
List files in a user's home directory above a certain size
find /home/$_USER/ -size +$_SIZE
List files older than a month (by their last modified date)
find $_PATH -name '$_STRING' -mtime +30 -print -exec date -r {} +%F \;
get a DNS record from a list of name servers
for ns in {1..5}; do echo -n "ns$ns: "; dig -t $_TYPE $_FQDN @$_NS.$_DOMAIN.$_TLD +short; done
Note: The script expects names servers in the form of ns.domain.tld
For when the argument list is too long for mv
find /$_SOURCE/ -type f -name '*' -exec mv {}  /$_DESTINATION/. \;
Get the usable number of IP addresses in a given range
echo '(2^(32-$_CIDR))-2' | bc
specified in CIDR notation (e.g. /24)
A Highspeed download speed test for the command line
wget --output-document=/dev/null http://speedtest.wdc01.softlayer.com/downloads/test100.zip
Find the unused IPs on a network in CIDR notation
fping -r1 -g $_NETWORK 2> /dev/null | grep unreachable | cut -f1 -d' '
Get the make, model & serial number of a server via DMIDecode
dmidecode | grep -A4 'System Information' | egrep 'Serial Number|Manufacturer|Product Name'
Percentage of used memory
free -m | grep "Mem:" | awk '{printf "Used Memory: "  "%.2f'%'\n", $2 / $4}'
Check if a service is running
SRV="$_SERVICE"; service $SRV status 2> /dev/null | grep -q 'is running' && echo "$SRV is running" || echo "$SRV is NOT running"
Available disk space (in realtime)
watch -n 10 df -h -x tmpfs -x swap | awk '{print $1 " " $2 " " $3 " " $4}'
Find and replace on the command line with sed
find $_PATH -type f -exec sed -i 's/$_FIND/$_REPLACE/g' {} \;
Get date of the last major version upgrade of the OS
rpm -qa basesystem --queryformat '%{INSTALLTIME:date}\n'
Get date OS was first installed
find /root/install.log -type f -printf "%Tm-%Td-%TY\n"