Skip to content
elephantoo

Navigating the filesystem

Lesson 4 of 31 12 min read

pwd, ls, cd, absolute vs relative paths, hidden files and tab completion.


Everything you do on the command line happens somewhere: in a current working directory. In this lesson you'll learn to see where you are, look around, move anywhere in the tree, and write paths correctly.

Practice area#

Create a small directory tree to explore. It's safe to run in your home directory:

Terminal
mkdir -p ~/playground/projects/website/css ~/playground/projects/api ~/playground/notes
touch ~/playground/notes/todo.txt ~/playground/notes/.secret ~/playground/projects/website/index.html
cd ~/playground

(You'll learn mkdir -p and touch properly in the next lesson.)

Where am I? pwd#

Terminal
pwd
Output
/home/ada/playground

pwd (print working directory) shows the absolute path of where you are. Your prompt usually shows it too, with ~ standing for your home directory.

What's here? ls#

Terminal
ls
ls notes
Output
notes  projects
todo.txt

The most useful options:

CommandShows
ls -llong format: permissions, owner, size, modification time
ls -ainclude hidden entries (names starting with .)
ls -hwith -l, human-readable sizes (4.0K, 12M)
ls -tnewest first (-tr: oldest first)
ls -Slargest first
ls -Rrecursive: every subdirectory too
ls -d dirthe directory itself, not its contents
ls -1one name per line (handy in scripts)

Options combine, so ls -lah is one of the most-typed commands on Linux:

Terminal
ls -la notes
Output
total 8
drwxr-xr-x 2 ada ada 4096 Oct  1 10:12 .
drwxr-xr-x 4 ada ada 4096 Oct  1 10:12 ..
-rw-r--r-- 1 ada ada    0 Oct  1 10:12 .secret
-rw-r--r-- 1 ada ada    0 Oct  1 10:12 todo.txt

Reading one long line from left to right:

Output
-rw-r--r--  1  ada  ada  0  Oct  1 10:12  todo.txt
│           │  │    │    │  │             └─ name
│           │  │    │    │  └─ last modified
│           │  │    │    └─ size in bytes
│           │  │    └─ group
│           │  └─ owner
│           └─ number of hard links
└─ type (- file, d directory, l link) and permissions

Permissions get their own lesson later. For a visual overview of a tree, install tree (sudo apt install tree):

Terminal
tree -a ~/playground
Output
/home/ada/playground
├── notes
│   ├── .secret
│   └── todo.txt
└── projects
    ├── api
    └── website
        ├── css
        └── index.html

6 directories, 3 files

Moving around: cd#

Terminal
cd projects/website     # into a subdirectory (relative path)
pwd
cd ..                   # up one level
pwd
cd ../notes             # up one, then into notes
pwd
cd /var/log             # anywhere, using an absolute path
pwd
cd -                    # back to the previous directory (prints it)
cd                      # plain cd: straight home
pwd
Output
/home/ada/playground/projects/website
/home/ada/playground/projects
/home/ada/playground/notes
/var/log
/home/ada/playground/notes
/home/ada

Tab completion is your best friend. Type cd pro and press Tab, and the shell completes projects/. Press Tab twice to list all possibilities. It's faster and prevents typos.

Absolute vs relative paths#

PathTypeMeaning
/home/ada/playground/notes/todo.txtabsolutestarts at /, so it works from anywhere
notes/todo.txtrelativerelative to the current directory
./notes/todo.txtrelativethe same, with an explicit . (current directory)
../notes/todo.txtrelative.. is the parent directory
~/playground/notes/todo.txthome-relative~ expands to your home directory
~bob/another user's home/home/bob

Use absolute paths in scripts and config files, because a script can be run from anywhere, and relative paths interactively, because they're shorter.

. matters when running programs in the current directory. The shell doesn't look there by default, so you type ./script.sh, not script.sh.

Jumping between directories with pushd/popd#

When you're bouncing between two places, the directory stack helps:

Terminal
cd ~/playground
pushd /etc > /dev/null     # remember where we were, go to /etc
pwd
popd > /dev/null           # return to the remembered directory
pwd
Output
/etc
/home/ada/playground

dirs -v lists the stack.

Seeing more about where you are#

Terminal
ls -ld ~/playground/projects    # details of the directory itself
du -sh ~/playground             # total size of a directory
file ~/playground/notes         # what is this?

stat name shows full details: size, permissions, owner, inode and timestamps.

Common mistakes#

  • cd into a file: cd notes/todo.txt gives "Not a directory".
  • Wrong case: cd Projects fails when the directory is projects.
  • Spaces in names: cd My Folder passes two arguments. Use cd "My Folder", cd My\ Folder or Tab completion.
  • Forgetting where you are before running something destructive. Run pwd first.

What's next#

You can look around. Next you'll create, copy, move, rename and delete files and directories, safely.

Check your understanding

Quick quiz

0/3 answered
  1. 1.You're in /home/ada/projects. Where does cd ../Downloads take you?

  2. 2.Which command shows hidden files, in long format, with human-readable sizes?

  3. 3.What does cd - do?

Finished reading?

Mark this lesson complete to track your progress.