Catalog My learning Blog
Linux Command Line Tutorial for Developers · Phase 1 · Lesson 2 · 3 min read

Linux Navigation Commands: pwd, cd and ls

On this page
  1. The Linux directory tree
  2. Where am I? pwd
  3. Absolute vs relative paths
  4. Moving around with cd
  5. How to read ls -la output
  6. Useful ls options
  7. Keyboard shortcuts that save typing
  8. Check yourself

The Linux directory tree

Linux has a single directory tree starting at / (the root). Disks, USB drives and network shares are all mounted somewhere inside it; there are no drive letters. Every file has a path describing its place in that tree.

Where am I? pwd

Code
pwd
Code
/home/alice/app

pwd prints the current working directory. Every process has one, and relative paths are resolved from it.

Absolute vs relative paths

  • Absolute paths start with /: /home/alice/app/deploy.sh means the same file from anywhere.
  • Relative paths start from the current directory: from /home/alice, app/deploy.sh is the same file.
  • . is the current directory, .. the parent, ~ your home directory.

Scripts should use absolute paths (or compute them), because the directory they're run from varies. Cron jobs and systemd services usually start in / or /root, not where you expect (Days 19 and 20).

Moving around with cd

Shell
cd /var/log        # absolute
cd ../lib          # relative: /var/lib
cd ~               # home (or just cd)
cd -               # back to the previous directory

How to read ls -la output

Shell
cd ~/app && ls -la
Code
total 24
drwxr-xr-x 5 alice alice 4096 Oct 14 23:09 .
drwxr-x--- 3 alice alice 4096 Oct 14 23:08 ..
-rw-r--r-- 1 alice alice    0 Oct 14 23:09 .env
-rw-r--r-- 1 alice alice    0 Oct 14 23:09 README.md
-rw-r--r-- 1 alice alice   15 Oct 14 23:09 deploy.sh
drwxr-xr-x 2 alice alice 4096 Oct 14 23:09 logs
drwxr-xr-x 2 alice alice 4096 Oct 14 23:09 src
ColumnExampleMeaning
Type and permissionsdrwxr-xr-xd directory, - file, l symlink; then permissions (Day 4)
Link count5Hard links to this inode (Day 13); for directories, roughly subdirectories + 2
Owner, groupalice aliceWho owns it (Day 5)
Size15Bytes; directories show their own metadata size, not their contents
Modified timeOct 14 23:09Last content change
Name.envNames starting with . are hidden from plain ls

-a shows hidden files like .env and the . and .. entries; -l gives the long format.

Paths are resolved from the root for absolute paths or from the current directory for relative ones; ls -la shows type, permissions, links, owner, size, time and name for each entry.
Paths are resolved from the root for absolute paths or from the current directory for relative ones; ls -la shows type, permissions, links, owner, size, time and name for each entry.

Useful ls options

  • ls -lh human-readable sizes (3.2M).
  • ls -lt newest first; ls -ltr oldest first (newest at the bottom, handy in log directories).
  • ls -ld dir shows the directory itself, not its contents.
  • tree -L 2 (install it) for a quick overview of a project.
  • stat file for every detail: exact times, inode, permissions in octal.

Keyboard shortcuts that save typing

  • Tab completes file and directory names: type cd /var/lo and press Tab to get /var/log/. Press it twice to list the options when there's more than one. It also prevents typos.
  • Up arrow brings back previous commands; Ctrl+R searches your history as you type (Ctrl+R ssh finds your last ssh command).
  • Ctrl+A and Ctrl+E jump to the start and end of the line; Ctrl+C abandons the current line.

Check yourself

/home. Each .. moves up one level: the first from /home/alice/app to /home/alice, the second from /home/alice to /home.

They resolve from the current directory, which differs depending on who or what runs the script, such as cron or systemd.

Names starting with a dot are hidden by convention, which keeps configuration files out of everyday listings. ls -a shows them, and ls -A shows them without . and ...

It switches back to the previous directory, which the shell keeps in $OLDPWD, and prints where you landed. It's handy for jumping between two locations, such as a project folder and /var/log.

The first character is the file type, such as - for a file, d for a directory and l for a symlink. The number after the permissions is the link count.