Unix Tutorials For Beginners

prepared by M. Madan babu
I Yr PhD student under Dr. Sarah Teichmann
Structural Studies Division
MRC-Laboratory of Molecular Biology
1. ls - used to list files
This is a command which is analogous to the 'dir' command in dos. With this command, you can display the list fo files which you have
in a particular directory. There are various options associated with the ls command which you can invoke by jusy typing 'ls -option'
eg: %> ls -al
2. mkdir - used to create a new directory
This command is used to create a new directory. A directory is analogous to a 'folder' in windows.
eg: %> mkdir mrc-lmb

3. cd - used to change directory
This command is used to change directory. This is analogous to clicking a folder icon in windows.

Points to remember:
Each new directory created will automaticaly have two files
the '.' and the '..'
The '.' file is the one which points to the current directory
The '..' file is the one which points to the previous directory

Assume that this is a  directory structure:

-->root 
        --> mrc-lmb
                 --> users
                         --> madan     
                                -->rasmol 
                                -->insight
                         --> christine
                         --> sarah
                         --> cyrus
                 --> guest
                         --> CIMRusers
                         --> HGMPusers

when you login as 'madan'. You will automaticall be taken to the foldercalled 'madan' in the above directory structure.

to change the current directory to 'sarah', you will have to type the following
eg: %> cd ../sarah

what this does is the following:
     .. points to the immediate previous directory (which is users)
     /sarah changes the directory to 'sarah' 
    
to change the current directory to CIMRusers, you will have to type the following:
eg: %> cd ../../guest/CIMRusers

the command to change from CIMRusers to rasmol will be the following:
eg: %> cd ../../users/madan/rasmol

Thus the whole thing is like tracing a path from one directory to another with a set of rules which is:
 1. to go to the previous directory use '..'
 2. to go to the sub-directory use '/'

4. echo - used to display something on the screen 
This is a command which allows users to put in information in to a  particular file in combination with the redirection symbol '>'. 
eg: %> echo 'MRC-LMB is a non-profit organization' > filename

Points to remember:
The standard output (which you see on the screen) can be captured in to a  text file by this redirection process in to another file. The file to which
redirection is pointed will be automatically created if it does not exist.

There are two types of redirection:
a. Overwrite - This will overwrite an existing file - '>'
b. Append  - This will add the information at the end of an exnisting file - '>>'

eg: %> type 'this will overwrite' > filename
eg: %> type 'this will append'  >> filename


5. type and cat - used to list the contents of a file
This is a command which allows the user to type out the contents of a text files. Please do not use type to list contents of a binary file
(binary files are executable files in linux/unix).
eg: %> type filename
eg: %> cat filename > filename2

6. cp - used to copy files
This command is used to copy files 
eg: %> cp filename1 filename2
eg: %> cp filename1 ./directory/filename2


7. mv - used to rename files
This command is used to rename files and to move files from on directory to another
eg: %> mv filename1 filename2
eg: %> mv filename1 ./directory/filename2


8. grep - global regular expression printer - pattern matching 
This a very powerful command which searches for patterns in your text file.
eg: %> grep 'mrc' filename
this will list all lines which has 'mrc' in the file 'filename'
eg: %> grep -i 'mrc' filename
case insensitive pattern search
eg: %> grep -v 'mrc' filename
this will list all lines which does not match the pattern

Point to remember:
A pattern is a word, set of words which you are searching for

9. more - used to display the contents in the form of a page
This command will display the contents of the file in a page format and the user can scroll the pages by pressing any key. This is also
powerful as it can search for patterns in a file. 
eg: %> more filename

Points to remember:
You can use the standard output of one command as the standard input for the  other command by using the '|' symbol.
eg: %> cat filename | more

here, the output of 'cat filename' will be used as the input for the more command.

10. head - used to list the first 10 lines in a file
eg: %> head filename
eg: %> head -15 filename 
the last command will display the first 15 lines of the file


11. tail - used to list the last 10 lines in a file 
eg: %> tail filename
eg: %> tail -15 filename
the last command will display the last 15 lines of the file

12. paste - used to paste 2 files laterally
eg: %> paste file1 file2 > pastedfile
eg: %> paste -d';' file1 file2 > pastedfile
the last command will paste two files delimited by a ';'

13. du - used to get the amount of disk space used
The amount of harddisk space used is mentioned in terms of Kb. 1000 Kb forms 1 Mb
eg: %> du

14. rm - used to remove unwanted files
This command is analogous to 'delete' in dos and in windows. The only problem is that there is no 'recycle bin' which temporarily stores the file. so please be ABSOLUTELY 
SURE BEFORE YOU DELETE A FILE!!. 
eg: %> rm filename
eg: %> rm -rf directory
the last command will remove all files in that particular directory and will also remove the directory.

15. sort - use to sort a file
This command will sort a file either numerically or alphabetically depending on the data. 
eg: %> sort filename
eg: %> sort -n filename (numerically)
eg: %> sort +1 -n filename (numerically based on the second column. Remember the first column is 0)

16. cut - used to cut specific columns vertically
This is similar to head and tail but functions in a 'vertical' manner.
eg: %> cut -c2-5 filename 
this means: cut column numbers from 2 to 5 (all inclusive) from the file filename.
eg: %> cut -f3-4 filename
if the filename has field delimiters, then individual fields can be cut out using the -f option.

Point to remember:
A file can have many fields and the basic unit of a field is a column. Fields are a set of columns which are delimited with a special symbol. 
madan;SS;MRC-LMB
christine;SS;MRC-LMB
This particular examples has 3 fields which are 'delimited' by a ;
so to get field number three, you should type
eg: %> cut -f4 -d';' filename

17. tr - used to translate alphanumeric characters
This command is used to translate one character to another. If you want all upper case to be translated to lower case, then you have to type the following:
eg: %> tr A-Z a-z < filename
eg: %> cut -f2 -d';' filename | tr a-z A-Z
the last command will cut field number 2 from the file 'filename' which has fields delimited by a ';'. The standard output is given to the translate command which
converts all the lower case to upper case letters.


18. find OR locate - finding for a file 
This command is used to find the directory in which a particular file you are looking for is located in. This is similar to the windows 'search' program.
eg: %> find . -name filename* -print
eg: %> locate madan*

Points to remember:
1. * match an arbitrary length
2. ? match a single character
3. [...] match any name in braces
4. [^..] match any name not in braces
5. ^pattern do not match pattern

eg: %> ls mrc*
will list files with names: mrc, mrc-lmb, mrcusers, mrc-dunn, etc
eg: %>ls mr?
will list files with names: mrc, mri, mr[any thing here which is of a 
single alphabet/number]]
eg: %>ls [Mm][Rr][Cc]
will list the files with names: Mrc, MRc, MRC, etc