B227 Tutorial: Introduction to Linux (external students only)

Motivation: 

To get you familiar enough with LINUX to
1.	Execute commands
2.	Navigate around the LINUX file system
3.	Modify files

The reason for this is because you will be doing two things in Assignment 2 that requires the above:
1.	Run network tools (as commands) and interpret the results
2.	Determine how to change configuration information, either by running commands or by editing configuration files.

If you are already familiar with LINUX:

If you have prior experience with LINUX, these first two tutorials will be simple (even trivial). Go through the instructions and make sure you know all of the commands given. They will be useful later.
The instructions (and these first two tutorials) are geared towards those students with no prior experience with LINUX.

This is not a unit to teach you about LINUX or about operating systems. The point is only to get to the stage where you can do the three operations above.

What is on the different disks?

Floppy disk: 	DOS and the LINUX kernel
CD: 		The read-only part of the LINUX file system
Hard disk:	The writable  part of the LINUX file system (the part you will be making changes to)

EXERCISES:

•	In the instructions below, the words in Courier font, like this, you can type at the command line and execute.
•	The following instructions are modified from http://floppix.ccai.com/labs.html

THE COMMAND LINE: 

Entering commands: 

Commands must be entered in lowercase. For example: 
type: cal and press [return]
This displays a calendar for the current month 

Command arguments: 

Many commands are used with one or more arguments. For example: 

   1. cal (no arguments) prints a calendar for the current month.
       
   2. cal 2000 (one argument) prints a calendar for the year 2000.
       
   3. cal 10 2000 (10 and 2000 are both arguments) prints a calendar for October, 2000.  

Command options: 

You can also specify options which modify the behaviour of a command. Options appear immediately after the command, they are usually entered as a - followed by a single letter. For example: 

   1. cal -j 2000 (j is an option) produces a julian calendar for the year 2000.
       
   2. cal -y (y is an option) prints a calendar for the current year.
       
   3. cal -yj  (2 options) prints a julian calendar for the current year.  

Getting help: 

The traditional unix help system is the man (short for manual) command. To get help about the cal command, type: man cal. 

Once a man page is displayed, the following commands navigate the document: 

     [SpaceBar] - display the next screen 
     [Enter] - advance 1 line 
     b - go backwards 
     /xyz - search for the string xyz 
     q - quit 

Command history: 

The bash shell saves all of the commands you've entered. When you logout, these commands are saved in a file in your own home directory called .bash_history so that the command history is even preserved between login sessions. The command history can be used in a number of ways, but the simplest is to use the up-arrow key to recall previous commands. Once the command is displayed, you can execute it immediately by pressing [enter] or modify the command and then press [enter]. 

Command completion: 

If you are entering long commands or filenames, you seldom have to type the entire string; type enough letters for bash to recognize the text and then press the [tab] key. Bash will fill in the remaining letters for you. If there is more than one option, the shell will beep. Pressing the [tab] key a second time will produce a list of possible matches for the text you have entered.

Ending commands: 

If for whatever reason you make a mistake and are caught in the middle of a command execution, you can kill it by typing Ctrl-C. This usually gives a signal to the command process to terminate itself.

THE FILE SYSTEM:

Refer to this diagram of the filesystem for the examples below. Note that this is a diagram I downloaded and does not reflect the filesystem you have on your CD precisely. It is just to illustrate the concepts below. 

Path: 

   1.An absolute path is the exact location of a file or directory starting from the root directory. For example: the absolute path to the abc directory is /home/abc .
     An absolute path always starts with /
       
   2.A relative path gives the location of a file or directory relative to the current directory. If the working directory is /home, then: 
          /home/abc is the absolute path to the abc directory 
          from /home, the relative path to the abc directory is: abc
            
   3.Some special symbols may be used in relative paths.
     .  is the current directory
     ..  is the parent directory
     ~  is your own home directory.
     If the working directory is /home/abc, then .. is a relative path to /home.  

Commands: 

   1. pwd - This command prints the working directory. Use it to find out where you are in the directory tree.
       
   2.cd - This command changes to a specified directory (access to some directories may be denied).
     eg: cd /
     eg: cd /home/xyz
     eg: cd ..

DIRECTORY LISTINGS

The ls command - examples of common options:
The ls command is used to show the contents of a directory. Some of the options of the ls command are: 

   1. ls
     List the contents of the current directory
       
   2. ls /etc
     List the contents of the /etc directory
       
   3. ls -a
     Display all entries in the current directory including those beginning with a period. File and directory names that start with a period are "hidden" files in unix; they do not appear in ordinary directory listings.
       
   4. ls -l
     Display a detailed listing of the current directory; one line of data for each entry
       
   5. ls --help
     The -h option is the "help" option, this gives a list of all of the options for the ls command and brief explanations.
       
   6.To get a list of all of the other options of ls, use the man command. [ man ls ] 

Some of the fields in a directory listing entry: 

There are a number of things that appear in a unix directory listing: 

   1.Permissions:
     Users and the system administrator need a way to control the way other users can access files and directories. The standard unix filesystem does this with
     permissions. Each file has read, write and execute permisssions set for the owner, the group and then all other users.
       
   2.Links: 
     Users can create links to existing files. The filesystem keeps track of the number of links.
       
   3.Owner and group:
     Unix is a multi-user operating system. For each file and directory, the filesystem keeps track of the file owner and group.
       
   4.Size in bytes:
       
   5.Date:
     The unix filesystem actually maintains 3 dates for each file: the last access date, the last modification date (the last date that the file contents were modified) and the last change date (the last date that the file "inode" or directory entry was modified). The date that appears in a standard directory entry in the last modification date.
       
   6.Filename:

A sample directory entry:
       -rwxrw-r-- 1 xyz staff  12 Mar 7 12:35 runme  

     -   This is an ordinary file 
     rwx   The permissions for the owner (xyz): read, write, execute 
     rw-   The permissions for the group  (staff): read, write, not execute 
     r--   The permissions for others: read, not write, not execute 
     1   The number of links to the file 
     xyz   The owner 
     staff   The group 
     12   The size (in bytes) 
     Mar 7 12:35   The last time that the file was modified 
     runme   The name of the file

SOME DIRECTORY COMMANDS:

Some directory commands: 

   1. pwd - print working directory (from the lab on navigating the filesystem)
       
   2. cd - change directory (from the lab on navigating the filesystem)
       
   3. ls - display a directory listing (from the lab on directory listings)
       
   4. mkdir - This command creates a new directory. Examples: 
          mkdir /etc/sample
            
   5. rmdir - This command removes an existing directory. The directory must be empty before it can be removed. Example: 
          rmdir /etc/sample

* You can’t do mkdir and rmdir in the part of the file system which resides on the CD. You can use mkdir and rmdir in the /etc directory because it resides on your hard disk.

FILE COMMANDS:

Some file commands: 

     cat - display file contents
          cat  /etc/passwd

     more - display file contents one screen at a time. Keystroke commands for more: 
          [SpaceBar] - display the next screen 
          [Enter] - advance 1 line 
          b - go backwards 
          /xyz - search for the string xyz 
          q - quit
          
          more  /etc/passwd
   
     less - a more powerful version of more
          less  /etc/passwd
       
     head - display lines from the beginning of a file The default is 10 lines. 
          head  -5  /etc/passwd
          displays the first 15 lines of /etc/passwd 
            
     tail - display lines from the end of a file 
          tail  -5  /etc/passwd
          displays the first 15 lines of /etc/passwd

FINDING FILES:

The find command searches for files in the filesystem by name, owner, size, date, ... 

Examples: 

  find  /home -name readme
     This starts the search in the directory /home and looks for a file named readme. 

     find  /  -user danny
     This starts the search at the root directory and looks for all files which belong to danny. 

     As find scans through the directory tree, there will be directories which you do not have permissions to search. The find command will not scan these directories but will display permission denied messages on standard error. To suppress the permission denied messages, redirect standard error to /dev/null. 

     find  /  -mtime  -1  2>  /dev/null
     This searches for all files that were modified in the last day (24 hours) starting the search in the root directory and sending all 'permission denied' messages to the bit bucket 

GREP:

The grep command searches the contents of a file for lines that contain a certain pattern. 

Examples: 

     grep  adm  /etc/passwd 
     Display the lines from the file /etc/passwd that contain the string adm. 

     grep  -v  root /etc/passwd
     Display the lines from the file /etc/passwd that do not contain the string root. 

     grep  -n  root /etc/passwd
     Display line numbers and the lines from the file /etc/passwd that contain the string root. 

     grep  ^f  /etc/passwd
     Display the lines from the file /etc/passwd that start with f. 

     grep  h$  /etc/passwd
     Display the lines from the file /etc/passwd that end with h.

STANDARD I/O AND REDIRECTION:

    1. Standard I/O: most unix commands are setup to: 
          get input from standard input (generally the keyboard) 
          send output to standard output (the display screen) 
          send error messages to standard error (also the display screen). 

    2. Redirecting output. When you redirect standard output, the data that would normally appear on the screen is stored in a file instead. Redirect output to a file using > or >> 

          echo “first line” > /etc/flist 
          puts the word “first line” in the file /etc/flist ; if flist already exists, it is overwritten ; echo is a command which prints out whatever arguments you give it

          * You can view the file /etc/flist using the cat command (see previous tutorial notes)

          echo “another line” >> /etc/flist
          appends the another line to the file /etc/flist; if flist does not exist, it is created 

         * View the file again using the cat command

     3. Redirecting input. When you redirect standard input, any data that would normally be entered from the keyboard is obtained from a file instead. Redirect input from a file using < 

          cat  <  /etc/passwd 
          sends the contents of /etc/passwd to the cat command, which normally reads from the keyboard.

     4. Redirecting standard error. Redirect standard error using 2>
 
          ls –l /nowhere  2>  /dev/null 
          tries to do a listing of the file /nowhere, and sends any error messages to /dev/null; /dev/null is a rubbish tip for output which is no longer needed. Compare with:
    ls –l /nowhere

PIPES AND FILTERS: 

   1. Pipes. A pipe takes the output from one command and uses it as input to the next command.  Example: 
          ls  -l  /etc | more

          display a long listing of the /etc directory and pipe the output through the more command so that it is displayed on the screen one screen at a time.
            
   2. Remember: 
          to send the output of a command to a file, use redirection 
          to send the output of a command to another command, use piping
             
   3. Commands that are used in pipelines to modify the output from other commands are being used as filters. All of these commands can be used on their own as well. Commands that are frequently used in pipelines include: grep, sort, wc
       
   4. wc: Count bytes, words and lines 

          wc  /etc/passwd
          Count the number of bytes, words and lines in /etc/passwd 

          wc -l  /etc/passwd
          Count the number of lines in /etc/passwd
 
          wc -w  /etc/login.defs
          Count the number of words in /etc/passwd
            
   5. sort: sort lines in a file 

          sort  /etc/group
          Sort the contents of /etc/group 

          sort  -r  /etc/group
          Sort the contents of /etc/group in reverse order 

          ls  -l  |  sort -k5n
          Sort the output of ls -l on the size of the files. k5 specifies sort starting on field 5 and n selects a sort in numerical order
            
   6. More examples: 
          What command will count the number of files in the directory var/spool/mail?
            
          Answer:  ls  /var/spool/mail | wc -w
            
          What command will count the number of users whose login shell is /bin/bash? (Recall that the login shell is the last entry in the line for each user in the file /etc/passwd)
            
          Answer:  grep  /bin/bash$  /etc/passwd  |  wc  -l
            
          What command will display a list of everyone who is logged on sorted by their usernames?
            
          Answer:  who | sort

TEXT EDITING:

   1. In your system is a text editor called pico. Try using it to create and edit a file.

    pico /etc/temp.txt    

   2. The features of pico are available as online help in pico. Try them.

AT THE END OF THE SESSION, PLEASE DELETE ALL THE FILES YOU HAVE CREATED IN THE DOSC DIRECTORY BY DOING THE FOLLOWING:

   cd  /etc
   rm  flist  temp.txt