Linux Paths and Navigation Cheat Sheet

Navigate Linux paths efficiently with cd, pushd/popd, globbing, basename, dirname, and canonical path tools.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Path Basics

Show current path via shell variable

Print current directory using shell state.

bashANYpathspwd
bash
echo "$PWD"

Get basename

Extract the last path component.

bashANYpathsbasename
bash
basename /var/log/nginx/access.log

Get dirname

Extract the parent directory path.

bashANYpathsdirname
bash
dirname /var/log/nginx/access.log

Resolve relative path from base

Produce canonical path using a base directory.

bashANYpathsrealpath
bash
realpath --relative-to=/var/log /var/log/nginx/access.log

Globbing

Match text files

Expand shell glob to files ending in .txt.

bashANYglobbingpatterns
bash
ls *.txt

Recursive glob in Bash

Match files recursively with globstar enabled.

bashANYbashglobbingrecursive
bash
shopt -s globstar && ls **/*.log

Include dotfiles in globbing

Enable matching hidden files with shell globs.

bashANYbashdotfilesglobbing
bash
shopt -s dotglob && printf '%s
' *

Brace expansion

Generate multiple path names quickly.

bashANYbrace-expansionpaths
bash
mkdir -p project/{src,tests,docs}

Directory Stack

Push and change directory

Change directory and push previous one onto stack.

bashANYpushdstacknavigation
bash
pushd /etc/nginx

Return to previous stacked directory

Pop top directory from stack and change to it.

bashANYpopdstacknavigation
bash
popd

Clear directory stack

Remove entries from shell directory stack.

bashANYdirsstack
bash
dirs -c

Path Recipes

Iterate over files safely

Use NUL delimiters for filenames with spaces/newlines.

bashANYfindsafe-shellpaths
bash
find . -type f -print0 | while IFS= read -r -d '' f; do printf '%s
' "$f"; done

Convert relative path to absolute path

Resolve a relative path from current directory.

bashANYrealpathpaths
bash
realpath ./scripts/deploy.sh

Test whether path exists

Check file or directory existence in shell scripts.

bashANYshellpathstests
bash
[[ -e /etc/hosts ]] && echo exists

More in Linux Paths and Navigation

No other published sheets yet.

Recommended next

No recommendations yet.