CS 139 Algorithm Development
Lab04B: Unix commands and files

Background

Over the past several weeks, you have been using a Unix-based operating system during the labs. The workstations in ISAT/CS 248 run Linux Mint, and the iMacs in ISAT/CS 250 run OS X. Both operating systems are based on Unix and come with a powerful suite of general purpose computing tools. Many of these tools are only available via the command line or "Terminal" interface. You will learn some of these commands this semester, and others throughout your career. Today we will focus on basic survival skills for navigating the command line and a Unix-based file system.

Objectives

The students will:

Instructions

If you have not already viewed the 13-minute tutorial on YouTube, please do so now. Write the answers to the questions below in a separate text file, and submit them via Blackboard. Your text file should contain your answers only; please do not copy this entire lab into it.

 


Part 1: File Systems and Paths

Most computer users are familiar with the Windows file system, which uses a "drive letter" and backslashes to identify files. The following example shows the "path" or location of the file PA1.java, starting from the C: drive.

C:\Users\John\Desktop\PA1.java

In Unix-based systems, there are no drive letters. All files (and disk drives) can be reached from the "root" directory, designated with a forward slash.

/Users/John/Desktop/PA1.java  (on OS X)

/home/John/Desktop/PA1.java  (on Linux)

All three of these examples are "fully qualified" or absolute paths, because they indicate the exact location of the file within the system.

You can alternatively specify a "relative" path, based on your current location in the file system. For example, if you are currently working in the "/home/John/" directory on Linux, you can use the name "Desktop/PA1.java" to identify that file.

Study the following example file system diagrams and answer the questions below.

  1. What do you find at the top of the Windows diagram? Describe which hardware it represents.

  2. What do you find listed below the "Program Files" directory? Describe what each file does.

  3. What is the path for the file john.doc? What is the path for the file 3dMaze.scr?

  4. In the Unix diagram, what is the absolute path to "donations" in the "jones" directory?

  5. You are now working in the /home/smith/ directory. What is the relative path to week.txt?

  6. Where are the "program files" installed on a Unix-based system? Name at least two paths.

Raise your hand when you have finished, and have a lab assistant check your answers.

 


Part 2: Command Line Basics

The following instructions will lead you through some basic exercises involving the command line. Open a new Terminal window to get started. Stop and ask your neighbor or a lab assistant for help anytime you don't understand something.

  1. Listing files

    The Unix file system is hierarchical. It starts at the root, designated by /. To see what's there, use "ls" (the directory list command).

    ls list the current directory
    ls ~/Desktop list your Desktop directory
    ls / list the root directory

    Try the above commands via your terminal.

  2. Creating directories

    The commands "mkdir" and "rmdir" will make a new directory and remove a directory from the file system, respectively. Try creating a new subdirectory of your home directory:

    mkdir Lab04B

    The "ls" command should now show a new directory named "Lab04B". Remove the new directory with:

    rmdir Lab04B

    Verify that it has been removed using the "ls" command.

  3. Current directory

    While you are logged in, you are attached to a "current directory" in the file system. To see what your current directory is, enter the "print working directory" command:

    pwd

    To change your current directory to a different directory, use the "change directory" command:

    cd <directory>

    The angle brackets are not part of the command, but indicate a placeholder for something you type. For example:

    cd / change to the root directory
    cd ~/Desktop change to the Desktop subdirectory within your home directory
    cd change to your home directory (default working directory)

    Try the above commands on your workstation. Do an "ls" command after each step to list the files in that directory.

  4. Parent directory

    The special names "." and ".." refer to the current directory and parent directory, respectively.

    ls . list current directory
    ls .. list parent directory
    ls ../.. list parent of parent directory

    Try the above commands on your workstation.

    QUESTION: If you are in your home directory, what is the location of ../..?

  5. Command history

    Unix keeps track of the commands you have recently entered. Use the up arrow to recall the previous command you wrote, and press enter to run it again. Now press up several times to go back several commands, followed by the down arrow to move forward to more recent commands.

    QUESTION: When have we used this technique before in this class? Why is it useful to recall commands like this?

  6. Nano text editor

    Nano is a simple text editor that can be used on most Unix platforms. When nano is invoked, a control-key menu appears at the bottom of the screen. Try creating a new file with nano and copy a small section of this lab into it:

    nano lab03b.txt

    Practice using the control keys to save (write), exit, search, cut (delete a line), uncut (paste the last cut line), go forward, and go backward.

    Raise your hand when you have completed this step, and demonstrate your nano skills to a lab assistant.

  7. Tab completion

    When working on the command line, you don't need to type every single character! Instead, use the tab key to complete the current word. For example, type "nano la" without pressing enter, and then press tab. The terminal should complete the word "lab03b.txt" for you.

    Try typing the following command with as few keystrokes as possible. (Hint: Use the tab key sometime after every slash.)

    nano /usr/share/info/dir

  8. File commands

    Here are some basic file manipulation commands. Practice using them with the file that you created in the last step.

    cp <from-file-name> <to-file-name> Copy a file. The "from" and "to" can be relative or absolute file names.
    mv <from-file> <to-directory>

    mv <old-filename> <new-filename>
    Move a file. This command can also be used to change the name of a file.
    rm <filename> Delete (remove) a file. You might want to make a copy of it first.
    cat <filename> Show the contents of a file (technically, catenate it to the standard output).
    less <filename> View the file contents one page at a time.
    Use the arrow keys and page up/down to scroll through the file.
    Press 'q' to quit (exit the less program).

    QUESTION: What commands would you type to do the following? (Test them out on your workstation.)

    1. Create a new subdirectory named step14.
    2. Copy a file from your home directory into step14.
    3. Rename the subdirectory step14 to delete_me.
    4. Delete the subdirectory. (This step requires multiple commands.)

  9. Man pages

    You now know something about ls, mkdir, rmdir, pwd, cd, nano, cp, mv, rm, cat, and less. Each of these programs have optional command line arguments. For example, type ls -l (ls with long formatting) in your terminal. You can use the "man" command to view the manual page for any command.

    man ls

    QUESTION: Use "man" to answer the following questions:

    1. What does clear do?
    2. What does grep do?
    3. What does man do?
Don't forget to upload your final answer file to Blackboard. You should have something written down for all of Part 1 and several items in Part 2.

 


Part 3: Optional advanced stuff

  1. Wildcards

    There are two "wildcard" symbols that can be used in most commands. Practice using them with the files in your directory. Create more files if necessary, using "cp" or "nano".

    * match any series of 0 or more characters
    ? match any 1 character

    For example:

    ls a* list files starting with 'a'
    rm *.log delete all files ending ".log"
    ls ??text.* list files with names containing any two characters followed by "text" and ending with any suffix

  2. Hidden files

    Files that have names beginning with '.' are not normally shown in directory listings. These files are typically used for configuration purposes, or to store personal settings for applications. To see the hidden files in a directory, add "-a" (all) to the ls command.

    ls -a

    QUESTION: What hidden directories and files exist in your home directory?

  3. Long listings

    To see additional details for the files in a directory, add the "-l" (long) option to the ls command.

    ls -l

    The directory listing that results will include several attributes for each file.

    [groverf@cs347 ~]$ ll
    total 44
    drwxr-x--- 2 groverf faculty 4096 Dec 11  2009 bin
    drwxr-x--- 2 groverf groverf 4096 Jul 26 11:20 Desktop
    drwxr-x--- 3 groverf faculty 4096 Jul 26 11:20 downloads
    -rwxr-x--- 2 groverf groverf 4096 Jul 26 14:15 cs347.txt
    drwxr-x--- 2 groverf groverf 4096 Aug 28  2009 tmp
    lrwxrwxrwx 1 root    root      33 Aug 12  2009 www -> /opt/tomcat/webapps/users/groverf
    

    The first 10 characters indicate the following information:
    * file type (d for directory, - for a regular file, l for link)
    * owner permissions (rwx)
    * group permissions (rwx)
    * public permissions (rwx)

    If the r, w, or x is present, that permission is on (access available); if it shows as a dash ('-'), that permission is off. For example, "rwxr-xr--" indicates that the owner has {read, write, execute} permissions, group members have {read, execute} permissions, and the public has {read} permission only.

    The remaining fields in the listing are:
    * number of links to the file
    * file owner
    * file group
    * file size
    * date last modified
    * file name

  4. Pipes and redirects

    One of the useful features of Unix is the ability to transfer the output of one command to the input of another. The symbol "|" (vertical bar) pipes output between commands.

    ls -l | sort -k 8 sorts output from "ll" by the 8th field of the output (time)
    ls -la | less displays the output of ls one page at a page

    The symbol '>' redirects standard output (normally directed to the screen) to a file.

    ls -l > list.txt sends output of the "ls" command to a file named "list.txt"
    ls -l | sort -k 8 > sortedlist sorts the directory listing and saves it in the file "sortedlist"

    QUESTION: How would you create a directory listing sorted by file size?

  5. Ready for more?

    Check out http://ss64.com/bash/ for a longer list of Unix commands.