A filesystem organizes a computer’s files and directories into a tree structure:
The first directory in the filesystem is the root directory. It is the parent of all other directories and files in the filesystem.
Each parent directory can contain more child directories and files. In the filesystem on the right, blog/ is the parent of 2014/, 2015/, and hardware.txt.
Each directory can contain more files and child directories. The parent-child relationship continues as long as directories and files are nested.
The first command we’re going to look at is ls. A command is a directive to the computer to perform a specific task. When you type ls, the command line looks at the directory you are in, and then “lists” all the files and directories inside of it. Be sure to type the letter l as in “List” and not the number 1.
In the terminal, the first thing you see is $. This is called a shell prompt. It appears when the terminal is ready to accept a command.
The next command we’re going to look at is pwd, which stands for “print working directory.” It outputs the name of the directory you are currently in, called the working directory.
Here the working directory is blog/. In Codecademy courses, your working directory is usually inside the /home/ccuser/workspace/ directory.
Together with ls, the pwd command is useful to show where you are in the filesystem
Our next command is cd, which stands for “change directory.” Just as you would click on a folder in Windows Explorer or Finder, cd switches you into the directory you specify. In other words, cd changes the working directory.
Let’s say the directory we change into is 2015:
When a file, directory, or program is passed into a command, it is called an argument. Here the 2015 directory is an argument for the cd command.
The cd command takes a directory name as an argument and switches into that directory.
Instead of using cd twice in order to move from 2015 to memory, we can use it once and give it a longer argument:
To navigate directly to a directory, use cd with the directory’s path as an argument. Here, cd jan/memory navigates directly to the memory directory.
To move up one directory, we use cd ..:
Here, cd .. navigates up from jan/memory/ to jan/.
Now that we can traverse the existing filesystem, let’s try editing it by making directories (folders) through the command line. The command for that is mkdir:
The mkdir command stands for “make directory”. It takes in a directory name as an argument and then creates a new directory in the current working directory.
Here we used mkdir to create a new directory named media/ inside our working directory.
Now we know how to create directories through the command line, but how do we create new files?
We can do this using the command touch:
$ touch keyboard.txt
The touch command creates a new file inside the working directory. It takes in a filename as an argument and then creates an empty file with that name in the current working directory.
Here we used touch to create a new file named keyboard.txt.
Now that we’ve covered the basics of navigating your filesystem from the command line, let’s look at some helpful commands that will make using it easier!
clear is used to clear your terminal, which is useful when it’s full of previous commands and outputs. It doesn’t change or undo your previous commands, it just clears them from the view. You can scroll upwards to see them at any time.
tab can be used to autocomplete your command. When you are typing the name of an existing file or directory, you can use tab to finish the rest of the name.
The up and down arrows (↑ and ↓) can be used to cycle through your previous commands. ↑ will take you up through your most recent commands, and ↓ will take you back through to the most recent one.
You’ve learned five commands commonly used to navigate the filesystem from the command line. What can we generalize so far?
- The command line is a text interface for the computer’s operating system. To access the command line, we use the terminal.
- A filesystem organizes a computer’s files and directories into a tree structure. It starts with the root directory. Each parent directory can contain more child directories and files.
- From the command line, you can navigate through files and folders on your computer:
- pwd outputs the name of the current working directory.
- ls lists all files and directories in the working directory.
- cd switches you into the directory you specify.
- mkdir creates a new directory in the working directory.
- touch creates a new file inside the working directory.
You can use helper commands to make navigation easier:
- clear clears the terminal
- tab autocompletes the name of a file or directory
- ↑ and ↓ allow you to cycle through previous commands