Terminal Tips

UC Irvine - Fall ‘22 - ICS 45C

These are just things I use during class that might be helpful to you!

pwd

If you open a terminal and don’t know where you are, you can use the pwd command. It stands for print working directory.

Example:

$ pwd
/Users/caio/

The $ sign indicates a line where I entered a command, the following line is the result.

In this example, pwd tells me that I’m in the caio directory, and what is the full path to it.

Note that Windows might use \ instead of /.

ls

To find out what’s inside your current directory, you can use the ls command. It stands for listing.

Example:

$ ls
Applications     Library          Pictures         Virtual Machines
Desktop          MPLABXProjects   Public           Workspace
Documents        Movies           Screenshots      Zotero
Downloads        Music            Templates        dwhelper

This result shows me that all those files/directories are inside my current one.

cd

Usually, when you open a terminal, you should be on your home directory. What is your home directory depends on the OS. For example, on Mac, it’s /Users/[USER].

If you want to go to a different directory, you can use the cd command, which stands for change directory.

Example:

$ pwd
/Users/caio/
$ ls
Applications     Library          Pictures         Virtual Machines
Desktop          MPLABXProjects   Public           Workspace
Documents        Movies           Screenshots      Zotero
Downloads        Music            Templates        dwhelper
$ cd Desktop
$ pwd
/Users/caio/Desktop

After I changed to the Desktop directory, the pwd command reflects that. If you want to go to a previous directory, you can use .. as an indicator.

Example:

$ pwd
/Users/caio/Desktop
$ cd ..
$ pwd
/Users/caio

If I wanted to go to the Users directory, I could have done like this:

$ pwd
/Users/caio/Desktop
$ cd ../..
$ pwd
/Users

Important: cd uses relative directories. For example, when I was in /Users/caio I could just use Desktop because that was in the current directory. If I wanted to go from Desktop to Documents, I could have done that like this:

$ pwd
/Users/caio/Desktop
$ cd ../Documents
$ pwd
/Users/caio/Documents

However, you could also use the full path. For example:

$ pwd
/Users/caio/Desktop
$ cd /Users/caio/Documents
$ pwd
/Users/caio/Documents

history

After you run a command, the terminal saves it to its history. It’s just like a browser history.

To see the previous commands you have typed, you can use the arrow-keys. Up goes to previous commands, down goes to next commands.

Example:

First, run any command:

$ pwd
/Users/caio/Desktop
$ cd ..

Then, with an empty command line, press the arrow up key. You should see the same command (cd ..) show up as if you had typed it again. If you press the arrow up key again, you should now see the pwd command. If you press the arrow down key now, you should go back to the cd .. command.

This is very helpful when compiling-recompiling things!

autocomplete

If you are trying to type a long filename, you can try asking the terminal to autocomplete. You can do this using the tab key.

For example, let’s say we have these files:

$ ls
Applications     Library          Pictures         Virtual Machines
Desktop          MPLABXProjects   Public           Workspace
Documents        Movies           Screenshots      Zotero
Downloads        Music            Templates        dwhelper

Now, if I want to go to the MPLABXProjects directory, I can start typing cd MP, press tab, and it should autocomplete to cd MPLABXProjects.