Class 29 of 55 • Linux Commands Hub

Linux Directory Commands: pwd, cd, ls, mkdir, rmdir & tree

 1. Mastering Linux Directory Navigation

 In Linux and Unix operating systems, file directories (commonly called "folders" in graphical operating systems) are organized in a single unified hierarchical tree rooted at /. Unlike Windows—where physical drive partitions are isolated into lettered drives like C:\ and D:\—Linux navigates through folders using forward slashes (/).

 Becoming a proficient Linux system administrator, developer, or DevOps engineer starts with mastering the foundational directory manipulation commands: pwd, cd, ls, mkdir, rmdir, and tree.

 2. Print Working Directory (pwd)

 The pwd command prints the full, unambiguous path of your current working directory starting from root (/). This ensures you always know your exact location in the filesystem before executing sensitive file operations.

# Print logical path (following symlinks):
pwd
# Output: /home/student/projects

# Print physical physical path (resolving all symbolic links):
pwd -P

 3. Changing Directories (cd)

 The cd command changes the shell's active working directory. You can navigate using either Absolute Paths (starting from /) or Relative Paths (relative to your current position):

Command Syntax Navigation Target
cd /var/log/nginx Navigates using an Absolute Path directly to the target folder.
cd ~ or simply cd Jumps instantly to the current user's personal home directory ($HOME).
cd .. Moves up one level to the Parent Directory.
cd ../.. Moves up two directory levels in a single command.
cd - Jumps back to the Previous Directory (like a browser back button, using $OLDPWD).
cd /home/student/projects/src Navigates down to subfolders using relative paths.

 4. Listing Directory Contents (ls)

 The ls command lists the files, directories, and permissions in your current or specified path. Mastering its flags is critical for system diagnostics:

# Standard short listing:
ls

# Long format listing with file permissions, owner, size, and timestamp:
ls -l

# Show hidden files (files beginning with a dot, e.g., .bashrc):
ls -a

# Human-readable file sizes (KB, MB, GB):
ls -lh

# Sort files by modification date (newest first):
ls -lt

# Sort files by file size (largest first):
ls -lS

# List contents recursively through all subdirectories:
ls -R

 5. Creating Directories (mkdir)

 The mkdir (make directory) command creates one or more new directories. The most essential flag for students and administrators is -p (parents):

# 1. Create a single directory:
mkdir web_project

# 2. Create multiple directories at once:
mkdir html css js images

# 3. Create deeply nested folder hierarchies with -p (Parents):
mkdir -p app/src/controllers app/src/models app/docs

# 4. Create directories with brace expansion:
mkdir -p project/{frontend,backend}/{src,tests,build}

# 5. Set custom octal permissions during directory creation:
mkdir -m 700 private_keys
# (Only the owner can read, write, or enter this directory!)

 6. Removing Directories (rmdir vs. rm -r)

 Linux provides two distinct tools for folder deletion with very different safety profiles:

Command Safety Level Behavior & When to Use
rmdir folder 100% Safe Removes a directory ONLY if it is completely empty. If any file exists inside, it aborts with Directory not empty, preventing accidental data loss.
rmdir -p a/b/c Safe Recursive Deletes empty subdirectories and their empty parent directories sequentially.
rm -r folder Destructive Recursively deletes the directory AND all of its contents (files, subfolders). Use with extreme caution.

 7. Visualizing Hierarchies with the 'tree' Command

 The tree utility displays files and folders as a clean, colorized ASCII graphical tree structure. If not pre-installed on your system, install it with sudo apt install tree or sudo dnf install tree.

# Render the complete folder tree of current directory:
tree

# Limit recursion depth to 2 levels (prevents terminal screen flooding):
tree -L 2

# Display only directories (hiding individual files):
tree -d

# Show human-readable file sizes alongside files:
tree -h

 8. Practical Lab: Building & Managing a Web App Directory

 Follow this step-by-step exercise in your terminal to practice real-world directory operations:

# 1. Verify where you are:
pwd

# 2. Navigate to /tmp:
cd /tmp

# 3. Create a multi-tiered directory structure in a single command:
mkdir -p devops_lab/{config,logs,web/{html,css,js},database/backups}

# 4. View your created tree:
tree devops_lab

# 5. Enter the web html folder:
cd devops_lab/web/html
pwd

# 6. Jump back to the previous folder:
cd -

# 7. Try deleting a non-empty folder safely (will fail safely):
rmdir devops_lab/web
# Fails: rmdir: failed to remove 'devops_lab/web': Directory not empty

# 8. Clean up the test lab safely:
rm -rf /tmp/devops_lab

 9. Student Q&A and Viva Preparation

The pwd (Print Working Directory) command outputs the full absolute pathname of the directory you are currently working in, starting from the system root directory (/).
An absolute path begins from the root directory (/) and specifies the complete path to a file regardless of your current location (e.g. /var/log/nginx). A relative path starts from your current working directory without a leading slash (e.g. ../images or docs/notes.txt).
The -p (parents) flag allows mkdir to create all missing parent directories along the path in a single execution without throwing errors if the directories already exist (e.g. mkdir -p project/src/utils).
rmdir is a safe command that deletes directories ONLY if they are completely empty. rm -r (recursive remove) forcefully deletes a directory along with all of its files, subdirectories, and contents, regardless of whether it is empty.
cd - switches your current working directory to the previously visited directory (utilizing the $OLDPWD environment variable), functioning like a back button in a web browser.

 10. Summary & Next Steps

 You now possess complete control over navigating, creating, and inspecting directory structures in Linux. In the next tutorial, we examine the fundamental commands used to create, inspect, search, and manipulate individual data files in Class 30: Linux File Management Commands Hub (touch, cp, mv, rm, cat, grep, find).

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!