Navigating directories

2.3 Using Directories and Listing Files (Weight: 2)

📘Linux Essentials (LPI 010-160)


In Linux systems, files are organized in directories. A directory is a container that holds files and other directories. Learning how to navigate directories is a basic and important skill when using the Linux command line.

System administrators and users often move between directories to access configuration files, scripts, logs, and application data. The Linux command line provides several commands that allow users to move through the directory structure quickly and efficiently.

This section explains everything needed for the Linux Essentials (LPI 010-160) exam related to navigating directories.


1. Understanding the Linux Directory Structure

Linux uses a hierarchical directory structure. This means directories are organized in a tree-like format.

At the top of the structure is the root directory:

/

All other directories and files exist under this root directory.

Example structure:

/
├── bin
├── etc
├── home
│ ├── user1
│ └── user2
├── var
└── usr

Example in an IT environment:

  • A system administrator may navigate to /etc to view system configuration files.
  • A developer may work inside /home/user/projects where development files are stored.
  • Log files may be reviewed inside /var/log.

Understanding how to move between these directories is called directory navigation.


2. Viewing the Current Directory (pwd)

Before navigating, it is useful to know where you currently are in the filesystem.

The command used is:

pwd

Meaning:
pwd stands for Print Working Directory.

It displays the full path of the current directory.

Example:

$ pwd
/home/admin

This output means the user is currently inside the directory:

/home/admin

In IT environments, this helps administrators confirm their location before performing file operations.


3. Changing Directories (cd)

The primary command used to move between directories is:

cd

Meaning:
cd stands for Change Directory.

Syntax:

cd directory_name

Example:

cd /etc

This moves the user into the /etc directory.

After moving, running pwd would show:

/etc

4. Absolute Paths

An absolute path specifies the complete location of a directory starting from the root directory /.

Example:

cd /var/log

Explanation:

  • / → root directory
  • var → directory inside root
  • log → directory inside /var

Absolute paths always start with /.

Example IT use:

A system administrator reviewing log files might run:

cd /var/log

This command works regardless of the current directory.


5. Relative Paths

A relative path describes a directory location relative to the current directory.

It does not start with /.

Example:

If the current directory is:

/home/user

Command:

cd documents

The system moves to:

/home/user/documents

Relative paths are commonly used when working within a project directory.

Example in an IT environment:

A developer working inside /home/dev/project may move to a subdirectory:

cd src

6. Special Directory Symbols

Linux provides special symbols that simplify navigation.

Current Directory (.)

The dot . represents the current directory.

Example:

cd .

This command keeps the user in the same directory.

It is commonly used in scripting or command execution.

Example:

./script.sh

This runs a script located in the current directory.


Parent Directory (..)

Two dots .. represent the parent directory (one level up).

Example:

If current directory is:

/home/user/documents

Command:

cd ..

The new location becomes:

/home/user

This is very common when navigating back up the directory tree.


Home Directory (~)

The symbol ~ represents the user’s home directory.

Example:

cd ~

This takes the user directly to their home directory.

Example output:

/home/user

Example in an IT environment:

A user working in multiple directories may quickly return to their home directory using:

cd ~

7. Returning to the Previous Directory

Linux allows users to quickly return to the last directory they were in.

Command:

cd -

Example workflow:

cd /etc
cd /var/log
cd -

The system returns to:

/etc

This feature is useful when switching between two directories repeatedly.


8. Changing to the Home Directory

If the cd command is used without arguments, it automatically moves the user to the home directory.

Example:

cd

This is equivalent to:

cd ~

Example output:

/home/user

This is frequently used to reset the working location.


9. Navigating Multiple Levels

You can navigate through several directory levels in a single command.

Example:

cd /var/log/apache2

This moves directly from the current directory to /var/log/apache2.

Similarly, relative navigation can move through multiple levels:

Example:

cd ../../

Explanation:

  • First .. → move up one level
  • Second .. → move up another level

10. Tab Completion (Very Important for the Exam)

Linux shells support tab completion.

Pressing the Tab key automatically completes directory names.

Example:

cd /et

Press:

Tab

The shell completes it to:

cd /etc

If multiple matches exist, pressing Tab twice shows available options.

Benefits:

  • Faster navigation
  • Fewer typing errors
  • Helpful for long directory names

11. Checking Directory Contents Before Navigating

Before moving into a directory, users often list its contents.

Command:

ls

Example:

ls /var

Output may include:

log
tmp
cache
spool

Then the user can navigate into a directory:

cd /var/log

This is common practice when exploring unfamiliar directories.


12. Example Directory Navigation Workflow (IT Environment)

Example steps a system administrator might perform:

Step 1 — Check current directory

pwd

Step 2 — Move to system configuration directory

cd /etc

Step 3 — View directory contents

ls

Step 4 — Move back one level

cd ..

Step 5 — Go to log directory

cd /var/log

Step 6 — Return to home directory

cd ~

This type of navigation is performed frequently when managing Linux servers.


13. Important Commands Summary

CommandPurpose
pwdDisplays current directory
cd directoryMoves into a directory
cd /pathUses absolute path
cd directoryUses relative path
cd ..Moves to parent directory
cd .Refers to current directory
cd ~Moves to home directory
cdMoves to home directory
cd -Returns to previous directory

14. Key Points for the LPI Linux Essentials Exam

You should understand:

  • Linux directory structure starts at /
  • How to use pwd
  • How to navigate with cd
  • Difference between absolute paths and relative paths
  • Meaning of ., .., and ~
  • Using cd - to return to the previous directory
  • Navigating multiple directory levels
  • Using Tab completion

These concepts are essential for working with files and directories in Linux systems.

Buy Me a Coffee