2.1 Redirection
Most UNIX commands will write to the standard output (that is, they write on to screen), and many take their input from the standard input (that is, they read it from the keyboard). The standard error by default, will aso write to the screen.

We have seen one use of the cat command is to write the contents of a file onto the screen.

Now type cat without specifing a file to read

% cat [Return]
Then type a few words on the keyboard and press the [Return] key.

Finally hold the [Ctrl] key down and press d (written as ^D for short) to end the input.

What has happened?

If you run the cat command without specifing a file to read, it reads the standard input (the keyboard), and on receiving the "end of file" (^D), copies it to the standard output (the screen).

In UNIX, we can redirect both the input and the output of commands.


 
2.2 Redirecting the Output
We use the > symbol to redirect the output of a command. For example, to create a file called amino1.seq containing a list of amino acids, type
% cat > amino1.seq
Then type in the names of some amino acids. Press [Return] after each one.
ALA  A
ARG R
PHE  F
ASN N
^D (Control D to stop)
What happens is the cat command reads the standard input (the keyboard) and the > redirects cat's output, which normally goes to the screen, into a file called amino.seq

To read the contents of the file, type

% cat  amino1.seq

 

Exercise

Using the cat, create another file called amino2.seq  containing the following residues: ASP D, CYS C, VAL V, GLU E (Note each will contain a 3 letter and one letter codes as above)
The form >> appends standard output to a file.
 

You should now have two files: amino1.seqand amino2.seq.  We will now use the cat command to put amino2.seqinto amino1.seq. Type

% cat amino2.seq  >> amino1.seq
What this is doing is reading the contents of amino2.seq  and  then appending the text to the file amino1.seq

To read the contents of amino1.seq, type

% more amino1.seq

 
2.3 Redirecting the Input
We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

% sort [Return]
Then type in the names of some amini acids. Press [Return] after each one.
ALA
PHE
CYS
^D (control d to stop)
The output will be
ALA
CYC
PHE
Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type
% sort < amino1.seq
and the sorted list will be output to the screen.

To output the sorted list to a file, type,

% sort < amino1.seq > amino1_sorted.seq
Use cat to read the contents of the file amino1_sorted.seq
 
2.4 Pipes
To see  recent logins of users and terminals on your system computer type
% last
One method to get a sorted list of user names is to type,
% last > names.txt
% sort < names.txt
This is a bit slow and you have to remember to remove the temporary file called names.txt when you finished. What if you really want to do is connect the output of the last command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is |

For example, typing

% last | sort
will give the same result as above, but quicker and cleaner.

To find out how many files/directories in a directory

% echo * | wc -w   (get total number of files and directories)
% echo */ | wc -w  (get total number of directories)
% echo *.* | wc -w (get total number of files with extension)


To find out how many users are logged on, type

% who | wc -l


 
Summary
 
command > file redirect standard output to a file
command >> file append standard output to a file 
command < file redirect standard input from a file
command1 | command2 pipe the output of command1 to the input of command2
cat file1file2 > file0 concatenate file1 and file2 to file0
sort sort data
last list users recently logged in




 
2.5 Wildcards
The character * is called a wildcard, and will match against none or more character(s) in a file (or directory) name. For example, in your pdb directory, type
% ls *.aln
This will list all files in the current directory ending with ".aln"

The character ? will match exactly one character.
So ls ?ouse will match files like house and mouse, but not grouse.
Try typing

% ls ?

 
2.6 Filename conventions
We should note here that a directory is merely a special type of file. So the rules and conventions for naming files apply also to directories.

In naming files, characters with special meanings such as /,*,&,% etc., should be avoided. Also, avoid using spaces within names. The safest way to name a file is to use only alphanumeric characters, that is, letters and numbers, together with _ (underscore) and . (dot).

File names conventionally start with a lower-case letter, and may end with a dot followed by a group of letters indicating the contents of the file. For example, all files consisting of Perl code may be named with the ending .pl, for example, prog1.pl . Then in order to list all files containing Perl code in your current directory, you need only type ls *.pl in that directory.

Beware: some applications give the same name to all the output files they generate. For example, some compilers, unless given the appropriate option, produced compiled files named a.out. Should you forget to use that option, you are advised to rename the compiled file immediately, otherwise the next such file will overwrite it and it will be lost.


 
2.7 Getting Help

On-line Manuals

There are on-line manuals which gives information about most commands. The manual pages tell you which options a particular command can take, and how each option modifies the behaviour of the command. Type man command to read the manual page for a particular command.

For example, to find out more about the wc (word count) command, type

% man wc

Whatis

When you are not sure of the exact name of a command,
% whatis keyword
will give you the commands with keyword in their manual page header. For example, try typing
    


 
Summary
 
* match any number of characters
? match one character
man command read the online manual page for a command
whatis command brief description of a command
apropos keyword match commands with keyword in their man pages
 
2.8 Other useful UNIX commands

cut

A column manipulation command. Very good at working with columns of any text files.

set

Widely used for text string substitutions of a text file.

echo

It just echoes arguments what you put in

quota

All students are allocated a certain amount of disk space on the file system for their personal files, usually about 5 Megabyes (equivalent to 4 floppy disks worth). If you go "over-quota", you are given 7 days to remove excess files.

To check your current quota and how much of it you have used, type

% quota -v

df

The df command reports on the space left on the file system. For example, to find out how much space is left on the fileserver, type
% df .

du

The du command outputs the number of kilobyes used by each subdirectory. Useful if you have gone over quota and you want to find out which directory has the most files. In your home-directory, type
% du

compress

This reduces the size of a file, thus freeing valuable disk space. For example, type
% ls -l  blast.txt
and note the size of the file. Then to compress t, by typing
% compress blast.txt
This will compress the file and place it in a file called science.txt.Z

To see the change in size, type ls -l again.

To uncomress the file, use the uncompress command.

% uncompress science.txt.Z

gzip

This also compresses a file, and is more efficient than compress. For example, to zip blast.txt, type
% gzip blast.txt
This will zip the file and place it in a file called science.txt.gz

To unzip the file, use the gunzip command.

% gunzip blast.txt.gz

file

file classifies the named files according to the type of data they contain, for example ascii (text), pictures, compressed data, etc.. To report on all files in your home directory, type
% file *

history

The C shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.
% history (show command history list)
If you are using the C shell, you can use the exclamation character (!) to recall commands easily.
% !! (recall last command)

% !-3 (recall third most recent command)

% !5 (recall 5th command in list)

% !grep (recall last command starting with grep)

You can increase the size of the history buffer by typing
% set history=100
Shell Scripting:

A very good introdution can be found at:
http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/unixscripting/unixscripting.html
http://www.atmos.albany.edu/das/classes/atm450/scripting.htm