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 exercise1to see all of the files within theexercise1directory. 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
photosandschool.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 exercise1to see all of the files within theexercise1directory. Verify that you have created the directories properly and show your work to your instructor.
Test Yourself
From home, navigate to the
2019directory.From within
2019, what single command can be used to change into thephotosdirectory? 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
2019directory.From within
2019, what single command can be used to change into theexercise1directory? Write the command in your notes.Challenge: There are at least three ways to navigate from
2019toexercise1with a single command. See how many you can come up with.
Part 2
Text Files
The
echocommand is a popular Unix command. For now, it’s sufficient to think of it as the Unix equivalent toSystem.out.printlnin Java.Change into the
notesdirectory.Use
echoand output redirection to add your first and last name to a regular file calledday.txtin 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
echoand output redirection to add the first and last name of another person today.txtwithout 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
exercise1and execute the commandtree 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
notesdirectory.From
notes, renameday.txttoday1.txt. Write the command you used in your notes.Navigate to the
exercise1directory.Without changing directories, copy
day1.txttonames.txt. You should now have two files in thenotesdirectory. How can you confirm this?Navigate to the
1302directory.Without changing directories again, copy both files from the
notesdirectory into theexercise1directory. Write the command you used in your notes. Verify that the two text files now exist in both locations.Navigate to the
exercise1directory and execute thetreecommand. You should see the following:exercise1 |--- day1.txt |--- names.txt |--- photos | |--- 2017 | |--- 2018 | |--- 2019 |--- school | |--- 1302 | |--- notes | |--- day1.txt | |--- names.txt
Navigate to the
1302directory.From
1302, delete both text files located in theexercise1directory. Write the command(s) you used in your notes.Execute the
treecommand from within theexercise1directory. 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:
cs1302 // You are here
├── activities
└── projects
This is how we want the directory structure to look after the commands are run:
cs1302 // You are here
├── activities
│ └── activity1
│ ├── bin
│ └── src
│ └── HelloWorld.java
└── projects
#. mkdir cs1302/activities/activity1
#. mkdir bin
#. mkdir activities/activity1/src
#. mkdir activities/activity1/src/HelloWorld.java
Solution
cs1302should not be included in the relative path since we are executing the command from within that directory.The relative path to
binshould beactivities/activity1/src. This command will placebindirectly inside ofcs1302if left unmodified.This command will work as expected assuming the proper command was given in step (i).
The relative path is correct, but we should not use the
mkdircommand to create a new.javafile.