In your previous exercise you did the following:
$ whoami $ hostname $ pwd $ ls $ date
Hopefully, when you typed in these commands, the shell responded more intelligibly than when you typed gibberish. This is because commands are programs that the shell has installed. When you type the command, you are instructing the shell to run a program it recognizes.
$ whoami #WHO am I? $ hostname #WHAT computer is this? $ pwd #(Path to Working Directory) WHERE on the computer am I? $ ls #(List Segments) WHAT are the contents of this directory? $ date #WHEN? What is the date and time?
Quick tip: Everything written after a “#” sign is a comment or annotation. The shell will not read things written after “#”.
Quick tip: A directory is just the Linux-y term for “folder”. Just as there are slight terminology differences between MAC and PC, so too are there differences in Linux.
Let's take a closer look at what spits out when you type the command
When I type this out on my MAC computer, I get something that looks like this:
$ pwd /Users/erinnishimura
This notation is called a path and it describes the location, or the address, of my working directory within the file structure of a computer system.
When I look in this directory, I see…
$ ls Applications Dropbox master_backup_171220.sh Backup_logs Library master_backup_bkps Backup_profiles Movies seqmonk_cache Creative Cloud Files Music seqmonk_genomes Desktop Pictures seqmonk_prefs.txt Documents Public Downloads igv
These are the items I have inside the working directory.
This directory corresponds to the same directory I can locate in my MAC Finder (or PC Explorer).
Exercise: Open your Finder or Explorer and navigate to the same directory you're in on the terminal. Double check that the contents are the same. Check that the path is similar.
MAC tip: If you don't see you path in the Finder, pull down the View menu and select Show Path Bar.
The directory you find yourself in when you first open up your terminal is called you home directory. This is a special place where the shell starts up by default.
We will learn how to move into different directories and at the same time learn more about paths. To move to a new directory we use the command
cd for Change Directory.
Exercise: Try the
cd command like so:
$ cd $ pwd $ ls
What happened? Well, if you started in your home directory, nothing. This is because typing
cd without anything after it defaults to changing you into your home directory. To give more instruction as to where we want to change into, we need to add an argument. Arguments are additional user-specific information we supply to a command.
$ cd <directoryname>
A cool quirk of Linux is that a period, “.”, is shorthand for the current working directory. And two periods, “..”, is shorthand for the directory a level up from my current directory. The directory one-level-up is called the parent directory.
Exercise: Write down the path of your current working directory somewhere so you can remember it. Next, try the following:
$ pwd $ ls $ cd . $ pwd $ ls $ cd .. $ pwd $ ls
Exercise: Open Finder or Explorer and navigate to the same location.
Exercise: Keep navigating up and up through your path using
cd .. until you get to the top. Do the same in the Finder or Explorer.
You should get to a place where you eventually see this:
$ pwd /
This location is known as the root. This is the uppermost directory of your computer's file structure (that you are allowed to be in).
Now that we are in the upper most directory, let's navigate back down to where we were before. To do this, we'll browse the contents of our root directory using
ls and then select a specific sub-directory to change into using:
Exercise Navigate back down to your path. To do this, consult the path you wrote down above. Then, execute the following set of commands in which you substitute <subdirectoryname> with the first directory name in your path.
$ ls $ cd <subdirectoryname> $ pwd $ ls
Exercise: Continue to come down your path until you are in your original home directory.
Common pitfall: Many new users have trouble navigating directories when they first start out. It is something that you'll get used to over time. One thing that can help make the process easier is to continually execute
ls commands. Just imagine that anytime you want to look at something in your Finder/Explorer, you are in effect issuing an
ls command. So you should be typing
ls as often as you look at your files!
Common pitfall: Linux does NOT like spaces in directory or filenames. If one of your directories contains a space, you'll need to type a backslash+space as
\ instead of just a single space. This is called escaping a character.
Quick tip: When you're typing out the name of a directory or sub-directory, instead of typing out the whole thing, start typing it a few characters and then autocomplete by typing TAB. If the characters you've typed so far limit you down to one option, the name will autocomplete. If it narrows it down into a few options, press TAB again and those options will be listed.
The types of paths we've used for navigating up to this point (.. and <subdirectory>) are called relative paths. This means that they only make sense from the perspective of the current working directory. In contrast, we can use
cd to take us to absolute paths that would make sense anywhere on the computer system. Absolute paths always begin with the root directory, /. When we execute
pwd, the shell spits out our current working directory as an absolute path because it start with a / such as /Users/erinnishimura/.
Exercise: Open your Finder/Explorer and navigate to some directory on your computer where you keep a piece of data you've recently gathered. Look at the path bar to get a sense of where this directory is located. Now, using the terminal, try to change into this directory using an absolute path as the argument for a
cd command. Use TAB to autocomplete to save time and improve accuracy.
Hint for homework: When moving up or down a directory structure using relative paths, you don't need to jump down each directory one at a time. You can double up your jumps using a
/ between directories. For example, if you are in
/Users/Name/ and you want to be in
$pwd /Users/Name $cd Documents $cd Labwork
$pwd /Users/Name $cd Documents/Labwork $pwd /Users/Name/Documents/Labwork/
CTRL+u erase the current line
CTRL+l (that's a lower case “L”), clear the terminal screen
CTRL+a go to the beginning of the line
CTRL+e to go the end of the line
CTRL+c cancel out of a program or command that is being executed
CTRL+d log out of the terminal
UP arrow print out the last command executed (even if it failed).