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 a lab!
passwd   Lets you change your password. You can't do it this way on a system that uses your UTORID (like tuzo and river).
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.

lpr fname   Prints fname on the printer in the computer lab.
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.

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.

sas fname   Executes SAS commands in fname.sas, yielding fname.log and (if no errors) fname.lst
R   Gets you into R
R --vanilla < boot1.R > boot1.out &   Executes R in "plain vanilla mode. Input from boot1.R, output in boot1.out, run in the background.

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.

mail yourname@yourisp.com < fname   Email a file to yourself. It will come to you in the body of a text-only email message. Very handy for getting files to your home computer for printing.
curl URL > fname   A URL is a Web address. This command is intended to help you get a copy of the source code of Web pages. But when the web page contains just a data file, as it sometimes does in this course, this is a great way to get a copy of the data. Copy/paste the complete URL from your browser.

 
f77 -o fname fname.f   Compiles Fortran 77 program in fname.f, producing executable code in fname. As of January 2017, this works on utstat.

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.

ssh nodename   ssh stands for Secure SHell; what you type is encrypted. Allows you to log into local node nodename, like ssh utstat.toronto.edu or ssh logincquest.utoronto.ca. PuTTy is an ssh tool.
sftp username@nodename   Secure File Transfer Protocol. Very useful for fast encrypted file copy, and available on servers at many universities and organizations. Primary commands are cd dirnameput fname  and get fname. Wildcards are ok.

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

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.

latex fname.tex   Processes LaTeX code in fname.tex, producing fname.dvi. Only on fisher.
dvips -f fname.dvi > fname.ps   Converts dvi file to Postscript. Only on fisher.
ghostview fname.ps &   Look at the rendered Postscript file with ghostview. Only on fisher under X-Windows.
ps2pdf fname.ps   Convert Postscript to PDF, creating fname.pdf. Only on fisher under X-Windows.
acroread fname.pdf &   View the PDF file with Acrobat Reader. On fisher or utstat under X-Windows. As of Dec. 2009, utstat has Version 8 while fisher has Version 5, and 8 is a bit better.

fname  

.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 an example.


#  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  ###########################



This handout is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.