Some useful scripts and commands

Unix Admin scripts General commands and utilities
Text processing Networking

Unix admin Scripts and commands.

All the scripts and commands concerning system administration are in this section.

Filesystem scripts System monitoring
Printing Hardware
File systems scripts and commands

Find ten day old files
#/bin/ksh
echo "This script will put the names of ten day old files in /tmp in /tmp/checkold.txt file"
find /tmp -size 0 -atime +10 -exec ls -l {} \; > /tmp/checkold.txt
find /tmp -size 0 -atime +10 -exec rm -f {} \;


Find large files on System
---------
#!/bin/sh
# Script to find the big files on a disk. 
# Defaults and temporary info.
# This script send the output to a default printer
DiskName=${1:?Disk name not supplied.}
TMPFILE=/tmp/$LOGNAME.$$.txt
SysName=`uname -n`
 
echo \
        "Checking for large files on $SysName $DiskName.  " \
        "Output will be sent to default printer."
 
echo "\nNewer big files on $SysName $DiskName" >> $TMPFILE
find $DiskName -type f -mtime -3 -size +5000 -exec ls -ld {} \; \
        2>/dev/null | sort -n -k 5.1,5 >> $TMPFILE
 
echo "\nOlder big files on $SysName $DiskName" >> $TMPFILE
find $DiskName -type f -mtime +3 -size +5000 -exec ls -ld {} \; \
        2>/dev/null | sort -n -k 5.1,5 >> $TMPFILE
 
#change the following line if you want to put output to a file
#or anything.

lp -onb -olandscape $TMPFILE
rm $TMPFILE
 # End of script


Count files in directory (not hidden one's)
#!/bin/ksh
echo * | wc -w


Total Disk space in system in MB.
#!/bin/ksh
#This script will return the total disk space in system.
df -t | awk 'BEGIN {tot=0} $2 == "total" {tot=tot+$1} END {print (tot*512)/10000
00}'


Total used space in system in MB.
#!/bin/ksh
#This script tells total used space in a system
df | awk 'BEGIN {tot=0} {tot=tot+$4} END {print (tot*512)/1000000}'


Search for pattern in a file and return total count.
#!/bin/ksh
#This script  searchs the file passed as first argument to this command line and 
#prints total number found
grep $1 filename | awk 'BEGIN {tot=0} {tot=tot+$1} END {print (tot)}'


This command will find the core files with group name mycomp and delete them while also writing the name of deleted file in mycompcore.
#!/bin/ksh
find / -name core -group mycomp -print -exec rm -f {} \; >> mycompcore

back to top
Back to main page

System monitoring scripts
Utilities available on different systems.
Umask over FTP

The easiest way to set the umask for a user when they ftp files to a server is to write a wrapper program as follows:

#!/bin/sh
umask xxx <- insert choice here
/usr/sbin/in.ftpd
save it as ftpwrap.sh (or anything you like). Along with your TCP wrappers the line in inetd.conf looks something like this:
ftp    stream  tcp     nowait  root    /usr/sbin/tcpd          ftpwrap.sh

Shell scripts from Crontab vs. command line.

Usually when a shell scripts that works from command line but not from crontab file,it means that crontab is not using the correct shell to execute this script. That means that the first line in script #!/usr/bin/ksh is incorrect and ksh is not in /usr/bin or something else to that extent.


Sample system monitoring Script
#!/bin/sh
## This script monitors a system named dxi4.
## Date created : May 15 1997
  
rm status.dxi4
echo '' > status.dxi4
echo '         ****************************************************' >> status.d
xi4
echo '         *************** LATEST DXI4 STATUS *****************' >> status.d
xi4
echo '         ****************************************************' >> status.d
xi4
echo '' >> status.dxi4
echo '         ***************        DATE        *****************' >> status.d
xi4
echo '' >> status.dxi4
date >> status.dxi4
echo '' >> status.dxi4
 
banner Netstat >> status.dxi4
echo '' >> status.dxi4
netstat -m >> status.dxi4
echo '' >> status.dxi4
banner Fax >> status.dxi4
echo '' >> status.dxi4
fxstat >> status.dxi4
echo '' >> status.dxi4
echo '         ****************************************************' >> status.d
xi4
banner Disk >> status.dxi4
echo '' >> status.dxi4
df >> status.dxi4
echo '' >> status.dxi4
 
echo '         *******************Virtual Memory ******************' >> status.d
xi4
sar >> status.dxi4
 
banner Paging >> status.dxi4
sar -p >> status.dxi4
echo '         *******************free virtual memory *************' >> status.d
xi4
sar -r >> status.dxi4
echo '         ****************************************************' >> status.d
xi4
echo '         ******************* Processes  *********************' >> status.d
xi4
echo '         *************   All rtm processes   ****************' >> status.d
xi4
echo '' >> status.dxi4
ps -ef | grep 'rtm' >> status.dxi4
 
echo '' >> status.dxi4
echo '         *************  All dbsid processes  ****************' >> status.d
xi4
echo '         **Check that synchronization is only one per site **' >> status.d
xi4
echo '' >> status.dxi4
ps -ef | grep 'dbsid' >> status.dxi4
echo '' >> status.dxi4
echo '         *************   All pws processes   ****************' >> status.d
xi4
echo '' >> status.dxi4
ps -ef | grep 'pws' >> status.dxi4
echo '' >> status.dxi4
echo '         *************   All sql processes   ****************' >> status.d
xi4
echo '' >> status.dxi4
ps -ef | grep 'sql' >> status.dxi4
echo '' >> status.dxi4
echo '         ****************************************************' >> status.d
xi4
echo '' >> status.dxi4
echo '         ****************** System Error Report *************' >> status.d
xi4
echo '' >> status.dxi4
tail -500 /usr/adm/messages >> status.dxi4
echo '' >> status.dxi4
 
echo '       ********************* Top Will now run **************************'
>> status.dxi4
more status.dxi4
 
top

back to top
Back to main page


Printing scripts (systems admin)
Scripts and general info regarding printers.


Hardware scripts (systems admin)
tar -xvf gcc -C / will let you decide the root directory of untarring itself.

back to top
Back to main page


General Shell commands and scripts

This includes general commands shell scripts.
Alias input from user.
$< will read a line of user input and return it. alias abc 'echo -n lynx http://www.;lynx http://www.$<' The echo command causes the partial command to be printed as a prompt, and then the user response is substituted into the command with $<.



back to top
Back to main page


Text Processing Commands and scripts.

All the text processing commands+scripts



Pico editor.

Some user's don't like to use vi the default editor with all unix systems. You can get a user friendly editor called pico which is also embedded into pine e-mail system is available at
http://www.washington.edu/pine/overview/availability.html



Change file names to upper case

To change the case (upper to lower or viceverse) of a large number of files. You can use this following script.

#to change uppercase filenames to lowercase 
#!/bin/sh
if [ $# -eq 0 ] ; then
echo Usage: $0 Files
exit 0
fi
for f in $* ; do
g=`echo $f | tr "[A-Z]" "[a-z]"`
echo mv -i $f $g
mv -i $f $g
done


back to top
Back to main page

Networking commands and Scripts.
All tcp/ip commands, scripts, etc here.
back to top
Back to main page


Other problems and solutions
Kernel configuration trouble.

If mk_kernel -s system is giving you error after you have installed patches or have done similar installation. That usually means that JFS got messed up by the patches; recompiling the kernel would give the same error (but different unsatisfied symbols). To see the depencency and conflicts you have in kernel do

 swconfig \* 
Then resolve those conflicts and recompile the kernel.

System hog problems

Some commands like find which if are run recursively can bring down system virually to a halt state. Glance utility that is HP-UX product can see that which find command seems to be hogging most of the cpu time, spending most of its time in stat() and lstat() calls. Disc, memory and network are idle at the time.

find is looking through every directory in the entire system, a very, very bad thing to do. Since the task involves a bit of computing, and a bit of disk directory reading, it will hog resources proportional to the number of files on thye system disks. When disks were 250-500 megs, one or two users and the Unix box had perhaps 10-20,000 files, find / was a possible method to locate a file. Today, it is considered very bad practice and most sysadmins will wrap the find command with a test to prevent this bad practice.


To setup DNS
As a client or as a server?

As a client, you first need to identify the IP address of the DNS server you with to use. Let's call that address 10.0.0.1. Then:
create /etc/resolv.conf to contain at a minimum:


	domain spinet.gov.sp
	nameserver 10.0.0.1

test with nslookup to ensure that lookups will work correctly

edit /etc/nsswitch.conf and change the "hosts" line to include dns. For example, if it did say "hosts: files" then change it to "hosts: files dns" . At this point any *newly created* processess will be using DNS. But the daemons started before you made these changes (such as sendmail) will not see the change to nsswitch.conf until they are restarted. If this is important, then either restart the affected daemons or just reboot the machine. But make sure that DNS lookups work correctly before rebooting or you may have a very difficult time getting the machine to come back up again.


back to top
Back to main page
Fundamentals of Unix Unix commands User Accounts Shell Programming
File Systems Networking Backups Security
Installing software Installing Hardware Performance and Tuning Some Useful Scripts

These scripts and other information was collected from usenet newsgroups pertaining to unix like comp.unix.shell and comp.unix.admin. Your suggestions and comments are welcome. Please e-mail me
Copyright reserved with Sandeep S Bajwa.