Meet the Terminal
The terminal is a program that allows you to talk to your computer by entering commands. No matter what operating system you use (Windows, MacOS, Linux), the terminal will be an essential tool for CS 61A.
Go ahead and open up a terminal. The terminal lets you give commands to your computer. Try this command:
echo "Hello world"
Your terminal will repeat "Hello world" back to the screen. The
command just tells your terminal to repeat the words you typed. Not too
impressive just yet, but it turns out the terminal can do a lot more!
On Windows or MacOS, you are probably familiar with folders and files, with which you can interact by dragging and dropping icons. Today, we are getting rid of the icons and using just the terminal to manage our files and folders!
With the terminal, you can do everything that a graphical filesystem can do -- and more!
The first command we'll use is
ls (the letter
l and the letter
s). Try typing it into the terminal!
user@computer:~$ ls Desktop/ Documents/ Downloads/ ...
Depending on what computer you are using, the output that you see after typing
lsmight be different.
ls command lists all the files and folders in the current
directory. A directory is another name for a folder (such as the
When you open a terminal, you will start from the home directory.
Notice that your prompt (e.g.
user@computer:~$) has a tilde
~ in it. Your
prompt helpfully tells you your current directory -- in this case, your
current directory is
~, which stands for the home directory.
Making New Directories
Our next command is called
mkdir. Try typing the following command
into your terminal:
mkdir command makes a new directory (i.e. makes a new
folder). Notice that, unlike
ls, we don't just type
and press enter. We also need to specify an argument to the command
(the argument is
example in this example). For
mkdir, the argument is
the name of the directory we want to create.
Some commands always require arguments to work, like
mkdir. Other commands can work just fine without supplying any arguments, like
Now that we've made our
example directory, let's make sure it is
actually there. Use the
ls command to verify that
example shows up in
our list of directories.
Moving to other directories
To move into another directory, we use the
cd command. Try typing the
following command into your terminal:
cd command will change directories -- in other words, it
moves you into the specified folder. In the example above, we chose to
move into the
Notice that the
~ in your prompt turned into
~/example. Again, the
prompt will tell you what your current directory is. In this case, we
are in the
example directory, which is located within the home directory
If you use the
ls command now, you'll notice that no output shows up.
This makes sense, since we just created the
example directory and we
haven't added any files to it. We'll come back to this later.
For now, let's get back to our home directory. There are a few ways to do this:
cd ..(two dots). The
..means "the parent directory". In this case, the parent directory of
examplehappens to be our home directory, so we can use
cd ..to go up one directory.
cd ~(the tilde). Remember that
~means home directory, so this command tells your terminal to change to the home directory, no matter where you currently are.
cd(that is, the
cdcommand with no arguments). In UNIX, typing just
cdis a shortcut for typing
We now know how to see, create, and move to directories. Our last
command involving directories will be to delete them using the
First, let's create a exampleorary directory:
If you use the
ls command, you should now see
tmp listed as a
Next, let's delete the
rm -r tmp
rm command will remove files and directories from your
filesystem. By itself (that is, without the
only removes files. However, since we are removing a directory, we need
-r to recursively remove the
tmp directory and any
tmp might contain (the process is called "recursive"
because, in order to remove
tmp, we have to remove everything inside
As you've seen, some commands require arguments, like
mkdir. Other commands do not require any arguments in order to work, like
ls. In addition, most commands can also be given flags, like the
rm. Flags are ways to specify modified behavior for commands -- for example,
rmby itself only removes files; using
rmto remove directories.
Summary of directories
So far, we have learned how to do the following with directories (folders):
ls: list the files and folders inside of the current directory
mkdir: make a new directory
cd: change directories
rm -r: remove a specified directory
Directories are not very useful if they don't contain any files. In this section, we walk through some more commands that allow you to interact with files.
For this section, let's start back in our home directory. Recall that we can do this by simply typing
into our terminal. Your prompt should now say
There are many different ways to create files. For this class, you will usually be using a text editor to directly write the file, much like how you would edit a Word document in Microsoft Word.
For now, we'll just download a file called
unix.txt, which can be
found here. In this class, you will start most
homeworks and projects by downloading a file.
The default location for downloads on the school computers is in the
Downloads directory. Let's change into that directory using our
You can use the
ls command to verify your
unix.txt is in this
On Windows and Mac, much of your interaction with files is likely spent
dragging them from folder to folder. UNIX provides a way to move files
Remember that we created a directory called
example. Let's move
mv unix.txt ~/example
mv command moves one file/directory into another
file/directory. Here, we are moving the
unix.txt file into the
example directory, which is inside the home directory.
To verify that the
mv command work, do the following:
lsto check that
unix.txtis no longer in our current directory (which is the Downloads directory).
- Change into the
exampledirectory. Your prompt should now show
lsto verify that
unix.txtshows up in
Reading a file: the quick and easy way
Files are useful because they contain information. Let's see what
unix.txt contains. Type in the following command:
This prints out a list of all the useful UNIX commands we've seen so far. The
cat command prints the contents of a file to the screen. This is a fast way
to verify that a file is correct or to read what a file contains. For example,
if you forget any UNIX commands in in this article, you can quickly
unix.txt to read about them.
To rename files on Windows or MacOS, you would click on the name of the file and type in the new name.
Renaming files with the terminal can be a little confusing at first.
Try the following command in the terminal (from the
mv unix.txt unix_commands.txt
ls, you'll see that
unix.txt is gone -- in its place is a
unix_commands.txt. Furthermore, typing
unix_commands.txt will print out the same list of UNIX commands.
It appears that we renamed
unix_commands.txt by using
mv command! Here's how to think about it:
mvwill move the contents of a file/directory into another file/directory. In the previous section, we moved a file into a directory.
- This time, we are moving the contents of a file (
unix.txt) into another file (
unix_commands.txt). While we are technically moving file contents, this is effectively the same thing as renaming a file!
This can be a bit confusing if you're seeing it for the first time, so make sure you understand it before you move onto the next section.
Note: Suppose you already have two files,
bob.txt, and you issue the command:
mv alice.txt bob.txt
This will overwrite the old contents of
bob.txtwith the contents of
alice.txt! UNIX won't warn you about overwriting, so be careful when using the
Sometimes, it is useful to have multiple copies of a file. Try the following command:
cp unix_commands.txt new_file.txt
cp command copies the contents of one file into another
ls, you will see that the
example directory now contains
cat will verify
that both files have the same contents.
Suppose we also wanted to copy the
unix_commands.txt file to our home
directory. Here's one way to do it:
- Change back to the home directory. (challenge: try doing this without looking up the command!)
Next, use the following command:
cp example/unix_commands.txt .
Don't forget the dot at the end!
The first argument (
example/unix_commands.txt) tells the terminal to
look in the
example directory to find
The second argument
. tells the terminal to copy
to the directory
.. Just as two dots (
..) represents the parent
directory, a single dot (
.) represents the current directory (the
directory we're in right now).
Now that we're in the home directory, we can use
ls to verify
that there is a copy of
user@computer:~$ ls Desktop/ ... unix_commands.txt ...
cat unix_commands.txt will show the same output of UNIX
Recap: we've seen two special directories: two dots
..represents the parent directory (one directory up), while a single dot
.represents the current directory. You can use these special expressions with any command that deals with directories. For example, you can
mva file to the current directory with the command
mv some_file .
rm command we
introduced earlier. We originally used the
-r flag to remove directories. Now we will use
rm without the
to remove a file. Type this into your terminal:
This will delete the copy of
unix_commands.txt that is in our current
directory (which is the home directory). A quick
ls will show you
unix_commands.txt is gone.
Warning: Unlike on Windows and MacOS, there is no friendly Recycle Bin or Trash from which you can restore deleted files. In UNIX, when you
rma file, it's gone. You can't "undo"
rm, so think twice (and thrice!) before using the
Summary of files
In this section, we learned the basics of manipulating files:
cat: displays the contents of a file on the screen
mv: moves a file/directory to another file/directory. When moving one file to another, we are effectively renaming the file!
cp: copies a file to another file/directory.
rm: removes a file. When using the
rmwill delete directories.
In addition, we learned about two special directories:
.. (the parent
. (the current directory).
If you ever come across a terminal command with which you are
unfamiliar, you can use a command called
man command will show the manual pages (reference pages) for
another command. In the example above, we ask the terminal to show the
manual pages for the
ls command. As you skim through the manual
pages, you'll notice that
ls can do a lot more than just list the
contents of a directory!
man is a great way to learn more about new
commands and even commands that you think you already know.
Note: Some school computers do not have the
mancommand installed, so you might get an error. That's okay -- if
manever fails, Google is your friend!
While the primary programming language in CS 61A is Python, it is important to know how to navigate through the UNIX filesystem to manage your class assignments. You will also be interacting with the Python interpreter from your terminal, whether you are using a school computer or your home computer.
In addition, if you continue with computer science after 61A, you will definitely interact more with UNIX and the terminal.
- Terminal: a program that allows users to enter commands to control the computer
Prompt: displays certain information every time the terminal is ready to receive new commands. For example, your prompt might look something like this:
Usually, prompts will tell you your current directory (in the example above, the current directory is
- Directory: the same thing as a folder. Directories can contain files as well as other directories
- Parent directory: the directory that is immediately above the
current directory (i.e. one directory up). This is represented in
UNIX as two dots,
- Current directory: the directory that we are currently looking
at. This is represented in UNIX as a single dot,
- Home directory: the top-level directory that contains all of your
files and sub-directories. This is represented in UNIX as a tilde,
ls: list the files and folders inside of the current directory
mkdir: make a new directory. For example,
mkdir examplecreates a directory called
cd: change directories. For example,
cd examplechanges directories to
rm -r: recursively remove a specified directory. For example,
rm -r exampleremoves the
exampledirectory and all files and subdirectories inside it.
cat: displays the contents of a file on the screen. For example,
cat unix.txtshows the contents of the file
mv: moves a file/directory to another file/directory. For example,
mv file1 file2moves the contents of
file1into a (possibly new) file called
file2. When moving one file to another, we are effectively renaming the file!
cp: copies a file to another file/directory. For example,
cp file1 file2copies the contents of
file1into a file named
rm: removes a file. For example,
rm file1deletes the file called
echo: displays words on the screen
man: displays manual pages for a specified command