Paths describe file locations. Bash distinguishes absolute paths (starting with /) and relative paths (from the current directory).
Navigation
pwd
cd /tmp
pwd
cd -
pwdcd - jumps to the previous directory—handy when toggling between project roots.
Special path tokens
cd ~
echo "$HOME"
cd .
cd ..
pwd
ls -la .. is current directory; .. is parent; ~ expands to home.
Basename and dirname
path="/var/log/system.log"
echo "dir: $(dirname "$path")"
echo "file: $(basename "$path")"Quote variables so paths with spaces survive word splitting.
Important interview questions and answers
- Q: Absolute vs relative?
A: Absolute starts at filesystem root; relative starts from current working directory. - Q: Why quote $path?
A: Spaces or glob characters in paths break unquoted expansions.
Self-check
- What command prints the working directory?
- What does .. mean in a path?
Tip: Quote paths: cd "$dir"—spaces in filenames break unquoted expansions.
Interview prep
- Absolute path?
Starts at filesystem root /.
- dirname/basename?
Split path into directory and file components.