Useful Shell Scripts

Useful scripts for unix/linux admins


This page contains a bunch of scripts that I've found useful over the years. Some are scripts that I've written to solve a specific problem on one of my servers, others are more general-purpose scripts that I or others have written to make some everyday admin tasks easier or more automated.

Nothing here is mind-bogglingly complex. Most of these could be written in a minute or two by any experienced shell programmer or unix/linux admin, but I thought I'd share them just in case it might save someone the time. None of these are under copyright, so you are free to use them as you wish. I also take no responsibility for any damage that may be caused by their use, misuse, or abuse. So don't blame me if you break something.

IP address monitor

Run as a cron job every 15 minutes or so. Monitors your external IP address and sends you an email if it changes. This is useful if you run a server on an ISP that assigns IP addresses dynamically. Any time your IP address changes you'll want to be notified right away so you can hit up your domain registrar and update your A records, MX record, etc. Make sure you change the email address at the top to your email address. And obviously you need to have a mail server or forwarder set up for this to work.

SUBJ="IP Address change notification"
IPADDR=`lynx --dump`
OLDIPADDR=`cat ~/ipaddr.txt`
if [ "$IPADDR" = "" ]; then
    echo "Got empty response from"
    exit 1
if [ "$IPADDR" != "$OLDIPADDR" ]; then
    /usr/bin/mail -s "$SUBJ" "$EMAIL" <<EOM

Your IP address appears to have changed:
    Old addr: $OLDIPADDR
    New addr: $IPADDR
echo "$IPADDR" > ~/ipaddr.txt
exit $?

Show full path to files

This will output the full path to a file or files in a directory. Useful when you need to get full paths to copy and paste into some other program, or you just need to generate a listing that shows the full path of every file. Any params you pass it will get passed on to the ls command. I've often wished ls could do this directly, so here is a way to make it do that. With a little tweaking, you could alias ls to this script, and make it pass everything directly to ls unless it finds a specific switch. If it finds the switch, it could do its thing, and if not, just act as a passthru to ls. Maybe I'll do that one day if I get some free time.

for file in $(ls "$@"); do
    echo -n $(pwd)
    [[ $(pwd) != "/" ]] && echo -n /
    echo $file

Link every file in src-dir to target-dir

Link each file in src-dir to target-dir. Thanks to Vasilios Alexiades at for this one.

#!/bin/csh -f

#--------- linkfiles: link each file from source_dir to targer_dir
#   usage: linkfiles srcdir tgtdir

set src = "$1"
set tgt = "$2"

echo " "
echo " --- linkfiles: link each file of srcdir into tgtdir --- "

if( "$1" == "" ) then
  echo -n " --- linkfiles srcdir tgtdir ---> enter srcdir: "
  set src = $<
if( "$2" == "" ) then
  echo -n " --- linkfiles srcdir tgtdir ---> enter tgtdir: "
  set tgt = $<

/bin/ls "$src" > /tmp/list

foreach fn ( `cat /tmp/list` )
	set tgtfn = "$tgt"/"$fn"
	echo " ...... linking $fn ......"
   if( -f "$tgtfn" ) then
	echo " ------ $tgtfn exists ----"
	ls -o "$tgtfn"
	echo " ------ Enter  y  to overide (else wont): "
	set yesno = $<
      if( "$yesno" == "y" ) then
	ln -sf "$src"/"$fn"  "$tgtfn"
	ln -s "$src"/"$fn"  "$tgt"/"$fn"
	ls -F "$tgt"/"$fn"
	sleep 1
	echo ""

/bin/rm -f /tmp/list

Make index.html page with a link for each file in a directory

Credit goes to Vasilios Alexiades at for this one too. This will generate an html page with a link to each file in the specified directory. Very useful if you need to make a bunch of files easily available through a web server. There are two scripts here and they're too big to include inline here, so grab this .zip file to download them.

More to come...


Return to Jeff Loughlin's home page

Last update: April 17, 2013