Annotated UNIX Session

Many UNIX applications like the bash shell, have a configuration file that contains information to modify the behavior of the application. These are things like user preferences and are similar to the .ini files used in Windows. These files are commonly called by the name of the application appended with the letters 'rc'. They are usually prepended with a dot to make them invisible to a normal ls command. So the one for the bash shell is called .bashrc. If you want to add your own preferences, like aliases or other settings, you simply add these to the .bashrc file in your home directory.

Here is the contents of the bashrc file after we added a couple of aliases.

----> cat .bashrc
# .bashrc

# User specific aliases and functions

# global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi
alias ls="ls -CF --color=none"
alias rm="rm -i"

An alias is simply a new name for an existing command. For example, the rm command, which removes files, and adding the -i option. This option makes rm ask before deleting a file. Since there is no recycle bin in UNIX, this can be a simple way to prevent accidents. But since it is tedious to always type the -i option, we can use an alias.

In this case,

alias rm="rm -i"
After the command name, alias, we have a statement of the form 'name="value"'. The name can be any string, including the name of an existing command. Here we are using the rm command name itself. After the = sign comes the value. The value part is what you want the system to do when you type the name part. The value gets substituted for the name. Here we put "rm -i" so t hat whenever we type rm, it is as if we had typed rm -i. So, when we type
rm joe
the system sees
rm -i joe
.

Here is an example of our using the alias for ls. This is set up so that when we type ls, we get the color coding turned off, [--color=none] It shows executable files with an asterisk(*)[-C] appended and it shows directories with a slash (/)[-F] appended.
----> ls
317  3172  joe	public_html/  three  two

The alias command by itself, without arguments, lists the current set of aliases.

---->  alias
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls="ls -CF --color=none"
alias rm="rm -i"
alias mc='. /usr/share/mc/bin/mc-wrapper.sh'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

The rmdir command only removes empty directories, as we can see in this example.

----> mkdir comp217
----> cd comp217
----> ls
----> mkdir asg1
----> cd asg1
----> pwd
/space/home/staff/d99110215/comp217/asg1
----> cd ..
----> pwd
/space/home/staff/d99110215/comp217
----> mkdir asg2
----> ls
asg1/  asg2/
----> pwd
/space/home/staff/d99110215/comp217
----> rmdir asg2
----> ls
asg1/

After this, the directory asg2 was removed because it was empty. Now let's put something in asg1 and try to remove it.

----> cd asg1

If we redirect into a file, but don't actually send anything, an empty file gets created.

----> > joe
----> ls -l
total 0
-rw-rw-r--    1 d99110215 d99110215        0 Mar 17  2004 joe

We can use the echo command to output messages. In this case, we want to put something into the file called joe.

----> echo "hello there" > joe
----> ls -l
total 4
-rw-rw-r--    1 d99110215 d99110215       12 Mar 17 15:44 joe
----> cat joe
hello there

Now the file joe has 12 bytes in it. We used the cat command to see the contents. If we try to remove the asg1 directory again, it should fail.

----> cd ..
----> 
----> rmdir asg1
rmdir: `asg1': Directory not empty

The cd command without any options takes us to our home directory. The '-' option by itself, takes us back to the directory we were just in. Doing it again takes us back to the original place. This is handy when we are working alternately in two directories.

----> cd ..
----> pwd
/space/home/staff/d99110215
----> cd public*
----> ls
comp217.zip  comp313/
----> cd -
/space/home/staff/d99110215

Now back to removing the directory asg1. We have to remove the files in it first.

----> cd comp217
----> cd asg1
----> ls
joe
----> rm joe
rm: remove regular file `joe'? 

If we type anything other than a 'y', the file is not deleted. Even typing just the 'Enter' key will cancel the deletion.

----> ls
joe

This time, we typed 'y' and the file was deleted.

----> rm joe
rm: remove regular file `joe'? y
----> ls

Now we could delete the directory is we wanted to.

in UNIX, searching a file for a certain word or phrase is pretty easy. We use th grep (General Regular Expression Program). A regular expression is a pattern that is compared against some text to see if it matches. It can be very powerful and expressive and can be used to extract parts of the text. We will look a a couple of simple but common uses.

----> cd ../public_html/comp313
----> ls
asgs/	    ect270.pl  index2.html  nav.js	 sched.html
bcard.html  examples/  index.html   notes/	 syll.html
ect270.cgi  images/    links.html   Osched.html  teach.css

In this directory, we want to find all the HTML files that contain JPEG images. We will look for uses of the extension 'jpg' that is usually use to mark JPEG files. The grep command, in this form, takes two arguments. THe first is the pattern to search for, in this case just the letters jpg and the second is the file or files to search. We will use the '*' operator to get all the HTML files.

----> grep jpg *.html
bcard.html:<IMG SRC="images/archie.jpg" ALT='Picture of Kent Archie'>
syll.html:<img src="images/bookcover.jpg" alt="Book Cover" height="119" width="91">

Two files matched, their names appear before the colon (:) in each line. grep also prints the line the pattern appeared on. grep will match the jpg anywhere in a string it appears, not just after the period in a file name.

In this next example, we will search for any word the starts with 'im' and ends with 'es'. In particular, we are looking for the word 'images', but this would also match words like 'impossibles".

----> grep im*es *.html
bcard.html:<IMG SRC="images/archie.jpg" ALT='Picture of Kent Archie'>
Osched.html:<a href="notes/tags3.html#images">Images
syll.html:<img src="images/bookcover.jpg" alt="Book Cover" height="119" width="91">

Note that two of the lines had the word 'alt' in them, one in upper case and one in lower case.

----> grep alt *.html
syll.html:<img src="images/bookcover.jpg" alt="Book Cover" height="119" width="91">

This only matched the lower case one because the pattern was in lower case. We can use a more complicated pattern to match both. The letters inside the square brackets ([]) contain a set of characters to match. So this pattern matches any three letter word where the first letter is either 'A' or 'a', the second is either 'L' or 'l' and the third is 'T' or 't'.

----> grep [Aa][Ll][Tt] *.html
bcard.html:<IMG SRC="images/archie.jpg" ALT='Picture of Kent Archie'>
syll.html:<img src="images/bookcover.jpg" alt="Book Cover" height="119" width="91">