Command the Command Line

Part II - Getting Your Bearings

The File System

photograph of papers stacked within a folder

"Stacked papers in folder" by Lindsay Holmwood is licensed under CC BY 2.0.

The file system is defined by a hierarchy of "directories". A directory can contain any number of directories (known as "sub-directories"). For the most part, each directory is itself contained within exactly one directory; this is known as the "parent directory".

These concepts are likely familiar because they are also featured in many GUI applications like Windows Explorer and Apple's Finder. The sample commands in this section will be be supported by recordings of equivalent operations performed with a graphical interface (specifically, the GNOME Files file manager).

Path:         /top-level/sub-directory/another directory!/
              |         |             |                  |
--------------+---------+-------------+------------------+
              |         |             |                  |
              |         |             |                  |
Directories:   top-level              |                  |
                         sub-directory                   |
                                       another directory!

On Unix-like systems, the forward slash character (/) separates directory names. This means that the forward slash character cannot be used in a directory name, but everything else is fair game.

The example above describes a path where top-level is a directory that contains sub-directory. Inside sub-directory is a third directory named and another! (yes: the name includes the space character and the exclamation mark).

vm$ 

pwd

Print the current (i.e. "working") directory

vm$ pwd
/home/sally
vm$ 

In a GUI:

Using a graphical interface to see the current working directory

ls

List directory contents

vm$ pwd
/home/sally
vm$ ls
my-amazing-subdirectory
my-normal-file.txt
vm$ 

In a GUI:

Using a graphical interface to see the contents of the current directory

vm$ ls my-amazing-subdirectory
oh-boy-another-directory
just-another-file.txt
vm$ 

In a GUI:

Using a graphical interface to see the contents of some other directory

When invoked with a path to another directory, ls will display the contents of that directory.

cd

Change directories

vm$ cd my-amazing-subdirectory
vm$ pwd
/home/sally/my-amazing-subdirectory
vm$ 

In a GUI:

Using a graphical interface to move into a subdirectory

vm$ ls
oh-boy-another-directory
just-another-file.txt
vm$ cd just-another-file.txt
cd: just-another-file.txt: Not a directory
vm$ cd this-directory-doesnt-exist
cd: this-directory-doesnt-exist: No such file or directory
vm$ 

In a GUI:

Using a graphical interface to move into a subdirectory of a subdirectory

Notice that we cannot move into a file or into a location that doesn't exist.

vm$ pwd
/home/sally
vm$ cd my-amazing-subdirectory/oh-boy-another-directory
vm$ pwd
/home/sally/my-amazing-subdirectory/oh-boy-another-directory
vm$ 

...and as discussed earlier, the forward slash character (/) separates directory names.

Going $HOME

vm$ pwd
/home/sally/my-amazing-subdirectory/oh-boy-another-directory
vm$ cd ~
vm$ pwd
/home/sally
vm$ cd ~/my-amazing-subdirectory/oh-boy-another-directory
vm$ pwd
/home/sally/my-amazing-subdirectory/oh-boy-another-directory
vm$ 

The tilde character (~) is a shorthand for "my home directory." You don't have to use it all by itself; it can be used as part of a path, too.

Backing up

photograph of a car stuck in a snow bank

"Stuck Car" by Michael Pereckas is licensed under CC BY-SA 2.0

We've learned how to dive into a directory, but we're kind of stuck once we get there. Fortunately, there is a consistent method for referencing "the parent directory."

vm$ pwd
/home/sally/my-amazing-subdirectory/oh-boy-another-directory
vm$ cd ..
vm$ pwd
/home/sally/my-amazing-subdirectory
vm$ 

In a GUI:

Using a graphical interface to move into the parent directory

Two period characters (..) signify "the directory above."

vm$ pwd
/home/sally/my-amazing-subdirectory
vm$ cd ../../sally
vm$ pwd
/home/sally
vm$ 

This works just like any other directory name, so it can be used anywhere you can write a path.

vm$ pwd
/home/sally
vm$ cd ../..
vm$ pwd
/
vm$ cd ..
vm$ pwd
/
vm$ 

The hierarchy of files is sometimes referred to as a "tree" because of the way directories and sub-directories tend to "branch out" from the center. Continuing this metaphor, the top-most directory is commonly referred to as the root directory. We can't move any higher in the file system than the root.

Paths from the root? Absolutely!

photograph of a footpath with tree roots running through

"rooted-path" by Devlon Duthie is licensed under CC BY-NC 2.0.

Up until now, all the paths we've used have been based on the current working directory. When our current working directory was /home, then cd sally meant, "move to the directory named sally (inside of /home)." If we had started in the /etc directory, then the same command cd sally would have put us somewhere different: /etc/sally.

It's also possible to write paths that describe the same location regardless of our current working directory.

vm$ pwd
/home/sally
vm$ cd /home
vm$ pwd
/home
vm$ cd /home/sally/my-amazing-subdirectory
vm$ pwd
/home/sally/my-amazing-subdirectory
vm$ 

A paths which begins with a forward slash character (/) is known as an "absolute path". It describes the same location regardless of where we are when we use it.

tree

Display directories and their contents

vm$ tree
.
├── my-amazing-directory
│   └── just-another-file.txt
└── my-normal-file.txt

1 directory, 2 files
vm$ 

tree is a powerful tool for visualizing files, directories, and their relationships. It uses a text-based "tree" diagram.

tree isn't included in all systems by default, so when you're working with a system outside of this course, you may need to install it yourself

Inspecting files

photograph of a dollar bill viewed under a magnifying glass

"Not legal tender" by skycaptaintwo is licensed under CC BY-NC 2.0.

Directories are great for organization, but most of our work concerns files. Lets take a look at some tools for learning about files and their contents.

cat

Display file contents (all at once)

vm$ ls
oh-boy-another-directory
just-another-file.txt
vm$ cat just-another-file.txt
This is the first line of just-another-file.txt
This is the second line of the file!
The file only has three lines, and this is the last one!
vm$ 

less

Display file contents (with navigation controls)

vm$ less a-longer-file.txt
This is the content of a-longer-file.txt, but only
just enough of it to fill the screen. You can use
the "up" and "down" arrow keys in the terminal to
scroll through the whole file. Press the `q` key
to quit.

The less utility provides a way to review the contents of a large file. It has a lot of special controls, but the most important ones are:

sort

Re-order input

vm$ cat just-another-file.txt
This is the first line of just-another-file.txt
This is the second line of the file!
The file only has three lines, and this is the last one!
vm$ sort just-another-file.txt
The file only has three lines, and this is the last one!
This is the first line of just-another-file.txt
This is the second line of the file!
vm$ 

As you might expect from the name, the sort utility sorts its input by line and displays the result.

In Review

Exercise

Yar! There be treasure in these parts, and now's the time to grab it!

I see ya eyeing me wooden leg. You're right, I can't do this on me own. But lest ye be gettin' any ideas about taking off with that treasure yourself, remember I've got yer precious pet snail waiting in the galley. If I don't hear word from you by midnight, I'll be feasting on escargot. Well it's just the one animal, so not much of a feast, I suppose. It'll be mostly coconuts, like always. Listen, what I'm trying to say is I'm going to eat your snail, okay? So make it quick!

Follow the instructions in Chapter 3 - Using Vagrant to get set up, then once you've connected with vagrant ssh, you'll find a directory named island in your "home" directory. Within that directory is a file named turtle.txt. Use cat to read the contents and get your next step.

Solution

  1. Use cd to move into the island directory.

    vm$ cd island
    

    Next, use cat to read the contents of turtle.txt (to "talk" with the turtle):

    vm$ cat turtle.txt
    
  2. The steps through the footpath are just directory names. Use cd to move through them:

    vm$ cd footpath/right/straight/left/right
    

    There is apparently a "sign" in this directory; we can "see" it using ls:

    vm$ ls
    left  right  sign.txt  straight
    vm$
    

    The file is named sign.txt. Use cat to read it:

    vm$ cat sign.txt
    
  3. In this context, we'll interpret "go back," to mean "move to the parent directory." We'll use .. to express this. We have to do that twice, then travel into two straight directories:

    vm$ cd ../../straight/straight
    

    ...and then read the sign.txt file there:

    vm$ cat sign.txt
    
  4. We'll "go to" the $MAGIC_PORTAL using cd:

    vm$ cd $MAGIC_PORTAL
    
  5. We're out of instructions, so we'll use ls to "look around":

    vm$ ls
    

    This reveals a file named sand.txt. Once again, cat will display its contents:

    vm$ cat sand.txt
    
  6. We can find the current directory using the pwd utility

    vm$ pwd
    

    Remember that the "forward slash" character (/) is the path separator, so we should note every digit that follows that character (and ignore the rest). We'll use cd to move to the appropriate sub-directory of the /home/vagrant/island/cave directory.

  7. We can "speak" to the cave dweller by reading the contents of the cave-dweller file:

    vm$ cat cave-dweller
    
  8. After moving to the forest:

    vm$ cd ../../forest
    

    We can inspect the contents with ls:

    vm$ ls
    

    Sure enough, there is a file named inventory. Use cat to read the contents:

    vm$ cat inventory
    

    We could find the last item by comparing them all ourselves, but we know that the sort tool will display a file's contents in sorted order:

    vm$ sort inventory
    

    The last item in that output is our next step.

  9. We'll use cat to read the contents of the item from the previous step. It looks like we need to find a file inside one of the "tree" directories. There's a lot of sub-directories there, and searching them one-by-one could take a long time. We should use tree to get a visual overview of the whole "forest" directory:

    vm$ tree
    

    Now it's clear where that file is. Based on the contents, this is the file we've been searching for, so we're done!