Linux directory commands for beginners
On Linux, directories are places in the filesystem which can contain files and other directories. The Windows equivalent is a ‘folder’ - 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 at
/ (forward slash). All other
files and directories can be described with a path from the root – with forward
slashes used to separate the different levels of the hierarchy.
Directory structure example
For example, we might find user1’s Downloads directory at `/home/user1/Downloads’. Therefore, 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
Absolute and relative paths
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.
Absolute and relative paths example
Continuing the example above, if you are in the user1 home directory (
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.
What directory am I 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 use
check the location!
Show the contents of a directory.
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
ls. Running just
ls on its own will list the contents of the current directory.
ls with a path to another directory (either a relative path or an absolute
path) will list the contents of that directory.
Directory listing example
$ pwd /home/user1 $ ls Downloads $ ls Downloads download1.ext download2.file
Furthermore, you can run
ls with various options to change what it
lists or how it displays that information.
Listing hidden files on Linux
-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
Hidden files example
$ ls Downloads $ ls -a . .. .hidden.txt Downloads
But what are those ‘dot’ (
.) and ‘double dot’ (
..) entries shown by
are special directories that we can interact with. The single dot refers to
the current directory, and the double dot refers to the directory one level up in
$ pwd /user1/home $ ls . Downloads $ ls .. user1
Navigating to files and directories
To move between folders (which we generally call directories), we can use the
command – ‘cd’ is short for change directory. 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.
Relative path example
$ pwd /home/user1 $ cd Downloads $ pwd /home/user1/Downloads
Absolute path example
$ pwd /tmp $ cd /home/user1/Downloads $ pwd /home/user1/Downloads
Go back to the previous directory.
Sometimes you might want to navigate back to the last directory that you were in.
There is a hand shorthand for this; you can use
cd with a hyphen/dash
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
Go up a directory
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. To 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
What is the home directory on Linux?
As a user on Linux, you will typically have a ‘home’ directory in
home directory is where you’ll generally find yourself when you open a new shell
session and provides somewhere convenient 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.
Navigate to the home directory
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
Print your home directory
You can use the echo command and the tilde shorthand to print the location
of your Linux home directory. There are various ways to achieve this but using
~ 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 use. Tab completion at the command line works for both
absolute and relative paths.
$ pwd /home/user1 $ cd <tab><tab> . .. Downloads