The purpose of this lab is to teach you some basic Unix commands for those who are using a Mac OS X machine. Mac OS X is a version of Unix. If you are using a Windows machine, read the DOS Lab instead.
You are probably already familiar with interacting with files and folders on your Mac through the "Finder" program. Finder is a GUI (Graphical User Interface) which allows you to navigate through your file system and create new folders and documents, and organize them in ways that make sense to you. Sometimes in programming however, you will need to use your file system in a different way. For this, on Mac, you will use a program called "Terminal". Terminal is known as a CLI (Command Line Interface). That means in addition to accessing your file system, you can also use Terminal to run Python programs directly, among many other programming tasks.
The purpose of this lab is to introduce you to some of the basic commands you will commonly use when interacting with Terminal. Many of these are very short abbreviations of longer words or phrases, so knowing what they stand for may help you remember and understand them better. If you are curious to explore the full list of Unix commands, you can find it here.
Also note that the file system on your computer is structured in a hierarchical way. That means that files and folders are nested inside of other folders. There is some specific terminology we will use when dealing with hierarchical structures like this: directory (also called folder), parent directory, sub-directory, and file. When one directory contains another directory, the containing one is called the "parent directory", and the contained one is called the "sub-directory."
You should see a mostly-blank window open that shows your computer username, followed by the $ symbol. This is called the Terminal prompt, and you will often read or hear the phrase "enter this text at the prompt." That means type that text after the $.
Whenever you open a new Terminal window like this, you will be put into the "Home" directory. This is equivalent to opening a new Finder window and clicking on the entry in your "Favorites" that has the small house icon next to it.
Note: You may want to add the Terminal program to your Mac's Dock for easier access. To do this, drag the Terminal program from within the Applications/Utilities folder in Finder on to your Dock.
An important thing to note is that in the command prompt you can use "tab-complete" to automatically finish the name of a file or directory so you don't have to type the whole thing. This will save you a tremendous amount of time and avoid typing mistakes. To tab-complete, begin typing the name of the file or directory and then hit the Tab key. If the command prompt is able to map what you've typed to a unique location, it will complete the name for you. If the command prompt finds multiple files or folders with a similar name, it will not autocomplete since it cannot figure out which file you mean, and you need to type more characters until it can get a specific match.
If at any point you want more information about a particular command you have entered, you can type
man
followed by the name of the
command you want information on. man stands for manual, and this command will display
the instruction manual for that command, including a description of what it
does and all the extra options you can add to change what the command does. To
exit from viewing the manual page, type q (for quit). Now you try it yourself!
Enter the following at the terminal prompt:
man pwd
The pwd command stands for print working directory, and it tells you where the terminal program is currently looking in within your file system, using the absolute path. You can always use this command to verify what folder or directory you are inside of.
In the following exercises, you will try moving around various directories and creating and modifying files on your computer. As you follow the steps, try to find the same directories and files using the Finder program at the same time so you can see the effects of the commands you are entering.
1. Navigate to your Desktop within Terminal
cd Desktop/
The cd command stands for connect
directory, and it moves you in Terminal to the directory you entered after
cd. Note that Desktop has a / character at the end. This
indicates that Desktop is a directory, rather than a file.
2. Create a new directory
mkdir NewDirectory
mkdir stands for make
directory, and it creates a new directory (or folder) named whatever you
type after mkdir.
3. List all of the files and directories in your current location
ls
ls stands for list. ls is often used with extra
options to print out more information than just file names. For example, ls -l (a lowercase letter L, not a
number 1) will print out all files and directories using their long
form, which includes permissions, file size, time last edited, and other
metadata about each file. You can use the man command to find out more about all the options available to
use with each command, for example man ls.
4. Move in to "NewDirectory", the directory you just created
cd NewDirectory/
Remember what cd means? This time you are
moving down a level in the file hierarchy. If at any point you want to get back
to your Home directory, no matter where you are in the file system, you can
just type cd with nothing after it, and
you will be returned to the Home directory.
5. Create a new blank file within NewDirectory
touch HelloUnix.txt
By entering the .txt at the end of the filename,
you are creating a new file that is a text file. If you entered a different extension,
you would be creating a different type of file. For example, many image files
end in .jpg to indicate they are JPEG
files.
6. Comparing with Finder
All the changes you make in Terminal are reflected on your actual file system, which you can see in Finder. Now you are going to see that it works the other way too! Go back to your Finder window, navigate manually to NewDirectory and open the HelloUnix.txt file. It should open in the your default text editor (likely TextEdit). Type your name, your major, and your favorite color on separate lines and then save the file and close it using the "X". Then go back to the Terminal window.
7. View the contents of HelloUnix.txt
more HelloUnix.txt
In addition to the more command, which shows you the
contents of the file page by page, you can also view just the beginning of a
file's contents using the head
command, and just the end of a file's contents using the tail command. To exit from
viewing the file's contents, type q.
8. Rename the new file you just created
mv HelloUnix.txt GoodbyeUnix.txt
mv stands for move.
But wait, aren't you supposed to rename the file, not move it? Well to
your computer's file system, they are the same thing, because the
"name" of something is actually the location on the computer where it
is stored, so from the computer's perspective, renaming a file means moving it
to a different spot.
9. Delete the file you just renamed
rm -i GoodbyeUnix.txt
Here you use the -i
option with rm
so that you get a confirmation prompt before deleting the file, because once a
file has been deleted, it is gone forever! If you are sure you want to delete
something, you can omit the -i option and just enter rm <filename>, but be careful with that
command!
10. Navigate out of NewDirectory, back to your Desktop
cd ..
A single . indicates the
directory you are in. If you typed cd . nothing would happen! But when you use two dots, that means
go to the parent directory of wherever you are. It moves you one level
up the hierarchy.
11. View a list of all the commands you just typed
history
history will show you, in reverse
order, all of the commands you have entered into Terminal so far.
12.
View the IP properties of a computer
ifconfig
This command will show you the
IP address, subnet mask and default gateway of each network adapter.
13.
See the currently logged in user
whoami
This command will show you the
name of currently logged in user.
14.
See the computer’s name
hostname
This command will display the
computer’s network name.
15.
Find round trip time to transmit and receive data to and from a server
ping <IP address> e.g. ping 172.217.175.100
ping <site name> e.g. ping www.google.com
The ping command sends
packets of data to a specific IP address on a network, and then lets you know
how long it took to transmit that data and get a response.
16.
Trace network route to a server
tracepath <IP address> e.g. tracert 172.217.175.100
tracepath <site name> e.g. tracert www.google.com
Displays jump from one
network device to another when connecting to the IP X.X.X.X or some site.
17.
Display all active ports on a computer.
netstat
Displays all active ports on
a computer.
18.
Display all active and inactive ports on a computer.
Netstat -a
Displays all active and
inactive ports on a computer.
19. Query the DNS name of an IP address.
nslookup <IP address> e.g. nslookup 172.217.175.100
This
command queries a DNS name of a computer represented by IP address.
And with that, you're done with the lab! Make sure you speak to your TA or instructor to get their sign off that you have completed the exercises, and don't forget to create a file named outputs.txt and copy and paste all of the input and output you just generated in Terminal there, save it, and upload it to Blackboard.
If you are interested in learning more about Unix and the Terminal, Wikipedia's page on Unix and Apple's Command Line Documentation are a good place to start!