2. Tools - Unix Commands

Unix Commands

Unix Commands

Note

There are no solutions to this first activity as it is meant for hands-on practice and there are various ways to achive the correct outcome.

After class, we recommend practicing this activity multiple times until you feel comfortable creating/moving/copying files in Unix.

Lesson Objectives

This exercise is designed to get you acquainted with basic Unix commands. You will create regular files and directory files and learn how to navigate a Unix system from the command line.

Course-Specific Learning Outcomes

  • LO1.a: Navigate and modify files, directories, and permissions in a multi-user Unix-like environment.

  • LO1.b: (Partial) Execute, redirect, pipe, and manage programs/processes in a multi-user Unix-like environment.

Part 1

Note

If at any point while working through this exercise, you encounter an error message, don’t worry. Carefully read and process the error message to see what went wrong. If the error message doesn’t make sense or you can’t figure out how to proceed, show your instructor or post a screenshot to the course Piazza page.

Login to Odin

This lesson is meant to be done on Odin. Go ahead and login.

Initial Hierarchy

Complete the following steps. If needed, discuss any questions that arise with your group members or ask the instructor if you need guidance.

  • On Odin, execute the command that prints the absolute path of your present working directory. Write the command in your notes.

  • Assuming you haven’t changed directories since logging in, your present working directory will be your home directory. Write the absolute path to your home directory in your notes. You will want to be able to recognize this path in the future.

  • Create the following directory structure directly within your home directory. Don’t worry about doing it incorrectly. If you mess up, we can help you get back on track.

    exercise1
    |--- photos
    |--- school
    
  • Navigate back to your home directory. Write at least two separate commands that would accomplish this goal in your notes.

  • From your home directory, execute the command tree exercise1 to see all of the files within the exercise1 directory. Verify that you have created the directories properly and show your work to an instructor or TA.

Extended Hierarchy
  • Make sure you are still in your home directory on Odin.

  • Without changing directories, add the following subdirectories within photos and school.

    exercise1
    |--- photos
    |      |--- 2017
    |      |--- 2018
    |      |--- 2019
    |--- school
    |      |--- 1302
    |            |--- notes
    
  • Discuss with your group: Is there more than one right answer to the previous question? Write any alternatives in your notes.

  • When you are finished (still in your home directory), execute the command tree exercise1 to see all of the files within the exercise1 directory. Verify that you have created the directories properly and show your work to your instructor.

Test Yourself
  • From home, navigate to the 2019 directory.

  • From within 2019, what single command can be used to change into the photos directory? Write the command in your notes.

  • Discuss with your group: Is there more than one right answer to the previous question? Write any alternatives in your notes.

  • Now, change back to the 2019 directory.

  • From within 2019, what single command can be used to change into the exercise1 directory? Write the command in your notes.

  • Challenge: There are at least three ways to navigate from 2019 to exercise1 with a single command. See how many you can come up with.

Part 2
Text Files
  • The echo command is a popular Unix command. For now, it’s sufficient to think of it as the Unix equivalent to System.out.println in Java.

  • Change into the notes directory.

  • Use echo and output redirection to add your first and last name to a regular file called day.txt in the present working directory (notes).

  • Verify that the file contains the desired text without using a text editor. Write the command you used in your notes.

  • Use echo and output redirection to add the first and last name of another person to day.txt without overwriting the first group member’s name.

  • Navigate to your home directory.

  • Without changing directories, append your instructor’s name to the end of day.txt. Don’t forget to use tab completion!

  • Verify that the file now contains all three names.

  • Navigate back to the parent directory of exercise1 and execute the command tree exercise1. You should see output similar to the following:

    exercise1
    |--- photos
    |      |--- 2017
    |      |--- 2018
    |      |--- 2019
    |--- school
    |      |--- 1302
    |            |--- notes
    |                |--- day.txt
    
Moving and Copying Files
  • Navigate to the notes directory.

  • From notes, rename day.txt to day1.txt. Write the command you used in your notes.

  • Navigate to the exercise1 directory.

  • Without changing directories, copy day1.txt to names.txt. You should now have two files in the notes directory. How can you confirm this?

  • Navigate to the 1302 directory.

  • Without changing directories again, copy both files from the notes directory into the exercise1 directory. Write the command you used in your notes. Verify that the two text files now exist in both locations.

  • Navigate to the exercise1 directory and execute the tree command. You should see the following:

    exercise1
    |--- day1.txt
    |--- names.txt
    |--- photos
    |      |--- 2017
    |      |--- 2018
    |      |--- 2019
    |--- school
    |      |--- 1302
    |            |--- notes
    |                  |--- day1.txt
    |                  |--- names.txt
    
  • Navigate to the 1302 directory.

  • From 1302, delete both text files located in the exercise1 directory. Write the command(s) you used in your notes.

  • Execute the tree command from within the exercise1 directory. You should see output similar to this:

    exercise1
    |--- photos
    |      |--- 2017
    |      |--- 2018
    |      |--- 2019
    |--- school
    |      |--- 1302
    |            |--- notes
    |                  |--- day1.txt
    |                  |--- names.txt
    

Additional Practice: Unix Commands

Additional Practice: Unix Commands

Note

This activity is intended to help students better understand the unix commands required to create a directory to hold applications.

Asks students to determine whether certain commands would work from specific directories in the project structure, explain why they might fail, and write the corrected commands based on their current working directory.

Question 1

Note

To show the solution for this question, you will need to toggle “Show Solutions” above.

Identify at least two mistakes in the following sequence of commands intended to set up a directory called activity1 to contain code related to a hypothetical first activity in CSCI 1302 (in bin and src folders). The goal is to place the activity1 directory inside of the existing activities directory without changing directories and place HelloWorld.java inside of the src folder.

The working directory is cs1302 and the current directory structure looks as follows:

Listing 5 The current directory structure
cs1302          // You are here
├── activities
└── projects

This is how we want the directory structure to look after the commands are run:

Listing 6 The intended directory structure
cs1302            // You are here
├── activities
│   └── activity1
│       ├── bin
│       └── src
│           └── HelloWorld.java
└── projects
Listing 7 Find at least two mistakes in these commands.
#. mkdir cs1302/activities/activity1
#. mkdir bin
#. mkdir activities/activity1/src
#. mkdir activities/activity1/src/HelloWorld.java
Solution
  1. cs1302 should not be included in the relative path since we are executing the command from within that directory.

  2. The relative path to bin should be activities/activity1/src. This command will place bin directly inside of cs1302 if left unmodified.

  3. This command will work as expected assuming the proper command was given in step (i).

  4. The relative path is correct, but we should not use the mkdir command to create a new .java file.