Lab01: Unix commands and files

Terminal Screencapture

Background

The workstations in ISAT/CS 143 run Linux Mint, and the iMacs in ISAT/CS 248 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

  • Describe how a Unix file system is organized.

  • Develop familiarity with basic Unix commands.

  • Be comfortable working in a Unix environment.

Instructions

If you have never used the command-line before, we highly recommend you watch this 13-minute tutorial on YouTube before you begin the lab. For best results, follow along with your own terminal.

Download the  Lab01.txt worksheet and open/edit the file as plain text. Submit the answers to the selected questions(4,5,6,and 14) in Canvas by the deadline.


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 Hello.java, starting from the C: drive.

C:\Users\John\Desktop\Hello.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/Hello.java  (on OS X)

/home/john/Desktop/Hello.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 computer's file system.

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

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

Windows Screen capture

Windows File Diagram

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

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

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

UNix Screen capture

Unix File Diagram

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

5. QUESTION: You are now working in the /home/smith/ directory. What is the relative path to week1.txt

6. QUESTION: Where are the "games files" installed on this Unix-based system? Name the path.


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 /. Every user has a home directory, designated by ~. To see a list of files, use the "ls" command.

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

    Try the above commands in 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 Lab01

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

    rmdir Lab01

    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: Why is it useful to recall commands like this? Please give a specific example.

  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 hello.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 he" without pressing enter, and then press tab. The terminal should complete the word "hello.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 fromfile tofile Copy a file. The "from file" and "to file" can be relative or absolute file names.
    mv file directory

    mv oldname newname
    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?
  10. 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

Part 3(Additional - not required but recommended to look at before next class/lab): Java intro module 

 

SoloLearn is a relatively new (and free!) platform for learning how to program. They offer fun and interactive courses in a variety of languages, including Java.

 

To get started, visit sololearn.com and either download the app or click learn on the web. Sign up for an account (using Facebook, Google, or other email), start the Java Tutorial, and complete the "Basic Concepts" module.

 

Part of this module walks you through how to install Java and Eclipse on your own computer. These steps are optional, but highly recommended so that you can work from home (in addition to the Linux lab). Mac users can just ignore the Windows instructions for setting up the PATH (under "Installing the JDK").

 

We will be using JGrasp instead of Eclipse for the first ten weeks of CS 149. Here are detailed instructions for installing JGrasp in Lab 2. If you run into trouble getting things installed, just complete the rest of the module.  We will be working through an Intro Java lab next lab, but this tutorial is a supplement / extra work if you'd like to get started early.

  

Part 4: Optional advanced stuff

  1. 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?

  2. 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 ~]$ ls -l
    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

  3. 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 time

    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?

  4. Ready for more?

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

Back to Top