On the command line, typing cd
followed by a valid directory name moves our current directory "forward" in the filesystem, as shown below:
But that's not all the cd
command can do! Oh no. It can also be used to go "backward" in the filesystem, using two periods. Assuming we're currently on the schrute-farms directory, here's what this looks like:
$ pwd
/Users/dwight-schrute/schrute-farms
$ cd ..
$ pwd
/Users/dwight-schrute/
Using the ..
double period moves us back in our filesystem.
Note: When moving forward in the filesystem, we go to a "child" directory. When moving backward, we go up to a "parent" directory.
Even so, one directory at a time can get tiring really quickly. In this case, we use a forward slash /
to move up or down multiple directories in a single command:
$ pwd
/Users/dwight-schrute/schrute-farms/
$ cd ../..
$ pwd
/Users/
If we want to see what is inside of a file (like a text-based .txt file or a Python .py file), we can do so with the cat
command! 😻
$ pwd
/Users/dwight-schrute/schrute-farms/
$ ls
beets.txt
$ cat beets.txt
I have 100 beets!
The cat
command will print all text-based content from a file to the command line.
Note: The echo
command can also be used for viewing file content, but the cat
command is more commonly used.
Let's practice changing between directories by "touring" the house directory we downloaded earlier!
From the house directory, change into the kitchen directory.
Then, use the ls
command to view the content within the directory.
Here's a question for you: Where is the peanut butter jar 🥜 and the jelly 🍇 in this kitchen to make a PB&J?
Use the cat
command to find out!
Now, use the double period ..
syntax to change back into the main house directory.
Here is a diagram of how the house directory and its contents are organized:
house/
├── bathroom/
│ └── medicine-cabinet/
│ └── items.txt
|── bedroom/
│ └── closet/
│ └── items.txt
├── dining-room/
│ └── cabinet/
| └── items.txt
├── kitchen/
│ ├── fridge/
│ │ └── items.txt
│ └── pantry/
│ └── items.txt
└── living-room/
└── shelf/
└── books.txt
Want the solution? Try completing the lesson first!
01. Beginning...
02. Filesystem
03. Moving Day
04. House Tour
05. Clean Slate
06. Scavenger Hunt
04. House Tour
15 XP