On Linux, directories are places in the filesystem which can contain files and other directories. The Windows equivalent is a ‘folder’ and sometimes the words folder and directory are used interchangeably. Navigating between directories from the command line and exploring what they contain will quickly become second nature to you. Linux is structured according to the Filesystem Hierarchy Standard – but you don’t need to understand all of it at this stage.
On Linux, the filesystem has a root simply given as
/ (forward slash). All other files and directories can be described with a path from root – with forward slashes used to separate the different levels of the hierarchy.
For example, we might find user1’s Downloads directory at `/home/user1/Downloads’. This means that the Downloads directory is inside the ‘user1’ directory which is inside the ‘home’ directory which is inside the root (‘/’) directory:
/ home/ user1/ Downloads/ download1.ext download2.file
A path can be absolute which specifies how to get there from the root directory or relative to the directory you are currently in. Absolute paths have a slash at the start (a leading forward slash) whereas relative paths do not.
Continuing the example above, if you are in the user1 home directory (
/home/user1) then the relative path to download1.ext in user1’s Downloads directory is
The absolute path is
/home/user1/Downloads/download1.ext and we can use this absolute path without worrying what directory we are currently in.
Often the prompt (the text to the left of where you type in a terminal – ending with a $ or a #) will display at least part of the directory that you’re in – the current working directory. However, there is also a command that will print the path to the current working directory -
pwd for ‘print working directory’.
$ pwd /home/user1
Wherever you are in the filesystem on Linux, if you get lost just use
pwd to check the location!
Now that you know what directory you’re in, you probably want to see what else is here in terms of files and directories. You can list the contents of a directory with
ls. Running just
ls on its own will list the contents of the current directory. Running
ls with a path to another directory (either a relative path or an absolute path) will list the contents of that directory.
$ pwd /home/user1 $ ls Downloads $ ls Downloads download1.ext download2.file
Furthermore, you can run
ls with various different options to change what it lists or how it displays that information.
-a option will include all files in the output of
ls, including hidden files. Files can be ‘hidden’ on Linux by having a file name which starts with a dot such as
$ ls Downloads $ ls -a . .. .hidden.txt Downloads
But what are those ‘dot’ (
.) and ‘double dot’ (
..) entries shown by
ls? These are special directories that we can interact with. The single dot just refers to the current directory and the double dot refers to the directory one level up in the hierarchy.
$ pwd /user1/home $ ls . Downloads $ ls .. user1
To move between folders (which we generally call directories), we can use the
cd command – ‘cd’ is short for change directory. Simply type ‘cd’ followed by the name of the directory that you would like to move into. You can either move to somewhere relative to your current directory or use an absolute path (starting with a forward slash) to move anywhere.
$ pwd /home/user1 $ cd Downloads $ pwd /home/user1/Downloads
$ pwd /tmp $ cd /home/user1/Downloads $ pwd /home/user1/Downloads
Sometimes you might want to navigate back to the last directory that you were in. There is a hand shorthand for this, you can just use
cd with a hyphen/dash
- like this
The following shows an example of moving back to the previous working directory.
$ pwd /home/user1/Downloads $ cd /tmp $ pwd /tmp $ cd - /home/user1/Downloads
It is also simple to navigate up a level in the directory hierarchy. We can do this using the special ‘double dot’ directory mentioned previously. As a recap,
.. is just a link to the directory above.
The following shows an example of moving into the directory above the current working directory.
$ pwd /home/user1/Downloads $ cd .. $ pwd /home/user1
As a user on Linux you will typically have a ‘home’ directory in
/home/. This is where you’ll normally find yourself when you open a new shell session and somewhere to store your files. The shell has a useful shorthand for the location of your home directory, the tilde character
~. Whenever you type
~ at the command prompt, the shell will expand it to the absolute path of your home directory when it interprets the command.
To get to your home directory, you can use the
cd command, with tilde
~ on the command line. The shell will replace
~ with the path to your home directory before completing the change directory command.
$ pwd /tmp $ cd ~ $ pwd /home/user1
You can use the echo command and the tilde shorthand to quickly print the location of your Linux home directory. There are various ways to achieve this but using echo and
~ is an easy way to start.
$ echo ~ /home/user1
When using a command which expects a directory as an argument (such as
cd) we can use the tab key on the keyboard (
<tab>) to automatically complete the rest of the path. If there is only one path that we could be typing then it will be expanded at the prompt. If there are multiple paths that the current line could be expanded to then pressing
<tab> twice will print a list of the possible directories that you could be referring to. Tab completion at the command line works for both absolute and relative paths.
$ pwd /home/user1 $ cd <tab><tab> . .. Downloads