Using the Terminal (Command Line)
1.0. Intro to the Terminal
Some definitions:
Definition: Terminal
The Terminal is a "command-line interface" (CLI) text-only application that allows you to access the shell. Most Terminal programs allow for no use, or limited use, of a mouse.
Definition: Shell
The Shell is a program that allows you to give instructions to the operating system.
In practice, most people use the terms "terminal," "command-line," and "shell" more-or-less interchangeably.
Definition: Graphical User Interface
A graphical user interface (GUI), which is what most operating systems provide you with, offers a more visually appealing way of giving input to a computer, usually involving the use of a mouse or touchpad. Although a GUI can be an advantage under many circumstances, in here we'll be mostly confining ourselves to working with a keyboard and the command-line interface, which has some advantages of its own.
Activity: Launching the Terminal
- Turn on your computer and log in
In the interests of efficiency, your computer should already be booted up and ready to run every day when you come into class. - Launch the Terminal application.
- On Apple machines running OS X, the Terminal application is located in /Applications/Utilities/Terminal.app
- On Windows machines, the Command Prompt application is available as a Terminal of sorts. On Windows 10 machines, the Ubuntu Bash for Windows 10 is available as an application once you've followed the instructions here.
- On Linux/Unix machines, the Terminal application is typically located in your Applications directory, in a sub-directory called Accessories.
1.1. Directories
Files are organized into "folders" called directories.
On OS X and Linux operating systems the root directory "/" ("forward slash") is the largest container in the file system. Everything else is contained in directories nested in the root directory, as shown here.
On Windows machines, the root directory is a "backslash", \
.
Your home directory is typically identified by your username—rwhite
or abrady
in this example. This directory contains your personal files, and its location depends on your operating system:
- Apple's OS X places a home directory for each user on the computer in the
/Users/
directory. - Windows users will find their home directory for all users installed at
C:\Users\
. - Linux/Unix users will find home directories located in
/home/
.
1.2. Using the Terminal to navigate your computer
When you start a Terminal session, you begin located in your "home directory," which is typically identified by your userID, often your name.
Home directory locations
- On a Mac, your home directory will be
/Users/userID/
- On a Linux machine, your home directory will be
/home/userID/
- On a Windows machine, your home directory will be
/C/Users/userID/
. Note that Bash for Windows will not open your home directory automatically! You'll need tocd
into that directory if you want to access all of your files. (Bash for Windows has its own home directory, but there's nothing in it.)
The "working directory"
When working in the Terminal, your perspective of the file system is based on your "location" in that system, the "working directory." When you first log in, your working directory will be your home directory.
You can identify the working directory with the pwd
command, which stands for "print working directory."
Here, the computer has indicated that my working directory—my current "location" in the computer file system—is the directory /Users/rwhite
.
The ls
command
Use the command ls
to list the files and folders in your current directory.
PhileasFogg:~ rwhite$ ls
The ls
command will give you different results based on where you tell it to look.
Basic Terminal commands on your computer
Take a moment to use the command ls
to list the current directory, as well as other directories using the follow possible combinations:
$ ls Documents # list files in Documents folder, located in your working directory
$ ls . # list files in current directory (same as ls)
$ ls .. # list files in directory above the current
$ ls ~ # list files in your home directory
$ ls ~/Downloads/ # list files in the Downloads folder of the home directory
While the ls
command "looks" at files and directories on your computer, the cd
command is used to "move to" other places in your directory structure.
The cd
command
Use the command cd
to c
hange d
irectory into a different location:
$ cd ~/Documents # move into the Documents folder
$ cd .. # move into directory above current one
$ cd ~ # move into your home directory
$ cd / # move into the root directory
Relative and absolute addresses
We can use the ls
and cd
command to list and move to directories in the file system, but there are two separate ways to identify a location: relative addresses and absolute addresses.
- A relative address is specified relative to the current, working directory; it doesn't begin with a
/
in the address.
If I'm in my Documents directory (inside my home folder) and I want to look at the contents of myDownloads
directory (also in my home folder), I can give the relative addressls ../Downloads
- An absolute address is specified relative to the root directory; it is unique, and it does begin with a
/
in the address.
Regardless of where I am on the computer, I can look at the contents of myDownloads
directory with the absolute addressls /Users/rwhite/Downloads
Both addressing systems are valid, and both are useful. You'll quickly get comfortable with them.
Two additional Terminal commands that will come in handy:
cat <filename>
will display the contents of the given file. This is useful for quickly examining text files.exit
or^D
will exit the current process. If you're not doing anything else in the Terminal, it will shutdown the Terminal itself.
Check yourself
Take a moment to ls
the files in various directories, and practice using cd
to move around the file system.
Identify two commands that could take you to your home folder
What single command takes you to the root directory of the computer?
What command would you use to list the contents of the root directory while you're in your home directory?
1.3. More bash commands
Here are a few more that you may find useful.
More BASH commands
$ cat <filename> # displays the contents of a file.
Useful with quickly examining text files.
$ less <filename> # displays the contents of a file screen-by-screen.
- Use f to move forward in the document
- b to move backward through the document
- /keyword[Enter] to search through a document (along with n to get the next instance of that term in the document)
- quit looking at the document.
$ ctrl-c # stops execution of a command.
If you've accidentally typed ls -R /, you're going to be waiting for a while before that listing stops running.
$ [tab] # The Tab key acts as an "autocomplete" key for many situations.
Pro tip: If you have a Documents in your home directory, try typing ls ~/Doc and then hitting the Tab key. The Tab key acts as an "autocomplete" feature to fill in remainder of the command. This is the single best bash command that will allow you to work faster in the Terminal.
$ [up-arrow] # repeats the last Terminal command
The second best bash command that will allow you to work faster in the Terminal
$ exit or Ctrl-D # Ends your Terminal session neatly.
Do this to make sure that all your work has finished
$ mkdir <directoryname> # creates a new directory (folder) in the current directory
Let's practice these on your computer.
Make a new directory
- Take a moment to create a new directory in your
~/Desktop
folder calledintrocompsci
. - Use
ls
to confirm that the folder was created. - Then make a folder in your
introcompsci
directory calledcode
. - Use the
ls –al
command to list out the attributes of these folders. Can you figure out what some of the columns listed mean? cd
into these folders to see if there is anything in them, then return to the home directory.
1.4. Interacting with the server.
When a computer is connected to a network, an Internet Protocol (IP) address or a hostname is required to interact with that computer.
Your own machine, the one that you're working on right now, has the IP address 127.0.0.1, and a hostname of localhost.
A remote networked computer called a server may—with proper authentication—allow for users to log in, whether they're on another computer in the same room or one halfway across the world. This class's server is headless, and tucked away in some corner of campus. The only way you can access that machine is via the command line.
1.4.1. What can you do?
You'll be interacting with this course's server in a number of different ways, depending on your needs. You might:
- Log in using ssh (the Secure SHell) in order to use the computer to run Python, edit programs, write web pages, etc. (You already did this once when you logged in to change your password.)
- Submit a copy of an assignment using scp (Secure CoPy)
$ scp -P 1030 assignment.java rwhite@crashwhite.polytechnic.org:~/forInstructor/ rwhite@crashwhite.polytechnic.org's password: assignment.java 100% 14 0.4KB/s 00:00 $
- View webpages that you've written that are being served by the server.
Let's log in again using SSH and try doing some other things.
1.4.2. Logging on to the server
To log in to the server, you'll need a userID and password. You will receive stern warnings from the instructor at this point about strong passwords and sharing your login credentials with others.
Learn about one way of creating a stronger password by watching this video:
When you've received your login credentials, use the
ssh
command to log in to the server and change your password.The
ssh
commandThe Secure SHell (
ssh
) command allows one to securely log in to and use a remote computer's shell.Log on to the server and change your password
Log in to the course's remote server by using
ssh
as follows:$ ssh -p 1030 userID@crashwhite.polytechnic.org userID@crashwhite.polytechnic.org's password: Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-29-generic x86_64) ... Last login: Wed Jan 9 11:27:19 2013 from 10.30.236.113 userID@crashwhite:~$As soon as you have logged in, change your password! Come up with a strong, unique password—again, you'll get more advice from the instructor on this—and then change your password by issuing the following command, and following the instructions:
$ passwd
Take a look in your home folder to see if anything is there:
userID@crashwhite:~$ ls userID@crashwhite:~$It doesn't look like there's anything in your home directory just yet.
Create a directory that you'll use to store the programs you want me to evaluate.
userID@crashwhite:~$ mkdir forInstructor
Check your home folder again to ensure that the directory was created.
userID@crashwhite:~$ ls forInstructor userID@crashwhite:~$Once you've changed your password, you can log off using the exit command:
userID@crashwhite:~$ exit logout Connection to crashwhite.polytechnic.org closed. $Let's practice that again!
Create a place in your home director where I can return your assignments
Use the mkdir command to create a directory called fromInstructor in your home directory. This is where the instructor will place work that you've submitted after it has been graded.
$ cd ~ $ mkdir fromInstructor $ ls -lp total 4 drwxr-xr-x 2 userID student 4096 2012-02-07 08:26 fromInstructor/
1.5. More bash commands
Even more BASH commands
$ mv fileb.py .. # moves the file from one location to another.
In this case fileb.py in the current directory is moved to the directory above.
$ mv oldname.py newname.py # "moves" the file so that it has a new name.
$ cp fileb.py .. # makes a copy of fileb.py and places it at the specified location
$ cp <filename1> <location>/<filename2> # makes a copy of filename1, renames it, and places it at the specified location
$ cp <filename1> <location>/<filename2> # makes a copy of filename1, renames it, and places it at the specified location
$ rm filea.py # removes (deletes) the file.
Note that there is NO recovery from this!
$ cd ~/Documents/stuffToDiscard; rm –r ./
- Combine two commands on a line
- Changes to the stuffToDiscard directory
- then removes everything in that (current) directory and below, recursively
- USE WITH EXTREME CAUTION!
Demo: rm -rf
What would be the result of executing the following command on a computer?
WARNING: Do not run this code on your computer! This is a hypothetical question only.
$ rm -rf /
This command will attempt to force the removal of the root (/) directory, recursively.
This command is so powerful that you probably wouldn't be able to run it without actually having
root
permission, and even then you'll probably have to issue an additional override command. The command is that powerful.If you want to "erase" every file on your hard drive, there are better ways to do it, especially if you're trying to do so securely so that no one will be able to recover them. But this command is plenty powerful enough to do some serious damage.
If you choose to use the
rm
command, be careful!