Navigating the file system
You are getting the first edition of all these pages. Please let me know if you find an error!
Part 1: Navigating the File System
Understanding the File System Structure
Filesystems are follow a “tree” structure for both Windows and Unix-based systems. Specifically, an upside-down or sideways tree.
Key terms and concepts
Course Note: You need to know terms and concepts that look like this.
Directories hold files and other directories. When you use the term subdirectory, you are talking about the directories listed inside the current working directory.
Files represent programs, pictures, audio, video, word processing docs, and the like can be run by the operating system (in the case of programs) or opened by another piece of software, like Photoshop, Microsoft Word, VSCode, etc.
The file system has a root directory. On Linux (and Mac), this directory is named /
. On Windows, it is typically C:\
.
- Linux uses forward slashes (
/
), whereas Windows uses backslashes (’\’). It matters, and is an endless source of annoyance for developers.
A user’s home directory is where their user-specific content lives, like documents and pictures that you save. On your personal computer, you probably only have one user. A lab machine will have many different users.
- On Linux, the home directory for the user named ‘alice’ is
/home/alice/
- On Mac, it would be
/Users/alice/
- On Windows, it would be
c:\Users\alice\
You can use the Terminal/CLI to navigate the file system, like you would graphically using the Windows Explorer or Mac Finder to navigate to files. As you navigate with the CLI, you are “in” one directory at a time. The directory that you are currently “in” is called the working directory. Commands that you run run in the context of the working directory. If you were to create a file using the CLI, for example using touch newfile.txt
, it will create the file in the working directory. Or if you were to try and run a program from the CLI, it will look in the working directory for that program (and other places we will discuss later).
Explore the root directory using the ls
and cd
commands.
Open a Terminal for Mac or Ubuntu. See the Launching a Terminal lab.
Type in the following CLI commands one at a time.
pwd
ls # This will not show anything because there are no files.
cd .. # Go "up" one level in the file tree.
pwd
ls # This should now list some things.
ls / # List the files in the root.
ls -l / # List the details of files in the root.
cd / # Change working directory to root.
ls # list files.
cd .. # Go up... But it won't go anywhere because you can go higher!
ls # You're still in the root. List root's files.
None of these commands change anything on your computer. They give you information and let you navigate between directories.
Mac users: If you encounter a Permission Denied
error while running the ls /
or cd /
commands, try running sudo ls /
or sudo cd /
. It will prompt you to enter your password. The sudo
command makes you an “administrator” in the eyes of the CLI. Mac is protecting the sensitive /
directory, and wants to make sure you have permission to do what you’re trying to do.
Key Commands
pwd
- Print Working Directory - what is the name of the directory you are currently “in”. Use then when you don’t know where you are.ls
- List contents. Will show both subdirectories and files in the working directory.ls <target>
- List the contents of target directory, e.g.,ls /usr/
ls -l
- Lists contents and gives you additional information, like the file type. May also dols -l <target>
ll
- Shorthand forls -l
. Can doll <target>
cd
- Change Directory. This is how you navigate.cd /
changes to the root directorycd ~
or simplycd
will navigate to the user’s home directory.cd ..
go “up” one level to the parent of the current directorycd <target>
changes to the<target>
directory.
The argument of the ls
and cd
commands is a directory name or the special ..
symbol. You can “jump” directories by putting the directories full name, like ls /usr/bin/
. A directory’s full name is called its path.
You can also specify relative paths, which we will discuss more later.
The terminals are capable of autocompleting. Type cd
to change to your home directory, then type cd D
then hit the Tab key. What happens? The terminal will find all subdirectories (if any) of your working directory that start with capital D.
Extremely important point: all file system names are case-sensitive in Linux. For example, you can have files named user.txt
and User.txt
and or a directory /usr/
and /Usr/
they are different. Capitalization matters in software development.
Exercise:
- Navigate to the
/usr/
directory. Use thepwd
command to display your current directory. Typels
. What do you see? - Now type
ls -l
orll
. What do you see? - Use
cd ~
or simplycd
to navigate to the home directory. Usels
to display the files and folders. What do you see?
Knowledge Check:
- Question: What does the
pwd
command do? - Question: How do you navigate to the root directory?
- Question: How do you navigate to your home directory?