Useful Shell Scripts

Useful scripts for unix/linux admins

By

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.

#!/bin/bash
EMAIL="admin@yourdomain.net"
SUBJ="IP Address change notification"
IPADDR=`lynx --dump http://ipecho.net/plain`
OLDIPADDR=`cat ~/ipaddr.txt`
if [ "$IPADDR" = "" ]; then
    echo "Got empty response from ipecho.net"
    exit 1
fi
if [ "$IPADDR" != "$OLDIPADDR" ]; then
    /usr/bin/mail -s "$SUBJ" "$EMAIL" <<EOM

Your IP address appears to have changed:
    Old addr: $OLDIPADDR
    New addr: $IPADDR
EOM
fi
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.

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

Link every file in src-dir to target-dir

Link each file in src-dir to target-dir. Thanks to Vasilios Alexiades at utk.edu 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 = $<
endif
if( "$2" == "" ) then
  echo -n " --- linkfiles srcdir tgtdir ---> enter tgtdir: "
  set tgt = $<
endif

/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"
      endif
   else
	ln -s "$src"/"$fn"  "$tgt"/"$fn"
	ls -F "$tgt"/"$fn"
	sleep 1
   endif
	echo ""
end

/bin/rm -f /tmp/list
exit

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

Credit goes to Vasilios Alexiades at utk.edu 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