Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Console Foundations Getting Started with the Console Running Commands

Justin Burkard
Justin Burkard
5,558 Points

Help understanding where I am in file system hierarchy when running commands.

In the example, we run the "ls" command. It returns a folder called 'documents' and a file called 'hello.txt'.

Then it's demonstrated you can do "ls /etc" to get the contents of that specific folder.

My first question is, where did the 'etc' folder come from and why wasn't it displayed when we initially ran "ls"?

The way I'm thinking, I should be able to execute "ls /documents" since that was returned back as a directory, but that's telling me no such file or directory exists. Is my thinking incorrect?

2 Answers

Hi Justin,

In Unix environments such as Linux and Mac, a / at the beginning of a file path denotes the root directory. This directory contains all of the important files for a system and requires administrator access to modify almost all contents.

When you originally start your terminal, unless you've configured it otherwise, you start in your personal home folder. The absolute file path for this is "/home/<yourusername>/" where <yourusername> is your user name. This path is referred to in the terminal by the shorthand "~" character. I believe on a Mac it is slightly different, but it is elevated outside of the root directory even if the path might be a bit different.

If you notice beside the area where you can type, it will start off by having that ~ character which means you are in your home directory. That means you are a few folders above your root directory, which is where "etc" resides. By typing out "ls /etc" you are putting the absolute file path of "/etc" into ls and that is printing out the contents of that folder.

Your documents folder resides in your home directory so its absolute file path is "/home/<yourusername>/documents" not "/documents", which would be in the root directory. If you type in "ls ~/documents" it is the same as "/home/<yourusername>/documents" because remember that the "~" is the same as "/home/<yourusername>". Or you can type out just "ls documents" because that is a relative path that uses the current folder you're in to look for the folder.

Does that make more sense?

Justin Burkard
Justin Burkard
5,558 Points

Wow thanks for the quick and very complete answer. That makes a lot of sense. I've been encountering these things for years and never took the time to really learn what everything means. It's definitely not as confusing as it seems on the outside, wish I would have spent a couple hours learning this stuff a long time ago. Thanks again!

You are very welcome, Justin, and I completely understand that. Once you practice around in the console for a couple hours, everything will become more clear. One of the best ways to practice is to make/get a live CD of a Linux OS such as Ubuntu and then boot that up and play around in the terminal. The changes will never be permanent and if you screw anything up, you just restart the computer and start over. It's a great way to learn with no collateral damage! lol

Kun Qian
Kun Qian
14,870 Points

Thanks for both of you asking and answering, I have exactly the same question and I got the answer