CS 114: UNIX Tools
Lecture, Friday, March 16, 2001

These are just lecture notes; for more detail, you should consult the course books or the UNIX man pages.

Previous lectures: C shell (csh)

Other Shells:
Bourne shell (sh)
Korn shell (ksh)

ksh is a superset of sh, with some of the features of csh as well

tcsh is a superset of csh - adds file name completion and command line editing

bash = GNU Bourne-Again SHell - superset of kcsh and csh

sh and csh always available on UNIX systems, others vary by the installation

Shell Differences from csh:
sh and ksh use .profile instead of .login and .cshrc

sh - different syntax for variable assignment and script flow control than csh
no history, aliasing, or job control (fg, bg, ^Z, jobs), ~

ksh - adds alias, job control, history, etc.
fc is the ksh equivalent of !, but allows for editing commands before using them, combining them, etc.

Running a shell:
can run them just typing the shell name at the command prompt (e.g. >csh )
can run scripts from other shells by sh script.sh


Linking:

ln - create a link
like "shortcuts" on PCs
can create a "link" between a filename in one location and an actual source file in another location - a different name, or an entirely different directory

hard links - default created by ln - any change made to the file as indicated by the link are made to the file itself, a pointer to the file

syntax: ln <original file> <new file>

Example:

babbage% ls s*
song1.txt
babbage% ln song1.txt song2.txt
babbage% ls s*
song1.txt song2.txt
babbage% diff song1.txt song2.txt
babbage% ls -l s*
-rw------- 2 amh16 cs114 102 Mar 15 17:42 song1.txt
-rw------- 2 amh16 cs114 102 Mar 15 17:42 song2.txt
babbage% chmod a+r song1.txt
babbage% ls -l s*
-rw-r--r-- 2 amh16 cs114 102 Mar 15 17:42 song1.txt
-rw-r--r-- 2 amh16 cs114 102 Mar 15 17:42 song2.tbbxt
babbage% vi song2.txt
add [repeat] as last line
babbage% diff song1.txt song2.txt
babbage% ls -l s*
-rw-r--r-- 2 amh16 cs114 111 Mar 15 17:45 song1.txt
-rw-r--r-- 2 amh16 cs114 111 Mar 15 17:45 song2.txt
babbage% rm song1.txt
babbage% ls -l s*
-rw-r--r-- 1 amh16 cs114 111 Mar 15 17:45 song2.txt
babbage% more song2.txt
Row, row, row your boat
Gently down the stream
Merrily, merrily, merrily, merrily
Life is but a dream
[repeat]

symbolic links are indirect pointers - can span file systems, can refer to directories rather than just files, can have different permissions than the permissions on the link

use the -s flag to get symbolic links

Example:

babbage% ls -l s*
-rw-r--r-- 1 amh16 cs114 111 Mar 15 17:45 song1.txt
babbage% chmod go-r song1.txt
babbage% ls -l s*
-rw------- 1 amh16 cs114 111 Mar 15 17:45 song1.txt
babbage% ln -s song1.txt song2.txt
babbage% ls -l s*
-rw------- 1 amh16 cs114 111 Mar 15 17:45 song1.txt
lrwxrwxrwx 1 amh16 cs114 9 Mar 15 17:48 song2.txt -> song1.txt
babbage% diff song1.txt song2.txt
babbage% song2.txt
song2.txt: Permission denied
babbage% more song2.txt
Row, row, row your boat
Gently down the stream
Merrily, merrily, merrily, merrily
Life is but a dream
babbage% chmod u-r song1.txt
babbage% ls -l s*
--w------- 1 amh16 cs114 111 Mar 15 17:45 song1.txt
lrwxrwxrwx 1 amh16 cs114 9 Mar 15 17:48 song2.txt -> song1.txt
babbage% more song2.txt
song2.txt: Permission denied
babbage% vi song2.txt
add [repeat] as last line
babbage% diff song1.txt song2.txt
babbage% ls -l s*
-rw------- 1 amh16 cs114 111 Mar 15 17:51 song1.txt
lrwxrwxrwx 1 amh16 cs114 9 Mar 15 17:48 song2.txt -> song1.txt
babbage% rm song1.txt
babbage% ls -l s*
lrwxrwxrwx 1 amh16 cs114 9 Mar 15 17:48 song2.txt -> song1.txt
babbage% more song2.txt
song2.txt: No such file or directory

a symbolic link - links directories, spans file systems, allows you to always refer to /amd/babbage/a/amh16 as /home/amh16 (~ always resolves to /home/username)
note that, with a symbolic link, ls -l ~amh16 didn't give the contents of the directory - force that with ls -lL

babbage% ls -l ~amh16
lrwxrwxrwx 1 root root 20 Mar 15 17:37 /home/amh16 -> /amd/babbage/a/amh16
babbage% ls -l .. | grep amh16
drwxr-s--x 4 amh16 cs114 1024 Mar 14 16:39 amh16


tar - creates an archive of files (and directories) - can add files, can extract them all...

c to create archive
x to unpack archive
t to list contents of archive
v for verbose mode (to get progress info)
f to give output file name - need the f or else assumes refering to a tape drive (/dev/rmt/0:) and get a permission denied error

syntax: tar cf outfile files
syntax: tar xf tarfile

Example:

babbage% ls -l
drwx--s--x 2 amh16 cs114 1024 Mar 15 17:57 texts
babbage% ls -l texts
total 200
-rwx--x--x 1 amh16 cs114 2976 Mar 15 17:57 cathedral.txt
-rw------- 1 amh16 cs114 280 Mar 15 17:57 frost1.txt
-rw------- 1 amh16 cs114 157 Mar 15 17:57 frost2.txt
-rw------- 1 amh16 cs114 156 Mar 15 17:57 frost3.txt
-rwx--x--x 1 amh16 cs114 2527 Mar 15 17:57 hacker.txt
-rw------- 1 amh16 cs114 90238 Mar 15 17:57 table1.txt
-rwx--x--x 1 amh16 cs114 1122 Mar 15 17:57 whyVi.txt
babbage% tar cf texts.tar texts
babbage% ls -l texts.tar
-rw------- 1 amh16 cs114 104448 Mar 15 18:03 texts.tar
babbage% tar tf texts.tar
texts/
texts/cathedral.txt
texts/frost1.txt
texts/frost2.txt
texts/frost3.txt
texts/hacker.txt
texts/table1.txt
texts/whyVi.txt
babbage% cd texts HAVE TO CD, texts/* gets same results
babbage% tar cf texts2.tar *
babbage% tar tf texts2.tar
cathedral.txt
frost1.txt
frost2.txt
frost3.txt
hacker.txt
table1.txt
whyVi.txt
babbage% mkdir new1
babbage% mv texts.tar new1
babbage% cd new1
babbage% ls
texts.tar
babbage% tar -xf texts.tar
babbage% ls -l
total 206
drwx--s--x 2 amh16 cs114 1024 Mar 15 17:57 texts
-rw------- 1 amh16 cs114 104448 Mar 15 18:03 texts.tar
babbage% ls -l texts PERMISSIONS RETAINED
total 200
-rwx--x--x 1 amh16 cs114 2976 Mar 15 17:57 cathedral.txt
-rw------- 1 amh16 cs114 280 Mar 15 17:57 frost1.txt
-rw------- 1 amh16 cs114 157 Mar 15 17:57 frost2.txt
-rw------- 1 amh16 cs114 156 Mar 15 17:57 frost3.txt
-rwx--x--x 1 amh16 cs114 2527 Mar 15 17:57 hacker.txt
-rw------- 1 amh16 cs114 90238 Mar 15 17:57 table1.txt
-rwx--x--x 1 amh16 cs114 1122 Mar 15 17:57 whyVi.txt
babbage% mkdir new2
babbage% mv texts/texts2.tar new2
babbage% cd new2
babbage% ls
texts2.tar
babbage% tar xf texts2.tar
babbage% ls -l
total 404
-rwx--x--x 1 amh16 cs114 2976 Mar 15 17:57 cathedral.txt
-rw------- 1 amh16 cs114 280 Mar 15 17:57 frost1.txt
-rw------- 1 amh16 cs114 157 Mar 15 17:57 frost2.txt
-rw------- 1 amh16 cs114 156 Mar 15 17:57 frost3.txt
-rwx--x--x 1 amh16 cs114 2527 Mar 15 17:57 hacker.txt
-rw------- 1 amh16 cs114 90238 Mar 15 17:57 table1.txt
-rw------- 1 amh16 cs114 103936 Mar 15 18:09 texts2.tar
-rwx--x--x 1 amh16 cs114 1122 Mar 15 17:57 whyVi.txt

can get more complicated, and use options to add single files to your archive, rather than having to unwrap and rewrap the whole thing

also have zip and unzip, gzip and gunzip - more recent formats, commands work much as you expect