| |

LFCS 7 ๐Ÿง Shell Navigation โ€” pwd, cd, ls

Every command you type in a shell runs in a directory. That directory is the working directory, and it determines what relative paths resolve to and where new files are created. Navigating the filesystemโ€”knowing where you are, moving to another directory, and listing what is thereโ€”is the most basic skill in the shell. The LFCS exam assumes it, and every task that touches files depends on it.

This chapter covers the three commands that form the navigation core: pwd, cd, and ls. You will learn what each one does, the options that matter, the special path names that save time, and the patterns that the exam expects you to know without thinking.

Key point: pwd prints the working directory. cd changes it. ls lists the contents of a directory. The special directories are . (current), .. (parent), ~ (home), and - (previous). The working directory is a property of the shell process, not the system; each shell has its own.


Why shell navigation matters

The relative path problem. A relative path is interpreted from the working directory. cat notes.txt reads notes.txt in the current directory. If the working directory is wrong, the command reads the wrong file or fails. Knowing where you are is a prerequisite for every relative path.

The task problem. The LFCS tasks often specify a file by its absolute path but expect you to work in a directory. Configuring a service might require editing a file in /etc/systemd/system/, which is easier after cd /etc/systemd/system. Navigating quickly and accurately is part of completing tasks quickly and accurately.

The listing problem. ls is the most-used command in the shell. It shows what is in a directory. The options change what is shown: hidden files, permissions, sizes, timestamps. Knowing the options is knowing how to inspect a directory before working in it.

The safety problem. Destructive commands like rm and mv operate on the working directory. Running rm -rf * in the wrong directory is a catastrophe. pwd before a destructive command is a habit that prevents it.

The script problem. A script’s working directory is not necessarily the directory the script is in. A script that uses relative paths must either cd to a known directory or use absolute paths. Understanding the working directory is understanding why a script fails when run from a different location.


a. pwd โ€” print the working directory

The pwd command prints the absolute path of the current working directory.

$ pwd
/home/alice

The output is the absolute path. There is no ambiguity. The command has two options: -L (logical) and -P (physical). The default is -L, which prints the path with symbolic links preserved.

$ cd /var/log
$ pwd
/var/log

If the current directory is reached through a symbolic link, pwd -L prints the link path and pwd -P prints the resolved path.

$ ln -s /var/log /tmp/loglink
$ cd /tmp/loglink
$ pwd
/tmp/loglink
$ pwd -P
/var/log

The -L form is the default. It prints the path as the shell tracked it. The -P form resolves all symlinks and prints the physical path. The difference matters when a script uses the path to construct another path, or when the path is used as a key.

The shell also stores the working directory in the PWD variable.

$ echo $PWD
/home/alice

The PWD variable is maintained by the shell. It matches the output of pwd -L. The OLDPWD variable holds the previous directory, which is what cd - returns to.


b. cd โ€” change the working directory

The cd command changes the working directory. It is a shell built-in, so the change affects the shell process.

$ cd /etc
$ pwd
/etc

The argument can be an absolute path (starting with /) or a relative path (starting with anything else). A relative path is resolved from the current working directory.

$ cd /etc
$ cd nginx
$ pwd
/etc/nginx

The special path names are shortcuts.

PathMeaning
.Current directory
..Parent directory
~Home directory
~userHome directory of user
-Previous directory
$ cd ..          # up one level
$ cd ../..       # up two levels
$ cd ~           # home
$ cd ~alice      # alice's home
$ cd -           # previous directory

The cd - command prints the directory it changed to, which makes it useful for switching back and forth.

$ cd /etc
$ cd /var
$ cd -
/etc

The cd command with no argument changes to the home directory, which is the value of HOME.

$ cd
$ pwd
/home/alice

If the argument is a file rather than a directory, cd fails with an error.

$ cd /etc/hosts
bash: cd: /etc/hosts: Not a directory

If the directory does not exist or the user does not have permission to enter it, cd fails with the corresponding error.

$ cd /nonexistent
bash: cd: /nonexistent: No such file or directory
$ cd /root
bash: cd: /root: Permission denied

The CDPATH variable is a list of directories that cd searches when the argument is a relative path. If CDPATH is set to /home/alice:/etc, then cd nginx finds /etc/nginx if it is not in the current directory. The feature is convenient but can surprise; most systems leave CDPATH unset.

The cd command has options. cd -P resolves symlinks and changes to the physical directory. cd -L keeps the logical path. The default is -L.


c. ls โ€” list directory contents

The ls command lists the contents of a directory. With no argument, it lists the current directory.

$ ls
Documents  Downloads  notes.txt  projects

The output is the names of the entries, sorted alphabetically. Hidden files (those beginning with .) are not shown by default.

The -a option shows all entries, including hidden files.

$ ls -a
.  ..  .bashrc  .profile  Documents  Downloads  notes.txt

The . and .. entries are shown. They are the current and parent directories.

The -l option shows the long format: permissions, link count, owner, group, size, modification time, and name.

$ ls -l
total 8
drwxr-xr-x 2 alice alice 4096 Oct  8 10:00 Documents
drwxr-xr-x 2 alice alice 4096 Oct  8 10:00 Downloads
-rw-r--r-- 1 alice alice  221 Oct  8 09:55 notes.txt
drwxr-xr-x 2 alice alice 4096 Oct  8 10:00 projects

The first character of the permission string is the file type: - for a regular file, d for a directory, l for a symlink, b for a block device, c for a character device, s for a socket, p for a pipe.

The -h option makes the sizes human-readable.

$ ls -lh
-rw-r--r-- 1 alice alice 221 Oct  8 09:55 notes.txt
-rw-r--r-- 1 alice alice 1.5M Oct  8 09:55 photo.jpg

The -R option lists subdirectories recursively.

$ ls -R
Documents:
report.pdf

Downloads:
archive.zip

The -d option lists the directory itself rather than its contents.

$ ls -ld /etc
drwxr-xr-x 145 root root 12288 Oct  8 09:00 /etc

The -t option sorts by modification time, newest first. The -S option sorts by size, largest first. The -r option reverses the order.

$ ls -lt        # newest first
$ ls -lS        # largest first
$ ls -ltr       # oldest first

The -i option shows the inode number. The -n option shows numeric UIDs and GIDs instead of names.

$ ls -li
131073 drwxr-xr-x 2 alice alice 4096 Oct  8 10:00 Documents

The ls command accepts multiple arguments and lists each in turn.

$ ls /etc /var

The output is grouped by argument. The -d option with multiple arguments lists the directories themselves.

$ ls -ld /etc /var
drwxr-xr-x 145 root root 12288 Oct  8 09:00 /etc
drwxr-xr-x  14 root root  4096 Oct  8 09:00 /var

The ls command does not show the contents of a directory it cannot read. The error is printed to standard error.

$ ls /root
ls: cannot open directory '/root': Permission denied

The --color option colorizes the output by file type. Most systems alias ls to ls --color=auto.


Complete Example Session

# ============================================
# PART 1: PWD
# ============================================
$ pwd
/home/alice
$ echo $PWD
/home/alice
# ============================================
# PART 2: CD ABSOLUTE
# ============================================
$ cd /etc
$ pwd
/etc
# ============================================
# PART 3: CD RELATIVE
# ============================================
$ cd /etc
$ cd nginx
$ pwd
/etc/nginx
$ cd ..
$ pwd
/etc
# ============================================
# PART 4: CD SPECIAL PATHS
# ============================================
$ cd ~
$ pwd
/home/alice
$ cd -
/etc
$ cd ../..
$ pwd
/
# ============================================
# PART 5: LS BASIC
# ============================================
$ ls
Documents  Downloads  notes.txt
$ ls -a
.  ..  .bashrc  Documents  Downloads  notes.txt
# ============================================
# PART 6: LS LONG
# ============================================
$ ls -l
total 8
drwxr-xr-x 2 alice alice 4096 Oct  8 10:00 Documents
-rw-r--r-- 1 alice alice  221 Oct  8 09:55 notes.txt
# ============================================
# PART 7: LS HUMAN READABLE
# ============================================
$ ls -lh
-rw-r--r-- 1 alice alice 221 Oct  8 09:55 notes.txt
-rw-r--r-- 1 alice alice 1.5M Oct  8 09:55 photo.jpg
# ============================================
# PART 8: LS SORTED
# ============================================
$ ls -lt
$ ls -lS
$ ls -ltr
# ============================================
# PART 9: LS DIRECTORY ITSELF
# ============================================
$ ls -ld /etc
drwxr-xr-x 145 root root 12288 Oct  8 09:00 /etc
# ============================================
# PART 10: LS MULTIPLE
# ============================================
$ ls /etc/hosts /etc/passwd
/etc/hosts  /etc/passwd

The ten parts covered pwd, cd with absolute paths, cd with relative paths, cd with special paths, basic ls, ls -l, ls -lh, sorted ls, ls -ld, and ls with multiple arguments.


Quick Reference

pwd Options

OptionEffect
pwdLogical path (default)
pwd -LLogical path
pwd -PPhysical path (symlinks resolved)

cd Special Paths

PathMeaning
.Current directory
..Parent directory
~Home directory
~userUser’s home directory
-Previous directory

cd Options

OptionEffect
cd -PResolve symlinks
cd -LKeep logical path (default)

ls Options

OptionEffect
-aAll entries, including hidden
-lLong format
-hHuman-readable sizes
-RRecursive
-dDirectory itself
-tSort by modification time
-SSort by size
-rReverse order
-iShow inode numbers
-nNumeric UID/GID

ls Long Format Columns

ColumnMeaning
1File type and permissions
2Link count
3Owner
4Group
5Size
6Modification time
7Name

Best Practices

โœ… Do This:

# Check the working directory before destructive commands
pwd                                                            # โœ…

# Use absolute paths for clarity in scripts
cd /etc/nginx                                                  # โœ…

# Use ~ for the home directory
cd ~/projects                                                  # โœ…

# Use cd - to switch back
cd /var/log && cd -                                            # โœ…

# Use ls -la to inspect a directory before working
ls -la /etc/nginx                                              # โœ…

# Use ls -lh for readable sizes
ls -lh /var/log                                                # โœ…

# Use ls -ld to check permissions on a directory
ls -ld /var/log                                                # โœ…

# Quote paths with spaces
cd "/home/alice/My Documents"                                  # โœ…

โŒ Don’t Do This:

# Don't run rm -rf in an unknown directory
rm -rf *                                                       # โŒ

# Don't assume the working directory in a script
# Use absolute paths or cd to a known location                  # โš ๏ธ

# Don't use cd with a file argument
cd /etc/hosts  # error                                          # โŒ

# Don't forget to quote paths with spaces
cd /home/alice/My Documents  # splits into two arguments        # โŒ

# Don't rely on CDPATH being set
cd nginx  # may not find /etc/nginx                             # โš ๏ธ

# Don't parse the output of ls in a script
# Use find or globs instead                                     # โš ๏ธ

# Don't use ls without -a when hidden files matter
ls  # misses .env, .gitconfig, etc.                             # โš ๏ธ

Common Pitfalls

PitfallWhy It HappensFix
cd fails silently in scriptSubshell or wrong directoryCheck $? or use cd dir || exit
Relative path resolves wrongWrong working directoryUse pwd or absolute path
Hidden files missedNo -a flagUse ls -a
Sizes unreadableNo -h flagUse ls -lh
Directory contents shown instead of directoryNo -d flagUse ls -ld
Symlink path confusingLogical vs physicalUse pwd -P
Spaces in path break commandNot quotedQuote the path

Real-World Examples

1. Check Location

pwd

2. Go Home

cd
pwd

3. Navigate to Config

cd /etc/systemd/system

4. Go Up Two Levels

cd ../..

5. Switch Back

cd -

6. List with Hidden Files

ls -la

7. List with Permissions

ls -l /etc/hosts

8. List Recursively

ls -R /etc/nginx

9. Check Directory Permissions

ls -ld /var/log

10. Sort by Time

ls -lt /var/log

Visual

Working Directory

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  FILESYSTEM                                                 โ”‚
โ”‚                                                             โ”‚
โ”‚  /                                                          โ”‚
โ”‚  โ”œโ”€โ”€ home                                                   โ”‚
โ”‚  โ”‚   โ””โ”€โ”€ alice          โ—€โ”€โ”€ working directory               โ”‚
โ”‚  โ”‚       โ”œโ”€โ”€ Documents                                      โ”‚
โ”‚  โ”‚       โ”œโ”€โ”€ Downloads                                      โ”‚
โ”‚  โ”‚       โ””โ”€โ”€ notes.txt                                      โ”‚
โ”‚  โ”œโ”€โ”€ etc                                                    โ”‚
โ”‚  โ”‚   โ”œโ”€โ”€ hosts                                              โ”‚
โ”‚  โ”‚   โ””โ”€โ”€ nginx                                              โ”‚
โ”‚  โ””โ”€โ”€ var                                                    โ”‚
โ”‚      โ””โ”€โ”€ log                                                โ”‚
โ”‚                                                             โ”‚
โ”‚  pwd โ†’ /home/alice                                          โ”‚
โ”‚  Relative path "notes.txt" โ†’ /home/alice/notes.txt          โ”‚
โ”‚  Relative path "../etc/hosts" โ†’ /home/etc/hosts (wrong)     โ”‚
โ”‚  Absolute path "/etc/hosts" โ†’ /etc/hosts                    โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

cd Path Resolution

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  START: /home/alice                                         โ”‚
โ”‚                                                             โ”‚
โ”‚  cd /etc           โ†’ /etc          (absolute)               โ”‚
โ”‚  cd nginx          โ†’ /etc/nginx    (relative to /etc)       โ”‚
โ”‚  cd ..             โ†’ /etc          (parent)                 โ”‚
โ”‚  cd ../..          โ†’ /            (two levels up)           โ”‚
โ”‚  cd ~              โ†’ /home/alice   (home)                   โ”‚
โ”‚  cd -              โ†’ /etc          (previous)               โ”‚
โ”‚  cd                 โ†’ /home/alice  (no argument, home)      โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ls Long Format

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  -rw-r--r-- 1 alice alice 221 Oct  8 09:55 notes.txt        โ”‚
โ”‚                                                             โ”‚
โ”‚  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚  โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€  โ”‚
โ”‚  permissions โ”‚  owner  group  size  mtime         name      โ”‚
โ”‚              โ”‚                                              โ”‚
โ”‚              โ””โ”€โ”€ link count                                 โ”‚
โ”‚                                                             โ”‚
โ”‚  First character:                                           โ”‚
โ”‚  -  regular file                                            โ”‚
โ”‚  d  directory                                               โ”‚
โ”‚  l  symlink                                                 โ”‚
โ”‚  b  block device                                            โ”‚
โ”‚  c  character device                                        โ”‚
โ”‚  s  socket                                                  โ”‚
โ”‚  p  pipe                                                    โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

ls Sorting Options

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ls          โ†’ alphabetical by name                         โ”‚
โ”‚  ls -t       โ†’ newest first                                 โ”‚
โ”‚  ls -tr      โ†’ oldest first                                 โ”‚
โ”‚  ls -S       โ†’ largest first                                โ”‚
โ”‚  ls -Sr      โ†’ smallest first                               โ”‚
โ”‚  ls -X       โ†’ alphabetical by extension                    โ”‚
โ”‚                                                             โ”‚
โ”‚  Combine with -l for the long format:                       โ”‚
โ”‚  ls -lt      โ†’ long format, newest first                    โ”‚
โ”‚  ls -lS      โ†’ long format, largest first                   โ”‚
โ”‚                                                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Summary

CommandPurpose
pwdPrint working directory
cdChange directory
lsList directory contents
ls -aInclude hidden files
ls -lLong format
ls -lhHuman-readable sizes
ls -laLong format, all files
ls -ldDirectory itself
ls -ltNewest first
ls -lSLargest first
cd ~Home directory
cd -Previous directory
cd ..Parent directory
pwd -PPhysical path

Key takeaways:

  • pwd prints the working directory. The output is the absolute path. The -P option resolves symlinks, and -L (the default) preserves them. The shell’s PWD variable holds the same value.
  • cd changes the working directory. The command is a shell built-in, so the change affects the shell. The argument can be absolute or relative. The special paths ., .., ~, and - are shortcuts.
  • cd - returns to the previous directory. The shell tracks OLDPWD, and the command prints the directory it changes to. It is the fastest way to switch between two directories.
  • ls lists directory contents. The -a option includes hidden files. The -l option shows the long format. The -h option makes sizes readable. The -d option lists the directory itself.
  • The long format has seven columns: permissions, link count, owner, group, size, modification time, and name. The first character of the permissions indicates the file type.
  • ls sorts alphabetically by default. The -t option sorts by modification time, -S by size, and -r reverses the order. The options combine: ls -ltr is long format, oldest first.
  • The working directory is a property of the shell process. Each shell has its own. A script’s working directory is not necessarily the directory the script is in. Use absolute paths or cd to a known location in scripts.

Remember: Navigation is the most basic shell skill, and it is the one the exam assumes without stating. pwd tells you where you are. cd moves you. ls shows you what is there. The three commands are simple, but the details matter: the special paths, the options, the difference between logical and physical paths, the sorting. Use them deliberately. Check pwd before destructive commands. Inspect with ls -la before working in a directory. Quote paths with spaces. And remember that a script’s working directory is not the script’s locationโ€”use absolute paths, and the script works regardless of where it is run from.



Stop using slow, ad-bloated tool sites! ๐Ÿคฎ

๐Ÿ”Ž Search “KandZ Tools” on Google to use many professional utilities for free.

KandZ.me is the ultimate minimalist hub for:
โœ… Finance (Mortgage, Interest, Inflation)
โœ… Tech (Base64, JSON, Dev Suite, IP)
โœ… Health (BMI, BMR, TDEE)
โœ… Productivity (Timer, Workspace, QR)

โšก๏ธ Fast & Private
๐Ÿ”’ No data leaves your device
๐Ÿ’Ž 100% Free

๐Ÿ”— Use it now: https://tools.kandz.me
๐Ÿ”– Bookmark itโ€”youโ€™ll need it later!