Logged in as: guest Log in
Lab 1 mcmillan / Version 7

The Unix Shell

January 18, 2013


Prelab (complete before lab)

  1. Download a secure terminal emulator and install it on your laptop. There are several available alternatives for windows including Secure CRT or SSH/SFTP Secure Shell, which are available from http://shareware.unc.edu. Another popular freeware alternative is Putty, which can be downloaded at http://www.chiark.greenend.org.uk/~sgtatham/putty/. If you are using Mas OS X or Linux, the builtin "Terminal" application will be sufficient. However, you'll need to make sure that the ssh client package has been installed, which can be tested by typing "ssh" at the command line prompt. If none of these options works for you, you can use a browser-based terminal application.

  2. Verify that your unix "cs login" is working by setting up a terminal session on "login.cs.unc.edu", or typing "ssh login.cs.unc.edu" at the Terminal command prompt. You should be asked to provide a username and password, and eventually you should arrive at a UNIX command prompt. If this succeeds, you can type "exit", and you have completed the prelab. Feel free to get a head start on the actual lab if you like.

Lab Tasks

The following exercises walk your through the basic UNIX commands. Scattered among the text are Checkoffs. You are expected to write on a sheet a paper, with your name on it, the answer to each question asked in the checkoffs. When you complete the Lab you should turn-in your checkoff list to the TA.
  1. Listing, creating, and traversing the filesystem.

    A filesystem is a means for organizing programs and data with a computers operating system. File systems are composed of several basic elements, including files and directories. Directories are analogous to folders in windows and Mac OS. They are used to organize files hierarchically.

    The following commands and techniques are used to explore the contents of, traverse, and create directories.

    1. Listing directories

      When you first login, your current working directory will be your home directory. Your home directory typically has the same name as your username, and it is where your personal files and subdirectories are saved. To list the contents of your home directory type

      % ls

      The ls command is short for list. There may be no files or subdirectories in your home directory, in which case ls will return without printing anything. In any case, now type the following:

      % touch myfile

      Now repeat the original ls command and you should see "myfile" among the listed files. The touch command is one of many ways to create a file. The file created by touch will have no contents, or a length of zero bytes. You can verify this by typing

      % ls -l

      This should output a more elaborate listing of the contents of your home directory. Each printed line will end with the filename, and before that the date of the file's creation and the length of the file in bytes. You will also notice several other things are printed including your user id. We will delve into these file properties later on.

      Note that we appended a -l to our previous ls command to change how it displayed its output. This is an example of a command-line option. Command-line options are often used to change the behavior of a command. For example, now type:

      % ls -a

      This should result in a listing of your home directory that includes several hidden files and directories, all of which are prefixed with a period or dot, ".". Next try this variant:

      % ls -F

      This directory listing appends characters to the end of each item in you directory to distinguish whether it is a file or a directory or somethings else. You can also include multiple options on the same command-line. Try it to see what you get.

    2. Making directories

      Next you can make a subdirectory within your home directory. We'll use this subdirectory to hold files that you'll create throughout the rest of this lab. Type the following commands:

      % mkdir lab1
      % ls -F

      The mkdir command creates to new subdirectory that is rooted in your home directory. You should see a difference between how the file "myfile" and the directory "lab1" are printed. If you list the directory contents using simply ls, you will not be able to distinguish which item is a file and which is a directory. Next try

      % mkdir myfile

      This should have resulted in some sort of error message indicating that "myfile" already exists as a file name. This illustrates that in UNIX, file names and directories within the same directory must have distinct names.

    3. Changing directories

      The command cd is used to change the current working directory. The current working directory may be thought of as the directory you are in, i.e. your current position in the file-system tree. To change to the directory that you just made, type

      % cd lab1

      Type ls to see its contents, which should be empty at this point. Next, enter the following series of commands:

      % touch bud
      % touch lee
      % ls -a -F

      Now the directory should have two files, bud and lee, and two directories "./" and "../". You may have noticed directories with these names when you were previously in your home directory. Well discuss them next.

      Checkoff 1. What commands would you use to make two directories in "lab1", one called "backup" and the second "data". Write down your answer as the first check off, and then test your commands.

    4. Shorthands for directories

      As you saw in the "lab1" directory, and perhaps in other directories, there were two special subdirectories, which you did not create, "." and "..", called dot and dot-dot respectively. In UNIX, dot means the current directory, so typing

      % cd .

      means to change to the current directory, which effectively means stay where you are (the "lab1" directory). This particular use of dot is not very useful, but using dot can save a lot of typing, as we shall see shortly. The directory dot-dot/ refers to the parent of the current directory, so typing

      % cd ..
      % ls -F

      takes you one directory up the hierarchy and back into your home directory. Now type

      % ls lab1
      % ls myfile

      The first command should have listed the contents of your "lab1" directory, which should have two files and two subdirectories in it, while the second should have only listed only the file "myfile" in your home directory. This illustrates another example how directories and files are treated by the ls command. Next type

      % cd lab1/data
      % ls -a

      The first command should have moved you into the subdirectory of "lab1" that you created previously. Notice how we specified a series of multiple directories to the cd command by separating each with a '/' (called slash). Once more "lab1" should be empty aside from '.' and '..'. While there type

      % touch thing1
      % touch thing2
      % cd ../backup
      % ls -a

      This cd command should have moved you into the empty subdirectory "backup". Here the cd command used dot-dot to get back up into "lab1" and "/backup" to go back down into the subdirectory from there.

      Checkoff 2. What series of commands would create files named "thing1", "thing2", and "thing3" in the "backup" subdirectory? What command would you then execute to return to "lab1", list its contents, and then list the contents of both "data" and "backup" while still in "lab1"?

      While in "lab1" enter

      % cd
      % ls -a

      Notice that typing cd with no argument returned you to your home directory. This is useful if you are ever lost in the file system. Now try the following commands:

      % cd lab1/data
      % ls ~/lab1/backup

      The path specified in the ls command uses the "~", or tilde to refer to your home directory. The net result is a listing of the contents of subdirectory backup while located in the directory "data". Thus, directory paths are specified via a list of directories separated by slashes, where the shortcut dot is used to specify paths relative to your current location, dot-dot is used to specify the parent of the current location, and tilde is used to specify a path relative to your home directory.

    5. Paths

      Path names enable you to specify arbitrary locations in the file system. You can also ask the UNIX shell to tell you where in the file system you currently are using the print working directory, or pwd. Try

      % cd
      % pwd

      The pwd command provides the full pathname starting from the file system's root. The reported pathname should look something like this, depending on how the system is configured:

      /home/userid or /afs/cs.unc.edu/home/userid

      which means that userid (your home directory) is in the directory "home", which itself resides under the "cs.unc.edu" node on our "afs" server.

      IMPORTANT NOTE: It is often the case that your home directory lies along some path that includes some directory called "home" (i.e. "./afs/cs.unc.edu/home/userid"). This directory-naming convention is commonly used as the parent for the home directories of all users on the system. In the following discussions, references to "your home" directories mean the directory that contians your files ".../home/userid", not the directory that serves as a parent for all user's home directories (".../home/").

      Checkoff 3. What command would generate a list of the contents of your home directory's parent when executed in any subdirectory?

  2. Creating, viewing, copying, moving, and removing files.

    1. Copying files

      The command cp is the command which makes a copy of the file specified as its first argument to path given as its second argument. What we are going to do now, is to take a file stored in an open access area of the file system, and use the cp command to copy it to your "lab1" directory.

      % cd ~/lab1
      % cp /home/mcmillan/public/byte.txt .

      Recall, in UNIX, the dot means the current directory. Thus, the above cp command copies the file quotes.txt to the current directory, keeping the name the same. Note: The directory /home/mcmillan/public/ is an area to which everyone in the department has read and copy access. If you are viewing this tutorial from outside the University, you can grab a copy of the file here. Use 'File/Save As..' from the menu bar to save it into your lab1 directory.)

      Checkoff 4. What command would you use to create a backup of "byte.txt" by copying it to a file called backup/byte.bak?

    2. Moving files

      The command mv is used to move a file rather than making a copy of it. Thus, after completion, the moved file will no longer be in its original location, but only at the location suggested by the second path argument. The mv command can also be used to rename a file, by moving it to the same directory, but giving it another file name. Change directories to your "lab1" directory (remember how?). Then, type

      % mv byte.bak backup

      Do an ls of both "lab1" and "backup" to verify that your move worked.

    3. Removing files and directories

      The rm command is used to remove (delete) files. As an example, we are going to create another copy of the byte.txt file then delete it. Inside your "lab" directory, type

      % cp byte.txt temp.txt
      % ls
      % rm temp.txt
      % ls

      The rmdir command is used to remove empty directories. Try the following:

      % rmdir backup

      This shoud have generated an error message since rmdir will not let you remove a non-empty directory.

      Checkoff 5. From your current location (you should still be in "lab1") what commmand would you use to remove the "thing1" file from the "data" subdirectory? Write down the command on your checkoff sheet and then execute it.

    4. Displaying the contents of a file

      Before starting the next section, you might want to clear the terminal window of earlier commands. At the prompt, type

      % clear

      This will clear all text and leave you with the "%" prompt at the top of the window.

      The command cat can be used to display the contents of a file on the screen. Type

      % cat byte.txt

      As you can see, the file is longer than than the size of the window, so the beginning of the file scrolls past making leaving only the las few lines.

      The command less writes the contents of a file onto the screen a page at a time. Type

      % less byte.txt

      Press the [space-bar] to advance a page, type [-] to move back a page, and [q] if you want to quit reading. As you can see, less is used in preference to cat for long files.

      The head command writes the first ten lines of a file to the screen. First clear the screen then type

      % head byte.txt

      Then type

      % head -5 byte.txt

      What difference did the -5 do to the head command?

      The tail command writes the last ten lines of a file to the screen. Clear the screen and type

      % tail byte.txt

      Checkoff 6. What command might you guess would view the last 15 lines of the file? Write down your guess and test it.

    5. Searching the contents of a file

      You can perform simple searching using the less command. For example, to search through "byte.txt" for the word "hexadecimal", type

      % less byte.txt

      then, while still in less (i.e. don't press [q] to quit), type a forward slash [/] followed by the search word.

      /hexadecimal

      As you can see, less finds and highlights the keyword. Type [n] to search for the next occurrence of the word.

      Another command for searching files is grep. It not only searches for specified strings, but it can also find any class of patterns called regular expressions. In fact, the name grep is an acronymn for a General Regular Expression Parser. Next clear the screen agian, and then type

      % grep assembler byte.txt

      As you can see, grep has printed out each line containing the word assembler. Or has it? Now try typing

      % grep Assembler byte.txt

      The grep command is case sensitive; it distinguishes between assembler and Assembler. To ignore upper/lower case distinctions, use the -i option, i.e. type

      % grep -i assembler byte.txt

      To search for a string containing spaces, you must enclose it in single quotes (the apostrophe symbol). For example to search for "16 bit", type

      % grep -i '16 bit' byte.txt

      Some of the other options of grep include:

        -v display those lines that do NOT match
        -n precede each matching line with the line number
        -c print only the total count of matched lines

      Try some of them and see the different results. Don't forget, you can use more than one option at a time, for example, the number of lines without the words assembler or Assembler is

      % grep -ivc assembler byte.txt

      Another handy little utility is the wc, which is short for word count. To do a word count on byte.txt, type

      % wc -w byte.txt

      To find out how many lines the file has, type

      % wc -l byte.txt

      To find out how many characters the file has, type

      % wc -c byte.txt

      To get all three simply type

      % wc byte.txt

  3. Redirection and Pipes.

    Most UNIX commands write to the "standard output". That is, they write to the terminal screen. Many UNIX commands receive their input from the standard input (they wait for input from the keyboard). There is also another place that they write to called "standard error". Standard error is generally reserved for error messages, and it too is set by default to the terminal screen.

    We have already seen one use of the cat command to write the contents of a file to the screen. Now type cat without specifing a file to read.

    % cat

    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 specifying a file to read, it reads the standard input (the keyboard), and then, after receiving an '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.

    1. Redirecting output

      We use the ">" symbol to redirect the output of a command. For example, to create a file called "list1" containing a list of friends, type

      % cat > list1

      Then type in names of your list of friends. Pressing [Return] after each one.

      Pauly D.
      Snooki
      JWoww
      ^D

      What happens is the cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called "list1". To read the contents of the file, type

      % cat list1

      Checkoff 7. How would you use the above method to create a second file of friends called "list2". That contains three lines with: "The Situation", "Sweetheart", and "Vinny"? Write down the series of commands and prompts, then do it.

      The redirection operator ">>" appends standard output to an existing file. For example to add one more item to the file "list1", type

      % cat >> list1

      Then extend your list of friends as follows:

      Deena Nicole
      ^D

      To view the contents of the appended file, type

      % cat list1

      You should now have two files. We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist as follows:

      % cat list1 list2 > biglist

      What this does is read the contents of list1 and list2 in turn, then outputs the text to the file biglist. To view the contents of the new file, type

      % cat biglist
    2. Redirecting input

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

      The command sort alphabetically or numerically sorts a list. Type

      % sort

      Then type in the names of some more friends. Press [Return] after each one.

      Kourtney
      Kim
      Khloe
      Robert
      ^D

      The output will be

      Khloe
      Kim
      Kourtney
      Robert

      Using < you can redirect the input to come from a file rather than enter it from the keyboard. For example, to sort your list of friends, type

      % sort < biglist

      and the sorted list will be output to the screen. To output the sorted list to a file, type

      % sort < biglist > slist

      Use cat to display the contents of the file "slist"

    3. Pipes

      To see who is also logged into the system with you, type the following command:

      % who

      One method to get a sorted list of names is to type

      % who > names.txt
      % sort < names.txt

      This is a bit clumsy since you have to use, and remember to remove, the temporary file "names.txt". What you really want to do is connect the output of the who command directly to the input of the sort command. This is exactly what pipes do. The symbol for a pipe is the vertical bar "|". For example, typing

      % who | sort

      gives the same result as above, but with less typing and overhead. To find out how many users are logged on, type

      % who | wc -l

  4. Wildcards, completion, and getting help.

    1. 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 "lab1" directory, type

      % ls list*

      This will list all files in the current directory starting with "list". Try typing

      % ls *list

      This will list all files in the current directory ending with "list". The character "?" will match exactly one character. So ls ?ouse will match files like house and mouse, but not grouse. Try typing

      % ls list?

      Checkoff 8. How might you use wildcards to generate a listing of the contents of all the subdirectories of "lab1"?

    2. Filename conventions

      As we mentioned previously, directories and files share a common name space. So the rules and conventions for naming files apply also to directories. In naming files, characters with special meanings such as / * & ? ~ 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 C code may be named with the ending .c, for example, "prog1.c". Then in order to list all files containing C code in your home directory, you need only type ls *.c in that directory.

    3. Getting Help

      There are on-line manuals which give 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

      Alternatively

      % whatis wc

      gives a one-line description of the command, but omits any information about options etc.

      When you are not sure of the exact name of a command,

      % apropos keyword

      will give you the commands with keyword in their manual page header. For example, try typing

      % apropos copy

  5. File access control and Job control (with and without AFS)

    1. File system security (access rights)

      In this section we discuss UNIX file permissions, both standard and those provided by a common file system extension AFS. In general, the AFS file permission system supersedes the standard UNIX permissions. You are referred to this article for more information on how AFS and UNIX file permissions interact, and you may search the web for more details.

      In your lab1 directory, type

      % ls -l

      You will see that you now get lots of details about the contents of your directory, similar to the example below.

      total 7
      drwx------ 2 mcmillan compsci 2048 Jan 16 21:41 backup/
      -rw-r--r-- 1 mcmillan compsci   53 Jan 18 21:33 biglist
      -rw------- 1 mcmillan compsci    0 Jan 16 21:39 bud
      drwx------ 2 mcmillan compsci 2048 Jan 16 21:40 data/
      -rw------- 1 mcmillan compsci    0 Jan 16 21:39 lee
      -rw-r--r-- 1 mcmillan compsci   22 Jan 18 13:00 list1
      -rw-r--r-- 1 mcmillan compsci   31 Jan 18 13:02 list2

      In the left-hand column is a 10 symbol string consisting of the symbols d, r, w, x, -, and, occasionally, s or S. If d is present, it will be at the left hand end of the string, and indicates a directory: otherwise - will be the starting symbol of the string. The 9 remaining symbols indicate the permissions, or access rights, and are taken as three groups of 3.

      • The left group of 3 gives the permissions for the file's or directory's owner
      • The middle group gives the permissions for a working group of people
      • The rightmost group gives the permissions for all others.

      The symbols r, w, etc., have slightly different meanings depending on whether they refer to a simple file or to a directory.

      Access rights on files.

      • r (or -), indicates read permission (or otherwise), that is, the presence or absence of permission to read and copy the file
      • w (or -), indicates write permission (or otherwise), that is, the permission (or otherwise) to change a file
      • x (or -), indicates execution permission (or otherwise), that is, the permission to execute a file, where appropriate

      Access rights on directories.

      • r allows users to list files in the directory
      • w means that users may delete files from the directory or move files into it
      • x means the right to access files in the directory. This implies that you may read files in the directory provided you have read permission on the individual files

      So, in order to read a file, you must have execute permission on the directory containing that file, and hence on any directory containing that directory as a subdirectory, and so on, up the tree.

    2. Changing access rights

      Only the owner of a file can use chmod to change the permissions of a file. The options of chmod are as follows:

      Symbol Meaning
      u
      user
      g
      group
      o
      other
      a
      all
      r
      read
      w
      write (and delete)
      x
      execute (and access directory)
      +
      add permission
      -
      take away permission

      For example, to remove read write and execute permissions on the file biglist for the group and others, type

      % chmod go-rwx biglist

      This will leave the other permissions unaffected. To give read and write permissions on the file biglist to all,

      % chmod a+rw biglist

      Checkoff 9. How would you change the access permissions of the file "byte.txt" so that it readable and writable by you and the compsci group but only readable by everyone else? Execute your command and use "ls -l: to check that the permissions have changed.

    3. Processes and Jobs

      An executing program is called a process and it is identified by a unique PID (process identifier). To see information about your processes, with their associated PID and status, type

      % ps

      A process may be in the foreground, in the background, or suspended. In general the shell does not return the UNIX prompt until the current process has finished executing. Some processes take a long time to run and hold up the terminal. Backgrounding a long process has the effect that the UNIX prompt is returned immediately, and other tasks can be carried out while the original process continues executing.

       

      To background a process, type an "&" at the end of the command line. For example, the command sleep waits a given number of seconds before continuing. Try it a follows:

      % sleep 5

      This will wait 5 seconds before returning the command prompt %. Until the command prompt is returned, you can do nothing but wait. To run sleep in the background, type

      % sleep 10 &
      [1] 6259

      The & runs the job in the background and immediately returns a prompt, allowing you do run other programs while waiting for that one to finish.

      The first line in the above example is typed in by the user; the next line, indicating the job number and PID, is returned by the machine. The job number is enclosed in square brackets, followed by the PID. Backgrounding is useful for jobs which will take a long time to complete.

      You can also background the current running foreground process. At the prompt, type

      % sleep 100
      ^Z
      Suspended
      % bg

      The process running in the foreground is suspended by holding down the [control] key and typing [z] (written as ^Z). Then it is restarted in the background by typing "bg".

    4. Listing suspended and background processes

      When a process is running, backgrounded or suspended, it will be entered onto a list along with a job number. To see your list of running processes type the following:

      % sleep 100; cat biglist &
      [1] 1957
      % sleep 50; grep 8080 byte.txt
      ^Z
      Suspended
      % jobs

      Your job list should look something like

      [1]   Running (sleep 100; cat biglist)
      [2] + Suspended sleep 50

      To restart (foreground) a suspended process, type

      % fg 2

      Typing fg with no job number foregrounds the last suspended process.

    5. Killing a process

      It is sometimes necessary to kill a process (for example, when an executing program is in an infinite loop). To kill a job running in the foreground, type ^C (control c). For example, run

      % sleep 100
      ^C

      The kill command kills a suspended or background process. For example, run

      % sleep 100 &
      % jobs

      If it is job number 2, type

      % kill %2

      To check whether this has worked, examine the job list again to see if the process has been removed.

      Alternatively, processes can be killed by finding their process numbers (PIDs) and using kill PID_number

      % sleep 100 &
      % ps

        PID   TTY   TIME COMMAND
      20077 pts/5 S 0:05 sleep 100
      21563 pts/5 T 0:00 netscape
      21873 pts/5 S 0:25 nedit

      To kill off the process sleep 100, type

      % kill 20077

      and then type ps again to see if it has been removed from the list.

      If a process refuses to be killed, uses the -9 option, i.e. type

      % kill -9 20077

  6. Other UNIX commands
    1. 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 .

    2. 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

    3. gzip and gunzip

      This command reduces the size of a file, thus freeing valuable disk space. For example, type

      % ls -l byte.txt

      and note the size of the file. Then to compress byte.txt, type

      % gzip byte.txt

      This will compress the file and place it in a file called "byte.txt.gz". To see the change in size, type "ls -l" again. To uncompress the file, use the uncompress command.

      % gunzip byte.txt.gz

    4. 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 *

    5. history

      The UNIX C shell keeps an ordered list of all the commands that you have enter. Each command is given a number according to the order it was entered.

      % history

      If you are using the C shell, you can use the exclamation character (!) to recall commands easily.

      To recall the last command

      % !!

      To recall third most recent command

      % !-3

      To recall 5th command in list

      % !5

      To recall the last command starting with grep

      % !grep

      You can increase the size of the history buffer by typing

      % set history=100

      Checkoff 10. Give a command that counts how many times the "ls" command was typed in your history.

Turn in your Lab 1 checkoff list.




Site built using pyWeb version 1.10
© 2010 Leonard McMillan, Alex Jackson and UNC Computational Genetics