tech katta

The file system: pwd, ls, cd and where things live

Lesson 02 of 23 · 5 min readFree

In this lesson

Move around a Linux server with pwd, ls and cd, tell absolute paths from relative ones, and know what /etc, /var, /opt and /home are for.

On this page
  1. Why this matters
  2. One tree, one root
  3. Absolute and relative paths
  4. Spaces and special characters
  5. What the standard folders are for
  6. A layout for deployed apps
  7. Try it

A Linux server has no drive letters and no desktop. Everything, including disks, devices and running processes, hangs off one tree that starts at /. This lesson shows how to move through that tree and where to look when you need a config file, a log or a deployed app.

Why this matters#

When something breaks on a server, the first question is "where is it?": the config, the log, the code. Engineers who know the standard layout find these in seconds, and they also put their own files in the right place so the next person can find them too. Paths are also the most common source of small deploy bugs, so it pays to be exact.

One tree, one root#

The top of the tree is called the root directory and is written /. Do not confuse it with the user named root, whose home folder is /root. A path is a list of folder names separated by slashes, ending in a file or folder.

pwd prints the folder you are in right now. ls lists what is inside it. cd moves you somewhere else.

Shell
pwd
ls
ls -l
Text
/srv/app
current
releases
shared
total 8
lrwxrwxrwx 1 root root   17 Dec  2 20:42 current -> releases/20261202
drwxr-xr-x 4 root root 4096 Dec  2 20:42 releases
drwxr-xr-x 3 root root 4096 Dec  2 20:42 shared

This is a small deploy folder like the ones you will meet later in the module. The long listing (-l) shows the type and permissions first (d means a directory, l a link), then the owner and group, size, modification time and name. The line for current ends with an arrow: it is a symbolic link pointing to releases/20261202. Lesson 3 covers links; for now just notice how a deploy tool can switch versions by changing one link.

Useful ls options:

  • -a shows hidden files, which are the ones whose names start with a dot, such as .env and .bashrc.
  • -h prints sizes like 4.0K instead of raw bytes (use it with -l).
  • -t sorts by modification time, newest first, and -S sorts by size, biggest first.
  • -d lists a folder itself instead of its contents. ls -ld /root shows the folder's permissions.
Shell
ls -la shared
Text
total 12
drwxr-xr-x 3 root root 4096 Dec  2 20:42 .
drwxr-xr-x 4 root root 4096 Dec  2 20:42 ..
-rw-r--r-- 1 root root    0 Dec  2 20:42 .env
drwxr-xr-x 2 root root 4096 Dec  2 20:42 logs

Without -a, you would see only logs and might never notice the .env file. Hidden files are not secret; they are just left out of the default view. The two special entries . and .. always exist: . is the folder itself and .. is its parent.

Absolute and relative paths#

An absolute path starts with / and means the same thing wherever you are: /etc/hostname. A relative path does not start with / and is read from your current folder: releases/20261202.

Shell
cd /srv/app/releases/20261202
pwd
cd ../..
pwd
Text
/srv/app/releases/20261202
/srv/app

.. means "one folder up", so ../.. goes up two. Some other shortcuts worth knowing:

  • cd with no argument, or cd ~, goes to your home folder. Bash expands ~ to the value of $HOME.
  • cd - jumps back to the previous folder and prints its name. It is the "back button".
  • . is the current folder, as in ./myscript from lesson 1.

Use absolute paths in scripts, cron jobs and service files. A script that says cd releases works only when it is started from the right folder, and a service started by the system does not start from the folder you expect. Relative paths are fine for quick work at the prompt.

When a folder is reached through a symbolic link, pwd shows the path you used. pwd -P shows the real path with links resolved.

Shell
cd /srv/app/current
pwd
pwd -P
Text
/srv/app/current
/srv/app/releases/20261202

If a program tells you it is running from a strange releases/... folder when you thought you were in current, this is why.

Spaces and special characters#

The shell splits words on spaces, so a name with a space needs quotes: cd "My Reports". Better still, avoid spaces in names you create. Use lowercase letters, digits, hyphens and underscores. Linux is case sensitive, so App.log and app.log are two different files.

What the standard folders are for#

Linux distributions follow a common layout. You do not need to memorise all of it, but these four come up daily.

  • /etc holds system-wide configuration, mostly plain text files. Web server settings, SSH settings and the hostname live here. The name is historical; read it as "settings".
  • /var holds data that changes while the system runs. The most important part is /var/log, where services write logs. Package caches and mail queues are here too.
  • /opt is for add-on software that comes as one self-contained bundle, such as a vendor's tool. Many teams also put an app here, for example /opt/myapp.
  • /home holds one folder per user, such as /home/deploy. This is where a person's files and dotfiles go. The exception is root, who uses /root.

A few more are worth recognising:

  • /usr holds installed programs and libraries. /usr/bin is where most commands live.
  • /tmp is scratch space that anyone can write to. Files here may be deleted on reboot, so never keep anything important there.
  • /srv is meant for data a server hands out, such as website files. Some teams deploy apps under /srv/app, which is what the examples here use.
  • /proc and /sys are not real disk folders. They are windows into the kernel, which you will use when looking at processes.

You can see the real layout of the machine you are on:

Shell
ls -d /etc /var /opt /home /tmp /usr /root /srv
ls /var/log | head -6
Text
/etc
/home
/opt
/root
/srv
/tmp
/usr
/var
README
alternatives.log
apt
btmp
dpkg.log
faillog

Exactly what you see in /var/log depends on the machine and which services are installed.

A layout for deployed apps#

Putting an app under /srv/app or /opt/app with three folders keeps things tidy. The release folders hold code, shared holds things that must survive a deploy (logs, uploaded files, the .env), and current points at the live release.

Text
/srv/app/
  current -> releases/20261202
  releases/
    20261201/
    20261202/
  shared/
    .env
    logs/

Rolling back is then a matter of pointing current at the previous release. The capstone lesson builds on this layout.

Try it#

  1. Run pwd, then cd /etc, then pwd, then cd -. Where did cd - take you?
  2. List /var/log with ls -lt and find which file was changed most recently. Then try ls -lS and compare.
  3. Create the deploy layout with mkdir -p ~/demo/srv/app/releases/20261202 ~/demo/srv/app/shared/logs and then use ls -R ~/demo to see it.
  4. From inside ~/demo/srv/app/releases/20261202, reach shared/logs using a relative path made only of .. and folder names. Then reach it again with an absolute path.
  5. Create a hidden file with touch ~/demo/.env. Show that ls ~/demo does not list it and ls -a ~/demo does.