Some Useful UNIX Commands

The following is a small but useful subset of UNIX operating system commands. Throughout, fname stands for the name of a file, and dirname for the name of a directory.

exit   Logs you off the system: ALWAYS log off before leaving!
passwd   Lets you change your password.
man command name HELP: explains command name (like man cp ). man command name > fname Put manual entry in fname, so you can print it.

ls   Lists file names
ls -l   Lists file names, long (fuller information)
ls -l -d   Shows info (inc access permission) about current directory
ls -F   Lists file names with / after directories, * after executable files
ls -a   Lists all file names, including those normally invisible.
ls *fname*   Lists all file names containing the string fname (* is wildcard)
ls fname[1234]   Lists files beginning with fname, ending with 1,2,3 or 4
ls ??a*   Lists all file names with "a" in 3d position (? matches any char)
ls dirname   Lists names of all files in directory dirname

cat fname   Displays fname on screen
cat fname1 fname2   Concatenates fname1, fname2, displays on screen as one file
cat fname1 fname2 > fname3   Puts fname1, fname2 together into fname3 (Generally < means comes from, > means goes to)
cat fname2 >> fname1   Adds contents of fname2 to the end of fname1.
cat > fname   Terminal input goes into new file fname; terminate with Control–D.
cat >> fname   Append terminal input to file fname.
less fname   Displays fname on screen, one page at a time: type q for quit, space bar for next page, b for back a screen, g top of file, G bottom of file, /pattern search for pattern, h for help.
more fname   A lot like less; available on some systems that do not have less.
tail fname   Displays last 10 lines of fname on screen.

laser fname   Prints fname on the laser printer. Local UTM command. lpr is standard.
draft fname   Prints fname on the dot matrix printer. Local UTM command.
lpq   Displays your entries on the printer queue
lprm #   Removes print job number # from the print queue

rm fname   Removes fname, erasing it forever
rm -i fname   Removes fname with interactive prompt
rmdir dirname   Removes directory dirname (must be empty)
rm -r dirname   Removes directory dirname and its contents recursively.

cp fname1 fname2   Copies fname1 to fname2
cp fname dirname   Copies fname to subdirectory dirname
cp ~/fname .   Copies fname (in the main directory) to .   which is the current directory.
cp /usr/local/lib/minitab/handbook/cartoon.DAT .   Copies a Minitab data set to current directory.

mv fname1 fname2   Moves (renames) fname1 to fname2
mv fname dirname   Moves fname to subdirectory dirname
mv fname1 dirname/fname2   Moves fname1 to subdirectory dirname, new name is fname2.

cmp fname1 fname2   Compares 2 files: Silent if identical. diff yields more output
cut -f2,4 -d: fname1 > fname2   Copies 2nd and 4th fields of fname1 into fname2; delimiter is a colon; can be anything.
cut -c5-8,10 fname1 > fname2   Copies columns 5 through 8, and col 10 of fname1 into fname2.
mkdir dirname   Create subdirectory dirname.

cd dirname   Change directory to subdirectory dirname
cd   Back to main directory
cd ..   Go up a level
pwd   Shows pathway (to current directory).

emacs fname   Starts the EMACS text editor, editing fname (can be new file)
vi fname   Starts the VI text editor, editing fname (can be new file)
pico fname   Starts the pico text editor, editing fname (can be new file)
jove fname   Starts the JOVE text editor, editing fname (can be new file)
teachjove   Gets you into an interactive JOVE tutorial.

minitab   Gets you into MINITAB
sas fname   Executes SAS commands in fname.sas, yielding fname.log and (if no errors) fname.lst
spss -m fname1 > fname2   Executes SPSS commands in fname1, yielding output in fname2.
spss -m -s 10m fname1 > fname2   Same as above, with 10 MB of workspace.

ps Shows active processes
kill -9 #   Kills process number # : Sometimes you must do this when you can't log off because there are stopped jobs. jobs Show jobs (including ones that are stopped)
kill % #   Kills job (not process) number #
fg #   Pulls (stopped) job number # into the foreground. Note that job number is different from process number.

f77cvt fname.for   Converts VAX fortran to SUN fortran, output in fname.f
f77 -o fname.out fname.f   Compiles SUN fortran program in fname.f, executable code in fname.out
f77 -u -C -listing -o fname fname.f   As above: -u means variable types must be declared; -C means check array subscript out of bounds; - listing means produce listing file fname.L.

chmod u+r fname   Allows the user to read fname
chmod g+r fname   Allows the group to read fname
chmod o+r fname   Allows other (everyone) to read fname. For security, directories that are to be read by other should have execute permission only.
chmod o-rwx fname   Removes read, write and execute permission for fname, for other. (other chmod commands follow this pattern)
fname &   Runs executable file fname as a background process. You can do other work while it's running, but the job will die when you log off.

nohup fname &   As above, but job does not die when you log off (no hangup).
nohup nice -5 fname &   Runs the job at a priority lowered by 5 levels, in such a way that you can log off and leave it running for a while. The nicest you can be is nice -19. The parts of this command may be used independently. Another example:
nohup nice -19 sas fname &   Runs a SAS job at lowest priority, suitable for running overnight. It has been many years since computers were so slow that I needed to do this.

chsh   Change shell: can select /bin/tcsh ; default is /bin/csh . With tcsh, last several commands can be replayed with up arrows; see set history below.

who   Who is on the system. w Like who; also tells load on system.
finger   Like who, but better. To get more information about a person, type finger name, where name can the person's first or last name, or login name.

rlogin nodename   Log into local node nodename, like rlogin utstat or rlogin server.sid.cquest. Typically not available on student accounts.
telnet   Gets you into telnet. Tuzo's telnet address is 142.150.1.22. Utstat is 128.100.73.1 UBVMSB is 128.205.2.3 Cquest is 192.82.128.1 Example: telnet 128.100.73.1 Probably students can't use telnet. You can also telnet to locations by nodename, like telnet utstat.toronto.edu.
telnet nodename   Like rlogin, but better for many purposes.

env   Shows values of environment variables.
setenv SAMPLE "Hi Mom"   Assigns the string Hi Mom to the environment variable SAMPLE.
set   Shows values of all shell variables.
set varname = value   Assignment of value to shell variable varname.
set history=20   Up arrow will display last 20 commands, one at a time. Only works in tcsh; see chsh A good line to have in the .login file in your home directory.
source fname   Executes the commands in fname , which can modify the current environment. Typically a shell script can only modify the sub-shell that executes its commands. Note fname must be executable; see chmod.

pine loginname   Lets you send mail to person with login name loginname. See pine handout for more information.
pine -f fname   read mail from fname rather than mbox.
from   If you have mail, lets you see whom it's from without getting into mail.

look   Look up a word in the dictionary. Echoes the word if found, no response if not found. Example: look plagiarism
rev fname   Reverses the file fname. Output may be redirected. Probably useless, but fun.

rn   Read news
stty erase '^?'   Makes control ? (backspace key) erase the character to the left. (shouldn't be necessary at Erindale)
ftp   Gets you into the File Transfer Protocol, very useful for fast file copy, and available on many machines at other universities. Can use Telnet addresses. Examples: open utstat, put fname1 fname2, get fname1 fname2,, dir, close, help. The commands mput and mget allow sending, receiving of multiple files. Wildcards are ok. Probably students can't use ftp.
ftp -i   Disables interactive prompting for multiple file transfers.

grep string fname   Returns all lines of fname containing string string.
awk '{print $2 " "$3" "$1}' fname   yields second, then 3d, then 1st field in fname. A field is anything with white space around it. Of course the output can be redirected. Awk is a full high level string manipulation language; this is just a useful example.
alias ls "ls -F"   ls now stands for ls -F . Will mark directories with a trailing slash (`/'), & executable files with a trailing asterisk (`*') alias go "f77 -u -C -o decrc decrc.f" Compile Fortran programme decrc.f by typing go.
alias saf "cp decrc.f decrc.bak ; del decrc.bak"   Execute two commands by typing saf.

command(s) >& fname   Redirects standard error from command(s) (can be file) to fname.
(commands > outfile) >& errfile   Need parentheses to do both

tr \* . < infile > outfile   Substitutes . for * in infile; output to outfile. Need backslash to escape wildcard. Watch out; the tr command will only work for sustituting strings of the same length. Maybe better is
sed s/"*"/"."/ tv1.dat > step1 Substitutes . for * in tv1.dat, output to step1.
sed s/"NOT AT HOME"/".   .   .   .   .   .   .   .   ."/ step1  >  tv1fixed.dat Substitutes 9 periods (SAS missing value codes) for the string NOT AT HOME. Input is step1, output is tv1fixed.dat

sort +1 -3 fname   Sorts on the first & second words in each line of fname. Sort field starts with word numbered + (start with zero) and ends with word BEFORE -. Output may be redirected of course.
cut -c1-3,6 < fname   Cuts all but cols 1 through 3 and 6
cut -f1,2 < fname   Cuts all but fields 1 and 2 (field delimiter is tab, by default).
cut -f1 -d: < fname   Delimiter is :

tar cvf 322ball 322s2000   Makes the directory 322s2000 into the tarball 322ball.
cat 322ball | tar xvBf -   After the tarrball has been ftp-ed to somewhere, this re-expands it. Since this directory was a Web site, I needed to recursively grant permissions with chmod -R ugo+rx 322s2000.

OTHER COMMANDS (May be added to this handout later)
cal
date
limit
quota
unlimit cputime
users
mesg n

.CSHRC

Assuming you use the cshell or tcshell (see chsh), you can customize your unix session with a file called .cshrc, in the home directory. Because the file name begins with a dot, it is normally invisible to ls unless you use ls -a. Here is my .cshrc


#  This file is executed whenever a new shell is created 
set history=20 
alias ls "ls -F" 
alias dds "ls -a -l" 
alias utstat rlogin -l brunner utstat 
alias info "ps -aux | grep -v root | grep -v 0:00 | sort | less" 
alias load "w | grep average" 
alias short "set prompt = '% '" 
alias renice "/usr/etc/renice 19" 
alias fol "source $HOME/bin/fol" 
alias cfol "source $HOME/bin/cfol" 
#  To undo the "foolproofing" in /etc/csh.cshrc 
unset noclobber 
unalias rm 
unalias cp 
unalias mv 
 
biff y 
mesg n 
set ignoreeof 
 
# This is so versaterm delete key works with mail 
stty erase '^H' 
stty new dec crt ff0 
 
# This makes prompt = current directory.  Inefficient? 
alias cd 'cd \!*;set prompt="$cwd > "' 
set prompt="$cwd > " 
 
# If /tmp/jerry.trashfiles does not exist, create it 
# Used by shell scripts del and empty 
# 
if (! -d /tmp/jerry.trashfiles) then 
   mkdir /tmp/jerry.trashfiles 
   chmod o-rwx /tmp/jerry.trashfiles 
endif 
##########################  End of .cshrc  ############################ 


SHELL SCRIPTS

These are files representing new commands that you can define; they can do more complicated things than aliases can. I put mine in a subdirectory called bin, and make sure my path includes $HOME/bin. See the .login file. Here are 3 sample shell scripts (del, empty and fol) that work on utstat under the tcshell.





######################################################################## 
# del Moves the argument to directory $HOME/trash instead of removing it. 
#     Bumps old version in trash to /tmp/jerry.trashfiles if it exists. 
#     Directory /tmp/jerry.trashfiles is created by .cshrc. 
# 
setenv stamp `date | awk '{print $2"_"$3"_"$4}'` 
echo "Moving files(s) to the trash ..." 
foreach FILE ($*) 
      if ( ! -f $FILE ) then    # if not file or directory (clumsy) 
         if (! -d $FILE ) then 
            echo "$FILE not found" 
            exit 
         endif 
      endif 
      if (-f $HOME/trash/$FILE) then 
         echo "   *  File $HOME/trash/$FILE exists. " 
         echo "   *  Bump it to /tmp/jerry.trashfiles/$FILE+$stamp." 
         mv $HOME/trash/$FILE /tmp/jerry.trashfiles/$FILE+$stamp 
      else if ( -d $HOME/trash/$FILE) then 
         echo "   *  Directory $HOME/trash/$FILE exists. " 
         echo "   *  Bump it to /tmp/jerry.trashfiles/$FILE+$stamp." 
         cp -r $HOME/trash/$FILE /tmp/jerry.trashfiles/$FILE+$stamp 
         rm -r $HOME/trash/$FILE 
      endif 
      mv $FILE $HOME/trash 
      echo "$FILE moved to $HOME/trash ..." 
end 
# 
#  Original del command was more simple: 
#    echo "Moving " $* " to $HOME/trash ..." 
#    mv -i $* $HOME/trash 
# 
######################################################################## 
 
 
###################################################################### 
#  empty     A command designed to empty the trash by moving file to 
#            /tmp/jerry.trashfiles, and affixing a time & date stamp. 
# 
setenv stamp `date | awk '{print $2"_"$3"_"$4}'` 
foreach FILE ($*) 
      if (-f $FILE) then 
         mv $FILE /tmp/jerry.trashfiles/$FILE+$stamp 
      else if ( -d $FILE) then 
         cp -r $FILE /tmp/jerry.trashfiles/$FILE+$stamp 
         rm -r $FILE 
      else 
         echo "$FILE not found" 
         exit 
      endif 
      echo "$FILE moved to /tmp/jerry.trashfiles/$FILE+$stamp" 
end 
chmod o-rwx /tmp/jerry.trashfiles/*  
###################  end of empty  ############################ 
 
############################################################################ 
# fol: Move a file to a directory and follow it there 
# Syntax: fol  fname  dirname        $1 is 1st argument, $2 is second. 
# Note that so cd will operate on the current shell rather than a subshell, 
# .cshrc must have  alias fol "source $HOME/bin/fol" 
# 
if ( -d $2 ) then             # if $2 is a directory ... 
   if ( -f $2/$1 ) then 
      echo "File " $2/$1 "already exists.  Trash it." 
      del $2/$1               # using personal command del 
      mv $1 $2 
      cd $2 
      echo "Moving " $1 " to " $2 "and following" 
   else 
      echo "Moving " $1 " to " $2 "and following" 
      mv $1 $2 
      cd $2 
   endif 
else if (-f $2) then             # if $2 is a file ... 
      echo "File " $2 "already exists.  Trash it." 
      del $2 
      echo "Moving " $1 " to " $2 
      mv $1 $2 
      echo "$2 is now a file!" 
      echo "Cannot   cd " $2 
else                             # if $2 is neither dir nor file 
      echo "Moving " $1 " to " $2 
      mv $1 $2 
      if ( -f $2 ) then 
        echo "$2 is now a file!" 
        echo "Cannot   cd " $2 
      endif 
endif 
########################  end of fol  ###########################