Linux Command Reference

Included below are some commands that I have found, created or tweaked to help make life easier along the way. I have collected them here so I can easily refer to them instead of buildling up the commands from scratch. Some are very simple, others are approaching "script" level. Hopefully others will find them useful as well.

Misc Linux Commands

Find and compress all PNG files under the apache web root:

dir="/var/www/html" ; presize=`du -bs $dir | gawk '{ print $1 }'` ;
for a in $(find $dir -name '*.png'); do /home/user/bin/pngout $a; done ;
postsize=`du -bs $dir | gawk '{ print $1 }'` ; diff=`expr $presize - $postsize` ;
echo -e "Size before: $presize\nSize After: $postsize\nDifference: $diff"

Create a cron job from the command line (via STDIN):

crontab -e - <<< "*/15 * * * * top -n 1 -b"

Find all rpmnew and rpmsave files:

for a in $(find /etc /var -name '*.rpm?*'); do diff -u $a ${a%.rpm?*}; done

Redirect both stdout and stderr:

ping army.ca > /tmp/pingresult 2>&1

Debugging with strace:

strace -p

Network troubleshooting:

/usr/sbin/mtr army.ca

To configure the services started/stopped at a specific run level:

/usr/sbin/ntsysv --level 5

Reboot a different way (useful once when the reboot command was removed!):

/sbin/telinit 6

Ring a bell when a script completes (once for success, 2 beeps if it fails):

myLongScript.sh && echo -e '\a' || (echo -e '\a'; sleep 1; echo -e '\a')

finding stuff

Find and delete all empty directories:

find -depth -type d -empty -exec rmdir {} \;

A simple recursive grep (yeah, grep -r works here too...):

find . -exec grep -H "searchtext" {} \;

Find files/dirs that have "tight" permissions:

find . -exec ls -ld {} \; | grep -e "---"

Update permissions on all directories (but not files):

find . -type d -exec chmod 755 {} \;

Finding all files ending in .pl:

find . -name "*.pl"

Find files over 10 megs, modified in the last 3 days:

find / -size +10000k -mtime -3

Count the number of files (not directories) recursively:

find . -not -type d | wc -l

Finds all core dumps using regex:

locate -r "/core\.[0-9]"

Find all "temp" png files (png.X where X is a digit):

locate -r "png\.[0-9]"