Navigating the Filesystem

Find your way around a Linux filesystem: what lives in each top-level directory, absolute and relative paths, cd shortcuts, symlinks and pwd -P, ls options, and pushd/popd.

By The Dark DebuggerUpdated 25 min read

This article teaches you to move around a Linux system with confidence. It covers where things live in the directory tree, how paths work, how cd, pwd and ls behave (including around symbolic links), and how to jump between directories with a stack. You should have a working terminal from Setting Up Bash and Your Terminal and know what a command, option and argument are from the introduction. The next article, Working with Files and Directories, builds on everything here.

Why navigation comes first

Every file command works on a path, and most of the time that path is relative to where you currently are. Running rm -r logs is fine in a project directory and a disaster in the wrong one. Before anything else, you need to always know where you are and how to get where you're going.

The second reason is servers. You log into a machine you've never seen, and you need to find the web server's configuration, the application's logs, and which version of the app is actually deployed. Nobody gives you a map. But Linux systems follow a common layout, so once you know it, you can usually guess the right directory on the first try.

The examples use a small web shop project. The source lives in ~/projects/webshop, and a deployed copy lives in ~/srv/webshop with the release-directory layout that many deployment tools use. Here's the project, shown with tree:

tree ~/projects/webshop
/home/sid/projects/webshop
├── README.md
├── config
│   ├── app.conf
│   └── nginx.conf
├── deploy.sh
├── docs
│   └── runbook.md
├── logs
│   ├── access.log
│   ├── access.log.1
│   ├── access.log.2.gz
│   └── error.log
└── src
    ├── api
    │   ├── orders.py
    │   └── products.py
    └── app.py
 
6 directories, 12 files

tree isn't installed by default on Ubuntu (sudo apt install tree). It's worth having for getting an overview of an unfamiliar directory. Useful options are -L 2 to limit the depth, -d for directories only and -a to include hidden files.

One tree, starting at /

Linux has no drive letters. Everything, including other disks, USB sticks and network shares, hangs off a single tree whose top is /, the root directory:

ls /
bin                dev   init               lib64       mnt   root  sbin.usr-is-merged  sys  var
bin.usr-is-merged  etc   lib                lost+found  opt   run   snap                tmp
boot               home  lib.usr-is-merged  media       proc  sbin  srv                 usr

Other disks appear as directories inside the tree, at their mount points. Under WSL your Windows C: drive is mounted at /mnt/c:

ls /mnt
c
wsl
wslg

A few entries above are specific to this machine. init is WSL's startup program, and the *.usr-is-merged directories are leftovers from Ubuntu's move to a merged /usr, which the long listing makes visible:

ls -l /
total 3480
lrwxrwxrwx   1 root root       7 Apr 22  2024 bin -> usr/bin
[…]
lrwxrwxrwx   1 root root       7 Apr 22  2024 lib -> usr/lib
[…]
dr-xr-xr-x 225 root root       0 Nov  9 21:38 proc
drwx------   5 root root    4096 Jul 27  2025 root
[…]
lrwxrwxrwx   1 root root       8 Apr 22  2024 sbin -> usr/sbin
[…]
drwxrwxrwt  12 root root   12288 Nov  9 21:50 tmp
drwxr-xr-x  12 root root    4096 Jan  6  2025 usr
drwxr-xr-x  13 root root    4096 Jul 21  2025 var

/bin, /sbin and /lib are just links into /usr. On a modern distribution, /bin/ls and /usr/bin/ls are the same file.

What lives where

The layout follows the Filesystem Hierarchy Standard, summarised on your machine in man hier (also online at man7.org). These are the directories you'll actually visit:

DirectoryWhat it holdsYou'll look here for
/etcSystem-wide configuration, as plain text files/etc/nginx/nginx.conf, /etc/ssh/sshd_config, /etc/hostname, /etc/passwd
/var/logLog filessyslog, auth.log (logins and sudo), nginx/access.log
/var/libPersistent state of servicesDatabase files (/var/lib/postgresql), package manager state
/var/cache, /var/spoolCaches; queued mail and print jobsUsually safe to clean / usually not
/homeOne directory per user (your ~)Your projects and dotfiles
/rootThe root user's home, readable only by rootNot /home/root
/tmpTemporary files, writable by everyone, often wiped at bootScratch files, sockets
/usr/bin, /usr/sbinPrograms installed by the package manager (sbin: admin tools)ls, bash, python3
/usr/lib, /usr/shareLibraries; architecture-independent dataMan pages live in /usr/share/man
/usr/localSoftware you installed yourself, outside the package managerSame layout as /usr: bin, lib, share
/optSelf-contained third-party packages/opt/<vendor>/<app>
/srvData served by this machineSome admins put websites here; often empty
/proc, /sysVirtual files generated by the kernel, nothing on diskProcess info, hardware info
/devDevice files/dev/null, disks like /dev/sda
/runRuntime state since bootPID files, sockets
/bootKernel and boot loader(Empty under WSL, where Windows supplies the kernel)
/mnt, /mediaMount points for other filesystems/mnt/c in WSL, USB drives under /media

Two of these show up in everyday work. /root is locked down, and /tmp is open to everyone but has the sticky bit (the t at the end of drwxrwxrwt), so users can't delete each other's files there:

ls -ld /root /tmp
ls /root
drwx------  5 root root  4096 Jul 27  2025 /root
drwxrwxrwt 12 root root 12288 Nov  9 21:50 /tmp
ls: cannot open directory '/root': Permission denied

And /proc really is generated on the fly. ls reports /proc/loadavg as an empty file, but it has content:

cat /proc/loadavg
ls -l /proc/loadavg
10.02 8.56 4.78 3/425 30570
-r--r--r-- 1 root root 0 Nov  9 21:39 /proc/loadavg

The kernel produces the text at the moment you read it. Tools like uptime, top and free get their numbers by reading files in /proc.

Where am I? pwd

Every shell has a current working directory. Relative paths are resolved against it, and new files land in it. pwd (print working directory) shows it, and Bash also keeps it in $PWD:

pwd
echo "$PWD"
/home/sid
/home/sid

Ubuntu's default prompt already shows the directory (sid@devbox:~/projects/webshop$), with ~ standing for your home directory. Still, run pwd before anything destructive. It costs nothing.

Absolute and relative paths

A path says how to reach a file. There are two kinds:

  • An absolute path starts with / and is spelled out from the root: /home/sid/projects/webshop/config/app.conf. It means the same thing no matter where you are.
  • A relative path doesn't start with / and is resolved from the current directory: config/app.conf means "the config directory inside wherever I am now".

Two special names exist in every directory: . is the directory itself and .. is its parent. They're how relative paths move up the tree:

cd ~/projects/webshop/src/api
pwd
cd ../../config
pwd
echo "$OLDPWD"
/home/sid/projects/webshop/src/api
/home/sid/projects/webshop/config
/home/sid/projects/webshop/src/api

../../config means: up to src, up to webshop, then down into config. $OLDPWD holds the previous directory, which we'll use in a moment.

You don't have to cd somewhere to use a relative path. Any command accepts them, and realpath shows what one resolves to:

ls ../logs
realpath ../logs
access.log  access.log.1  access.log.2.gz  error.log
/home/sid/projects/webshop/logs

When to use which: relative paths for interactive work, because they're short. Absolute paths in scripts, cron jobs, systemd units and config files, which run from a working directory you don't control. A script that does cat config/app.conf works when you start it from the project directory and fails when cron starts it from /.

Why ./script needs the ./

cd ~/projects/webshop
deploy.sh
./deploy.sh
deploy.sh: command not found
Deploying webshop from /home/sid/projects/webshop

When a command name contains no /, Bash looks for it only in the directories listed in $PATH (we cover PATH in Variables, Quoting, and the Environment). The current directory isn't in PATH, deliberately: otherwise anyone who could drop a file called ls into a shared directory could get you to run it. ./deploy.sh is a path containing a /, so Bash runs exactly that file.

Shortcuts: ~, cd - and cd on its own

You typeGoes to
cdYour home directory
cd ~ or cd ~/projectsHome, or a path under it (~ expands to $HOME)
cd ~rootAnother user's home directory (~user)
cd -The previous directory, and prints it
cd ..The parent directory
cd /The root directory

cd - is the one that saves the most time. It toggles between two directories, so you can jump between a config directory and a log directory without typing either path again:

cd ~/projects/webshop
cd /var/log
cd -
cd -
cd
pwd
echo ~root
/home/sid/projects/webshop
/var/log
/home/sid
/root

The first two lines are printed by the two cd - commands. cd with no argument took us home, and ~root expanded to root's home directory. Expanding ~root doesn't require being able to enter it.

A symbolic link (symlink) is a small file that points to another path. Deployment tools use them heavily. Each release goes into its own directory, and a link called current points at the live one, so switching versions (or rolling back) means changing one link. Here's the deployed web shop:

cd ~/srv/webshop
ls -l
total 8
lrwxrwxrwx 1 sid sid   15 Nov  6 18:07 current -> releases/v1.4.2
drwxr-xr-x 4 sid sid 4096 Nov  6 18:07 releases
drwxr-xr-x 4 sid sid 4096 Nov  6 18:07 shared

The l at the start of the line and the -> arrow mark the symlink. Now go into it:

cd current
pwd
pwd -P
/home/sid/srv/webshop/current
/home/sid/srv/webshop/releases/v1.4.2

Bash tracks the logical path, the way you got here (through current). pwd -P asks for the physical path, with every symlink resolved. When someone asks "which release is live?", the answer is in pwd -P.

This is where it gets confusing. Watch what .. means to ls and to cd from inside current:

ls ..
cd ..
pwd
v1.4.1  v1.4.2
/home/sid/srv/webshop

ls .. listed releases, the physical parent of v1.4.2. But cd .. went back to ~/srv/webshop, the logical parent, because cd follows the path you used to get in. Both are "correct", and it trips everyone up at least once. Most commands (ls, cp, cat ../file) work with physical paths. Only cd and pwd work with the logical one by default. To make cd resolve links too, use cd -P:

cd current
cd -P ..
pwd
/home/sid/srv/webshop/releases

To ask where a link points without going into it, use readlink (prints the link's target as stored) or realpath (resolves it fully to an absolute path):

cd ~/srv/webshop
readlink current
realpath current
releases/v1.4.2
/home/sid/srv/webshop/releases/v1.4.2

How links are created, and the difference between symbolic and hard links, is covered in Working with Files and Directories.

ls: seeing what's there

Plain ls lists names in columns, sorted alphabetically:

cd ~/projects/webshop
ls
ls -a
ls -A
ls -F
README.md  config  deploy.sh  docs  logs  src
.  ..  .gitignore  README.md  config  deploy.sh  docs  logs  src
.gitignore  README.md  config  deploy.sh  docs  logs  src
README.md  config/  deploy.sh*  docs/  logs/  src/
  • Names starting with a dot are hidden from plain ls. That's the only thing that makes a file "hidden" on Linux. -a shows everything, including . and ... -A shows hidden files but leaves out . and .., which is usually what you want.
  • -F adds a marker to each name: / for directories, * for executables, @ for symlinks (ls -F ~/srv/webshop prints current@ releases/ shared/). It's useful when colours aren't available, for example in a log or an email.

ls -l, column by column

The long format is the one you'll use most:

ls -l
total 24
-rw-r--r-- 1 sid sid  116 Oct 30 16:45 README.md
drwxr-xr-x 2 sid sid 4096 Nov  4 14:20 config
-rwxr-xr-x 1 sid sid  142 Nov  6 18:05 deploy.sh
drwxr-xr-x 2 sid sid 4096 Mar 14  2026 docs
drwxr-xr-x 2 sid sid 4096 Nov  9 10:17 logs
drwxr-xr-x 3 sid sid 4096 Nov  2 09:30 src

Take the deploy.sh line apart:

FieldValueMeaning
Type-The first character: - regular file, d directory, l symlink (more below)
Permissionsrwxr-xr-xRead/write/execute for the owner, the group and everyone else. Covered in File Permissions and Ownership
Link count1Number of hard links to this file. For a directory it's 2 plus the number of subdirectories
OwnersidThe user who owns the file
GroupsidThe group that owns the file
Size142Size in bytes. For directories, 4096 is the size of the directory's own name table, not the size of its contents
ModifiedNov 6 18:05Last modification time. Files older than about six months (like docs) show the year instead of the time
Namedeploy.shThe file name, followed by -> target for symlinks

The total 24 line is the disk space used by the listed entries, in 1 KiB blocks. Don't confuse it with a file count.

Directory sizes trip people up regularly. ls -l will never tell you how much space a directory's contents take. That's what du -sh is for, which comes up in the next article.

The options that matter

Sizes in bytes are hard to read at a glance. -h makes them human-readable:

ls -lh logs
total 628K
-rw-r--r-- 1 sid sid 216K Nov  9 10:17 access.log
-rw-r--r-- 1 sid sid 365K Nov  8 23:59 access.log.1
-rw-r--r-- 1 sid sid  34K Nov  7 23:59 access.log.2.gz
-rw-r--r-- 1 sid sid 5.4K Nov  9 10:16 error.log

-t sorts by modification time, newest first, and -r reverses any sort. ls -ltr is a habit worth building for log directories, because the newest file ends up at the bottom, right above your prompt:

ls -lt logs
ls -ltr logs
total 628
-rw-r--r-- 1 sid sid 221139 Nov  9 10:17 access.log
-rw-r--r-- 1 sid sid   5467 Nov  9 10:16 error.log
-rw-r--r-- 1 sid sid 373019 Nov  8 23:59 access.log.1
-rw-r--r-- 1 sid sid  34541 Nov  7 23:59 access.log.2.gz
total 628
-rw-r--r-- 1 sid sid  34541 Nov  7 23:59 access.log.2.gz
-rw-r--r-- 1 sid sid 373019 Nov  8 23:59 access.log.1
-rw-r--r-- 1 sid sid   5467 Nov  9 10:16 error.log
-rw-r--r-- 1 sid sid 221139 Nov  9 10:17 access.log

-S sorts by size, largest first. It's the quick way to see which log is eating the disk:

ls -lS logs
total 628
-rw-r--r-- 1 sid sid 373019 Nov  8 23:59 access.log.1
-rw-r--r-- 1 sid sid 221139 Nov  9 10:17 access.log
-rw-r--r-- 1 sid sid  34541 Nov  7 23:59 access.log.2.gz
-rw-r--r-- 1 sid sid   5467 Nov  9 10:16 error.log

When you give ls a directory, it lists the directory's contents. To see the directory entry itself (its permissions, owner, date), add -d:

ls -l config
ls -ld config
total 8
-rw-r--r-- 1 sid sid  79 Nov  4 14:20 app.conf
-rw-r--r-- 1 sid sid 125 Nov  4 14:20 nginx.conf
drwxr-xr-x 2 sid sid 4096 Nov  4 14:20 config

Notice that ls -l config didn't show .env, which is in that directory, because it's hidden. Hidden config files that contain secrets are a common surprise. ls -la shows them:

ls -la config
total 20
drwxr-xr-x 2 sid sid 4096 Nov  4 14:20 .
drwxr-xr-x 6 sid sid 4096 Nov  6 18:05 ..
-rw-r--r-- 1 sid sid   42 Nov  4 14:20 .env
-rw-r--r-- 1 sid sid   79 Nov  4 14:20 app.conf
-rw-r--r-- 1 sid sid  125 Nov  4 14:20 nginx.conf

The rest, briefly:

ls -1 logs
ls -l --time-style=long-iso docs
ls -R src
access.log
access.log.1
access.log.2.gz
error.log
total 4
-rw-r--r-- 1 sid sid 46 2026-03-14 11:02 runbook.md
src:
api  app.py
 
src/api:
orders.py  products.py
OptionEffect
-lLong format
-a / -AInclude hidden files (with / without . and ..)
-hHuman-readable sizes (with -l)
-tSort by modification time, newest first
-SSort by size, largest first
-rReverse the sort order
-dList directories themselves, not their contents
-1One name per line
-FAppend a type marker: / directory, * executable, @ symlink
-RRecurse into subdirectories
-iShow inode numbers (used in the next article)
--time-style=long-isoUnambiguous YYYY-MM-DD HH:MM dates
--group-directories-firstDirectories before files

Options combine freely: ls -lhtr, ls -lAh, ls -ld */. One warning: ls is for people. In scripts, use globs (for f in logs/*.log) or find, not ls output. Names with spaces or newlines break that kind of parsing. See Wildcards, Globbing, and Brace Expansion and Finding Files.

File types

The first character of ls -l tells you what kind of object you're looking at. You'll meet all seven eventually:

CharTypeExample
-Regular fileText, images, programs, archives: all the same type to the kernel
dDirectoryconfig
lSymbolic linkcurrent -> releases/v1.4.2
cCharacter device/dev/null, terminals
bBlock deviceDisks such as /dev/sda
pNamed pipe (FIFO)Created with mkfifo
sSocketLocal service endpoints under /run
ls -l /dev/null /dev/sda /run/dbus/system_bus_socket
mkfifo deploy.fifo
ls -l deploy.fifo
crw-rw-rw- 1 root root 1, 3 Nov  9 21:39 /dev/null
brw-rw---- 1 root disk 8, 0 Nov  9 21:39 /dev/sda
srw-rw-rw- 1 root root    0 Nov  9 21:39 /run/dbus/system_bus_socket
prw-r--r-- 1 sid sid 0 Nov  9 21:50 deploy.fifo

For devices, the size column shows two numbers (1, 3), the device's major and minor numbers, instead of a size.

A "regular file" can contain anything, and the extension doesn't decide what it is. Linux doesn't care what a file is called. To find out what's actually inside, use file, which looks at the content:

file README.md deploy.sh src/app.py ~/srv/webshop/current /usr/bin/ls
README.md:                     ASCII text
deploy.sh:                     Bourne-Again shell script, ASCII text executable
src/app.py:                    Python script, ASCII text executable
/home/sid/srv/webshop/current: symbolic link to releases/v1.4.2
/usr/bin/ls:                   ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=44900829b39271d878615772bb2a6ffa422f0891, for GNU/Linux 3.2.0, stripped

Give a text file a misleading name and file isn't fooled:

cp logs/error.log docs/report.pdf
file docs/report.pdf
docs/report.pdf: ASCII text

Use file before opening something unknown with cat. Binary data printed to a terminal can garble your terminal settings.

Jumping around with pushd, popd and dirs

cd - remembers one previous directory. When you're working across three or four places (a project, its logs, the deployed config), Bash's directory stack remembers as many as you like. pushd dir goes to dir and pushes it onto the stack. popd removes the top entry and returns to the next one. Both print the stack, with ~ for your home directory:

cd ~/projects/webshop
pushd /var/log
pushd ~/srv/webshop/shared/config
dirs -v
popd
/var/log ~/projects/webshop
~/srv/webshop/shared/config /var/log ~/projects/webshop
 0  ~/srv/webshop/shared/config
 1  /var/log
 2  ~/projects/webshop
/var/log ~/projects/webshop

dirs -v numbers the entries. Entry 0 is always the current directory. pushd with no argument swaps the top two entries, which makes it a cd - that works on the stack:

pushd
pushd
popd
popd
~/projects/webshop /var/log
/var/log ~/projects/webshop
~/projects/webshop
bash: popd: directory stack empty

The stack only holds the directories you pushed plus the current one, so popping one time too many gives an error. With a longer stack, pushd +2 rotates it so that entry 2 comes to the top, taking you there. ~2 expands to the path of entry 2, so cd ~2 or cp app.conf ~2/ work as well.

pushd and popd are also useful in scripts that need to do some work in another directory and then come back. Add > /dev/null to hide the stack printout there. The more common script idiom, a subshell ( cd dir && do_work ), appears later in Command Substitution, Chaining, and Exit Codes.

Where you'll use this

Here's a realistic task: you've been asked to look at the web shop on a server you don't know. Three questions: which version is live, where is its configuration really stored, and which logs changed most recently? Navigation alone answers all three.

cd ~/srv/webshop
cat current/VERSION
ls -l current/
1.4.2
total 12
-rw-r--r-- 1 sid sid    6 Nov  6 18:07 VERSION
drwxr-xr-x 2 sid sid 4096 Nov  6 18:07 api
-rw-r--r-- 1 sid sid  150 Nov  6 18:07 app.py
lrwxrwxrwx 1 sid sid   19 Nov  6 18:07 config -> ../../shared/config
lrwxrwxrwx 1 sid sid   17 Nov  6 18:07 logs -> ../../shared/logs

Version 1.4.2 is live. Its config and logs are themselves symlinks into shared, the standard pattern that keeps configuration and logs in place across releases. The link targets are relative (../../shared/config), so they're resolved from the directory the link lives in. Follow one and ask for the physical location:

cd current/config
pwd
pwd -P
/home/sid/srv/webshop/current/config
/home/sid/srv/webshop/shared/config

If you edit app.conf here, you're editing the shared copy, which every release uses. That's worth knowing before you edit it. Finally, the logs, newest first:

ls -lt ~/srv/webshop/shared/logs
total 224
-rw-r--r-- 1 sid sid 221139 Nov  9 10:17 access.log
-rw-r--r-- 1 sid sid   5467 Nov  9 10:16 error.log

The same approach works for system services. Configuration is under /etc/<service>, and the system logs are in /var/log. Listing that directory by time shows what's been active:

ls -lt /var/log | head -6
total 3076
-rw-r-----  1 syslog    adm             972247 Nov  9 21:50 syslog
-rw-rw-r--  1 root      utmp            291072 Nov  9 21:50 wtmp
-rw-r-----  1 syslog    adm               8548 Nov  9 21:50 auth.log
-rw-r-----  1 syslog    adm             150612 Nov  9 21:48 kern.log
-rw-r-----  1 root      adm              35295 Nov  9 21:39 dmesg

(Reading syslog and auth.log needs membership of the adm group or sudo. The -rw-r----- permissions say so.)

Common mistakes

Quoting the tilde

cd "~/projects"
bash: cd: ~/projects: No such file or directory

~ is only expanded when it's unquoted and at the start of a word. Inside quotes it's a literal directory named ~. Put the quotes after it, or use $HOME, which does expand inside double quotes:

cd ~/projects/"Client Docs"
cd "$HOME/projects/Client Docs"

Names with spaces

mkdir -p "Client Docs"
cd Client Docs
cd "Client Docs"
pwd
bash: cd: too many arguments
/home/sid/projects/Client Docs

Without quotes, Bash passes Client and Docs as two arguments, and cd only accepts one. Quote the name, or let Tab completion type it for you. Completion inserts the backslash escape (Client\ Docs) automatically.

ls .. and cd .. disagree

Inside a symlinked directory, ls .. shows the physical parent while cd .. goes to the logical one, as shown in the symlink section above. If a relative path behaves differently in cd and in another command, compare pwd with pwd -P.

Permission denied, or not a directory

cd /root
cd ~/projects/webshop/README.md
bash: cd: /root: Permission denied
bash: cd: /home/sid/projects/webshop/README.md: Not a directory

The first is about permissions: entering a directory needs execute (x) permission on it, and /root is drwx------. Note that sudo cd /root doesn't work either, because cd is a shell builtin and sudo can only run programs. The second just means the path ends in a file. Use ls or cat on files, cd on directories.

Summary

  • Linux has one tree rooted at /. Configuration lives in /etc, logs in /var/log, service data in /var/lib, your files in /home/<you>, and your own installs in /usr/local or /opt.
  • Absolute paths start with / and work from anywhere. Relative paths are resolved from the current directory, with . for here and .. for the parent. Prefer absolute paths in scripts and cron jobs.
  • cd alone goes home, cd - toggles to the previous directory, and ~ expands only when unquoted.
  • Programs in the current directory need ./name, because . isn't in PATH.
  • Through symlinks, pwd and cd .. are logical while pwd -P, cd -P, ls .. and most other commands are physical. readlink and realpath show where a link points.
  • ls -l columns: type and permissions, link count, owner, group, size, modification time, name. A directory's size is not the size of its contents.
  • ls -lhtr, ls -lS, ls -ld dir and ls -A cover most daily needs. Use file to find out what a file really contains.
  • pushd, popd and dirs -v keep a stack of directories when you're working in several places.

Exercises

Exercise 1: Climb out of a deep directoryeasy

Start in ~/projects/webshop/src/api. Using a single cd with a relative path, move to ~/projects/webshop/logs, and confirm where you are.

Show Solution
cd ~/projects/webshop/src/api
cd ../../logs
pwd
/home/sid/projects/webshop/logs

.. takes you from api to src, the second .. from src to webshop, and logs goes down into the sibling directory. cd ~/projects/webshop/logs would work too, but the point is to think in relative steps.

Exercise 2: Read a log directory the useful wayeasy

From ~/projects/webshop, list the logs directory so that sizes are human-readable and the most recently modified file is at the bottom. Then print only the name of the most recently modified log.

Show Solution
ls -lhtr logs
ls -t logs | head -n 1
total 628K
-rw-r--r-- 1 sid sid  34K Nov  7 23:59 access.log.2.gz
-rw-r--r-- 1 sid sid 365K Nov  8 23:59 access.log.1
-rw-r--r-- 1 sid sid 5.4K Nov  9 10:16 error.log
-rw-r--r-- 1 sid sid 216K Nov  9 10:17 access.log
access.log

-t sorts newest first and -r flips that so the newest is last. In the second command, ls writes one name per line because its output goes into a pipe rather than a terminal, so head -n 1 gets the newest name. Fine for a quick look. Remember not to build scripts on parsing ls.

Exercise 3: Find the hidden filemedium

ls config in the web shop project shows two files. Show that the directory actually contains three, and then display the permissions of the config directory itself rather than its contents.

Show Solution
ls -A config
ls -ld config
.env  app.conf  nginx.conf
drwxr-xr-x 2 sid sid 4096 Nov  4 14:20 config

.env starts with a dot, so plain ls hides it. -A shows hidden entries without . and ... -d makes ls report on the directory entry itself. Hidden .env files often hold credentials, which is why .gitignore in this project lists it.

Exercise 4: Which release is live?medium

In ~/srv/webshop, current is a symlink to the live release. Find out which release directory it points to in two different ways: once without entering it, and once by entering it.

Show Solution
cd ~/srv/webshop
readlink current
cd ~/srv/webshop/current && pwd -P
releases/v1.4.2
/home/sid/srv/webshop/releases/v1.4.2

readlink prints the target exactly as stored in the link (here a relative path). pwd -P resolves the physical path of the directory you're in. Plain pwd would have printed /home/sid/srv/webshop/current, which doesn't answer the question. realpath current gives the same answer as pwd -P without changing directory.

Exercise 5: A round trip with the directory stackhard

Starting in ~/projects/webshop, use pushd to visit /var/log and then ~/srv/webshop/shared/logs. Display the stack with numbers, then use popd to return to the project directory, step by step, and prove you're back.

Show Solution
cd ~/projects/webshop
pushd /var/log
pushd ~/srv/webshop/shared/logs
dirs -v
popd
popd
pwd
/var/log ~/projects/webshop
~/srv/webshop/shared/logs /var/log ~/projects/webshop
 0  ~/srv/webshop/shared/logs
 1  /var/log
 2  ~/projects/webshop
/var/log ~/projects/webshop
~/projects/webshop
/home/sid/projects/webshop

Each pushd adds a directory on top of the stack and moves you there. Each popd drops the top entry and moves you to the new top. After two pops only the starting directory is left, and a third popd would fail with directory stack empty.


Tested with: GNU Bash 5.2.21 and GNU coreutils 9.4 on Ubuntu 24.04 (WSL2), November 2026. All commands were run as shown in an interactive terminal session, so ls output is in the same column layout you'll see. The project files and timestamps were set up beforehand in a scratch home directory, and the system directory listings come from the WSL machine itself. That explains WSL-specific entries such as /init, /mnt/wslg and the empty /boot.

Was this page helpful?
The Dark Debugger

Written by The Dark Debugger

Engineer documenting the things that took me too long to learn, so they take you less time. Every Bash tutorial here is tested end to end before publishing.