PythonIntermediate PythonNumPySQLGen AI
HTMLCSSJavaScriptIntermediate JavaScriptReactp5.jsNode.js
Command LineGit & GitHub
C++JavaData Structures & Algorithms

Exercise

04. H |

Traversing the Filesystem

On the command line, typing cd followed by a valid directory name moves our current directory "forward" in the filesystem, as shown below:

Changing from one folder to another with the cd command

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/

Viewing File Content

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.

Instructions

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

Help

Get a Hint

Want the solution? Try completing the lesson first!

New Item Unlocked!

pom pom characters gifitem duck

Rubber Duck NEW

Rubber duck debugging is a classic technique used by developers to debug code. By explaining the code line-by-line to a rubber duck, you can break down the code into smaller pieces, and potentially identify the error.

... It's silly, we know.

New Item Unlocked!

pom pom characters gifitem duck

Codédex BotNEW

I’m a coding mentor bot built with GPT-4 👋. I’m here to answer any questions related to programming, Codédex, and much more.

FEATURE UNLOCKED!

sad characters gif

You have 100 XP! This means you can now join our Discord community to chat and hang out with fellow learners and the team behind Codédex!
See you there. 😊

FEATURE UNLOCKED!

pom pom characters gif

You have 250 XP! This means you can now join our On-Platform community to chat and hang out with fellow learners!
Say hi in the Introductions channel. 😊

Navigation

01. Beginning...

02. Filesystem

03. Moving Day

04. House Tour

05. Clean Slate

06. Scavenger Hunt

Manipulation

04. House Tour

15 XP

Others on this exercise:

wIngy image

@wIngy

Level 5

ashthecoder image

@ashthecoder

Aravind Kumar

Level 1

patr image

@patr

Patrícia Silva

Level 1

cleanex image

@cleanex

Sky Floor.

Level 1

patr
wIngy
ashthecoder
cleanex