Home
Up

Unix Commands

at at -m 1203pm Dec 17
at> /etc/reboot
at> ctrl-d
Runs as shell Schedule a one-off job
basename basename poo.com .com should return poo use to remove extension from a filename
bell /usr/5bin/echo '\007' Just echo on system 5 machines sound a basic bell
col man ls | col -b | lp col takes out the escape sequences for reverse highlighting Print manual pages
cpio find . -print | cpio -oaB | rsh machine "(cd /tmp ; cpio -ivumBd)" Retains permissions Transfer directory to remote machine
cpio cd /
find home/mars_a -print I cpio -oaB > /dev/rmt/0
cpio -ivumBd exact_dir&filename < /dev/rmt/0
cpio -ivumBd < /dev/rmt/0
Retains permissions cpio backup and restore
cpio rsh machine "( cd /tmp ; find . -print | cpio -oaB )" | cpio -ivumBd retains permissions Transfer directory from remote machine
cpio find . -print | cpio -pmd /tmp retains permissions Transfer directories across filesystems
csh foreach user ( user1 user2 user3 )
ls -l ~user/.cshrc
end
good for operations in home-directories for each loop
csh (time date > /tmp/date_out) >& /tmp/error   Standard error to file
csh tar cvf $TAPE . >& /tmp/log   Redirect output and error
csh if ( $1 ="hello || $2 ="goodbye" ) then
echo "greetings"
endif
  if loop
dc dc
99k
1 1 32 41 ^ / - 60 ^ 60 ^ 24 ^ 365 ^
Figure between 0 and 1 of a monkey not typing a line from hamet in a year, given 32 keys on keyboard and 41 keystokes for the phrase. CPU exerciser
du du -skd /   Du which doesn't cross filesystems
exec exec 2> errors.log Put in the beginning of a shell script. Redirect errors
expr i=1
expr $i + 1
Shell example, setenv for csh etc. Mathematics in shell
find find . -type f -mtime 0 -exec ls -l {}\;   Find files modified in last 24hrs
find find . ! -user root -exec ls -l {} \;   files not owned by a user
find find . -mtime -3 -exec ls -l {} \; mtime = modified, atime = accessed Find files modified within the last X days
find find . -atime +X -exec ls -l {} \;   Find files that haven't been accessed in X days
find find . \( -name "add*" -o -name ".add*" \) -print   Find files .add* or add*
find find . -xdev -type d '[a-zA-Z0-9] -exec du -sk {} \; I sort -nr I head -50   Biggest 50 directories
find find . -name ignore -print -o -size 5000000c -ls wont find directories beginning with . find files bigger than 5m, exluding a directory
find find / ! -local -prune -o -group exfbiz -ls   Find all files owned by a group on a server.
ftp ~/.netrc
machine dalsb3.dal login axmarsha password guess??
macdef init
get file1
close
quit

< blank line to end macro definition >
Permissions on ~/.netrc = 600
Automated session ftp -v dalsb3.dal
Interactive session ftp -n dalsb3.dal
How to automate an ftp job
fuser fuser -u /dev/pts/*   What psudo ttys are in use
grep cat file | egrep -e 'string1|string2' egrep also works with the exclude option (-v) how to search for more than one thing
Inittab /etc/inittab
bb:respawn:/usr/local/bin/xxx
If the prog xxx is prone to dying when started from an rc script.
Running it from inittab will get it respawned.
Respawn a process that dies
iostat iostat -x 30 (Solaris 2) Look in the %b column for >30% busy
Look in svc_t (service time) to see if that
disk is also achieving times >50ms
Does the system have a disk bottleneck ?
iostat iostat -En Use to identify physical disks How to find disk serial numbers
iostat iostat -xpM 5 1000 | sed -f /usr/local/etc/iostat.sed   iostat into per mount point statistics
ls ls -l I sort -nb +4   Sort an ls -l on size
lsof lsof -i I grep port_no as root Will give other processes that hook into that port Clear a TCP port in close_wait state
lsof lsof -p PID   show open files for process
lsof lsof -iTCP@192.122.122.34
lsof -i @192.122.122.34
  Show open files for an IP address
lsof lsof -iTCP@machine:1-50000   Examine all TCP ports
lsof lsof -u username   Open files for a user
lsof lsof /dev/hme   Show processes that has file in use
mv #!/bin/sh
# @brename
# $1 is pattern to match
# $2 is pattern to replace
for NAME in 'ls *$1*'
do
NEWNAME='echo $NAME | sed s/$1/$2/g'
echo $NAME "to " $NEWNAME
mv $NAME $NEWNAME
done
*cat.dgn to *dog.dgn = brename cat.dgn dog.dgn rename all files in directory that match a pattern
ncheck ncheck -Fufs -s /dev/dsk/c0t0d0s0   Find SUID GID files
netstat netstat -I le0 | tail -1 | awk '{ print $9 /$7 }' Should be less than 10% Collisions on network
newfs newfs -i 100000 -m 0 /dev/rsd10a   Maximise space by minimising free-space and inodes
nfsstat nfsstat -m srrt - smoothed round trip times the time for request and response should be less then 50ms NFS server performance
nfsstat nfsstat -m srtt - smoothed round trip time
For local filesystems all ~7ms
for intranet filesystems all ~25
Shows each nfs mounted filesystem staistics
pr pr -t -4   put output into 4 colums
printf .... printf "%40-s%40-s\n",$1,$2 s is a string -s is left justifed Print 2 colums of text within 40 Character fields
ps ps -ecl I sort -k 7,7r PRI column high numbers high priority Process schedule priorities
ps ps -efl does the SZ column get bigger ?   Is a process leaking memory?
regular expressions . any single character
^ begining of line
$ end of line
.$ character at end of line
^$ blank line
[A-Za-z0-9] alphanumeric
[^0-9]
  Pattern matching, used in ed/sed/awk etc.
rm ls | grep -v fileXXX | xargs rm   delete all files except fileXXX
sed sed s/-..../\ \ 0.0/ | sed s/-.../\ 0.0/ Pad with spaces to keep columns Change negative numbers to 0.0
sed sed '/^$/d' inputfile > outputfile   Strip out blank lines
sed sed '11,5d' filename > filename2 Starting at line 11, delete 5 lines Delete a range of lines
sed sed '/reg-ex/d' filename > filename2   Delete a line containing
sendmail telnet localhost 25
MAIL FROM: jerk@nowhere
RCPT TO: root@localhost
DATA
This is the message
.
  Send email from a fake id
sendmail (.forward) ~/.forward
\axmarsha, otheruser
\ tells the mailer not to loop Send the mail to two people
sh case value in
0) echo "number = nothing"
;;
*) echo "number =something"
;;
esac
esac is case spelt backwards !! case statment
sh ping machine 1 >/dev/null 2>&1
if [ $? -gt 0]
then
echo $machine "down"
else
echo $machine "up"
fi
  exit status
sh [ -f filename1 -o -f filename2 ] if filename1 or filename2 are files or
sh ECHOIT() {
echo "this is the function echoit"
echo $*
return 99
}
# end of function
ECHOIT hello good evening
echo $? # exit status of function
  Functions
sh -b block special files
-c character special file
-d directory
-f file
-g group setuid
-k sticky bit set
-p named pipe
-r readable
-s size not equal to 0
-v set UID
-w writtable
-x executable
-eq equal
-ge greater than or equal to
-gt greater than
  Test operators
sh for loop in hosta hostb
do
echo $loop
rsh $loop uname -a
done
  for loop
sh [ -f filename -a r filename] if filename is a file and is readable and
sh [ ! -r filename ] not readable not
sh read #! /bin/sh
echo "yes or no ?"
read CONFIRM
if [ `expr X"${CONFIRM}" : 'X[Yy]'` -ne 1 ]: then
echo "I guess you mean yes"
else
echo "I guess you mean no"
fi
expr counts the number of characters that match on either side of the ":" Read yes|Yes|Y as yes or no
Sort ypcat passwd I sort -t: +2 -3n Numeric sort on field 3 Sort passwd file
ssh-agent ssh-agent /bin/sh
ssh-add
  how to a ssh without a password
tar rsh -n host dd if=/dev/rst0 bs=20b | tar xvBfb - 20 files
tar cvfb - 20 . | rsh host dd of=/dev/rmt0 obs=20b
  Remote restore and write
tr tr -d '..' "." = <SPACE> Delete multiple spaces
tr tr 'X' 'x'   Translate X -> x
tr tr '()' '{}' ( -> {, ) -> } translate brackets
tr tr '[A-Z]' '[a-z]'   translate upper <-> lower case
tr tr -d '\014' man ascii will give other codes Delete all form feeds
mail mailx -s "rubbish" mars_a < /tmp/rubbish   Send an email from the comand line
vi :set list   show special characters
vi :g/^$/d   delete all blank lines
vmstat vmstat 5 Ignore the fre, pi and po column
The sr (scan rate) column is the important one
this could reach over 200 pages/sec
Are you short of RAM ?
vmstat vmstat 5 Look at the procs r (run queue)
This should be less than 4 times no. of CPU's
Long run queue / CPU bound
vmstat vmstat 5 procs b is a sign of disk bottleneck, forgivable if a system is running db batch jobs. Look for process blocked waiting for I/O
who who -r   Current run level
ypmatch ypmatch key map espcially useful for use with nested netgroups find keys in a NIS map
ypxfr ypxfr -f -h master-server mapname Issue command from slaves New NIS map wont push to slaves

 

  © Andrew Marshallsay 2008