Linux Basics

Learn the basics of operating a Linux-based operating system (OS) and take your first steps in exploitation in a Linux environment!

Easy

Linux Basics
Directory Traversal

Linux_Basics::Directory_Traversal

Directory traversal is one of the most basic concepts one should grasp to use an operating system.

cd command

One of the most useful and commonly used commands that you will use is the cd command, provided by most shells like bash and zsh and even cmd.exe (Windows). cd stands for "change directory", meaning it changes the current directory or folder that your current session is working in. The syntax for this command is straightforward and looks like so:

cd [optional flags] directory

One directory that will always exist is the root directory /. We can simply change our directory to the root directory by typing (do not type the $):

$ cd /

Now our shell session is current in the root directory. We can verify this by using the pwd command, which as the man-page says, "print[s] [the] name of current/working directory".

$ pwd
/

As we can see, the output of the pwd command is /, which verifies that our shell session has the current working directory /, the root directory of the file-system. Knowing this, what other directories could we traverse to? In order to figure out the files and directories present in the directory we are working in, we can use the ls command, which "list[s] directory contents". The syntax for ls is very similar to that of cd,

ls command

ls [optional flags] directory

If we do not specify a directory in the commandline for ls, it will simply list the files in our current working directory, which will be / in our case. Let's give it a try,

$ ls
bin   dev  home  lib32  libx32  mnt  proc  run   srv  tmp  var
boot  etc  lib   lib64  media   opt  root  sbin  sys  usr

As we can see, the root directory contains various other directories and files. In fact, on the Linux system, the root directory should contain every single file on the system, nested in the various folders.

mkdir command

mkdir [optional flags] directory

We know how to list files in a directory, and we can create a directory with mkdir.

$ mkdir new
$ ls
bin   dev  home  lib32  libx32  mnt  opt   root  sbin  sys  usr
boot  etc  lib   lib64  media   new  proc  run   srv   tmp  var
# notice new above             ^^^^^

Putting it all together

Let's navigate to the /tmp directory and practice some of the skills we've learnt!

$ cd /tmp
$ pwd
/tmp

$ mkdir a
$ mkdir b
$ mkdir c

$ ls
a  b  c

$ cd a
$ pwd
/tmp/a

Hopefully from this demonstration, you can understand that our directory structure looks like so.

/
└── tmp
    ├── a
    ├── b
    └── c
└── ... <others>

We refer to directories a, b, c as child directories of tmp, while tmp is referred to as the parent directory of a, b and c.

Quiz

What is the parent directory of tmp

As you see, we can navigate to other directories in our current directory by simple doing cd <dir_name>. This is making use of the relative path, which means we are referring to a new path relative to our current location.

A more advanced way to use relative paths is to use the special .. and . directories. These are special directory names which refer to the current directory (.) and the parent directory (..).

$ pwd
/tmp/a
$ cd ..
$ pwd
/tmp

$ cd .
$ pwd
/tmp

Alternatively, we can navigate using the absolute path, where we detail the full directory path. Absolute paths always start from the root directory, and thus always begin with a /. It usually takes more characters to type the full absolute paths, and so this is less often used. However, absolute paths can be useful when navigating to a directory that is not "near" the current directory (e.g. from /a/b/c/d to /e).

$ cd /tmp/b
$ pwd
/tmp/b

Mini-Quiz

Time to test your understanding with a final quiz!

(*) Questions may require more information than what was taught.
      The internet is your best friend for these :)

Quiz

what is the command-line used to traverse to the parent directory?

which directory is the root of the filesystem?

*which command-line flag is used for listing all files and directories, including hidden ones.

*What is the short-form name of the home directory?