Navigating the Filesystem

Filepaths

Sorry, you can't use Finder or File Explorer here. All we get are ls, cd, and friends.

Drake meme about navigating filesystems

But it's well-worth learning to use the command line well. It can be very efficient. At this point I prefer it over Finder.

When you first ssh into myth, you'll see something like this prompt:

adbenson@myth66:~ $▐

Yes, it's a dollar sign. That just signals where you type. Because CS makes you bank

The fragment before the colon identifies the current user (by your SUNET!) and the name of the computer you're on.

Between the colon and the dollar sign is a fragment describing your current location within the filesystem. In this example, we see ~, which is where you always start off at. ~ is a special symbol representing your "home directory". For the purposes of CS 107, everywhere you go will be a subdirectory of ~. As you enter subdirectories and thus change your current location, this will conveniently update so you always know where you are.

Every file and directory on the filesystem has a unique absolute "path". Your home directory has the path ~/. Your Downloads folder has the path ~/Downloads/. Your most recent download might have the path ~/Downloads/jungkook.jpg. As you can see, paths are expressed as a sequence of forward-slash-delimited directories from the root, ending with the name of the file (or a trailing slash for directories).

~ is a shorthand for our home directory, and for me, my home directory is located at /afs/.ir/users/a/d/adbenson. The root directory I mentioned is /.

ls

ls is a program that prints out a list of the directories and files at the specified path (which should be a directory). If you don't specify one, it defaults to the current directory you're at.

adbenson@myth66:~/Documents $ ls▐

Recipes

Tax Forms

heap_allocator_solution.c

adbenson@myth66:~/Documents $ ls Recipes▐

scalloped_potatoes.txt

김치전.txt

adbenson@myth66:~/Documents $▐

As it turns out, you can pass "flags" to ls to change the format of the output, add color, and a whole lot more. For example, you might want ls to list a lot more information about each file or directory. You'll want the -l flag.

adbenson@myth66:~/Documents $ ls -l▐

total 0

drwxr-xr-x 4 andrew operator 128 Sep 8 2019 4:12 Recipes

drwxr-xr-x 2 andrew operator 64 Mar 13 2020 00:30 'Tax Forms'

-rw-r--r-- 1 andrew operator 0 Dec 2 2021 05:26 heap_allocator_solution.c

This shows you information such as the permissions on each entity, the owner, the size, the last modification date, etc.

By default, ls hides any files whose name starts with a dot (.). There's a story behind this that I won't share. If you want ls to show these "hidden files" too, you'll need to pass the -a flag.

adbenson@myth66:~/Documents $ ls -a▐

.

..

Recipes

Tax Forms

.diary.txt

heap_allocator_solution.c

adbenson@myth66:~/Documents $ ls▐

cd

While technically you could stay in your home directory forever and do everything from there, specifying long paths for everything, it's nice to be able to change your current directory so you can refer to files relative to your current location. For example, in the examples above, if I were in my home directory, I would refer to my scalloped potatoes recipe as Documents/Recipes/scalloped_potatoes.txt, but if I changed my current directory to the Recipes directory, I could refer to it as simply scalloped_potatoes.txt.

Pass cd the path of the directory you want to want to change to.

adbenson@myth66:~/Documents $ cd Recipes▐

adbenson@myth66:~/Documents/Recipes $▐

Wait, how do I get back? I was able to cd into Recipes because ls showed me that it was inside my current directory. But how do I "go backwards"?

For that purpose, there's a symbolic link to the parent directory present inside every directory. It's called .. (You may have noticed it in the ls -a example above). For example, .. here would allow us to return to the Documents directory.

adbenson@myth66:~/Documents/Recipes $ cd ..▐

adbenson@myth66:~/Documents $▐

Ok, so what's .? . is another symbolic link like .. It refers to the current directory. So if you cd into it, you change your current directory to the current directory...so yeah, nothing happens. It has some uses outside of cd.