Getting started

Navigate folders in Linux with cd, ls and pwd

Move around the Linux filesystem with confidence using cd, ls and pwd, understand absolute and relative paths, show hidden files and sort listings by size or date.

BeginnerLinux server5 min read

Three commands do almost all of your moving around on a Linux server: pwd tells you where you are, ls shows what is there, and cd takes you somewhere else. This guide covers each one, the path shortcuts that save typing, and the ls options that help you find the biggest or newest files.

Before you start

Know where you are with pwd

pwd stands for print working directory. It shows the full path of the folder you are in:

Bash
pwd
/home/deploy

Your prompt often shows the folder too, but it may shorten it. pwd always prints the complete path.

Absolute and relative paths

A path tells a command where a file or folder is. There are two kinds:

  • Absolute paths start with / and describe the location from the top of the tree. /var/log/nginx means the same thing no matter where you are.
  • Relative paths do not start with /. They are read from your current folder. If you are in /var, then log/nginx points to /var/log/nginx.

Some shortcuts work in any path:

  • ~ is your home folder, for example /home/deploy or /root.
  • . is the current folder.
  • .. is the parent folder, one level up.
  • - (with cd only) is the folder you were in before.

Move around with cd

Bash
cd /var/log        # go to an absolute path
cd nginx           # go into a subfolder of where you are
cd ..              # up one level
cd ../..           # up two levels
cd ~               # back to your home folder
cd                 # also back home
cd -               # jump back to the previous folder

cd - is useful when you switch between two folders, such as a config folder and a log folder.

Tip: Press Tab while typing a path. The shell completes folder names for you and shows the options if more than one matches. It is faster and prevents typos.

If a folder name contains spaces, quote it or escape the space:

Bash
cd "/srv/My Site"
cd /srv/My\ Site

List files with ls

On its own, ls prints the names in the current folder. You can also give it a path:

Bash
ls
ls /etc/nginx

The options you will use most:

  • -l: long format, with permissions, owner, group, size and modification date.
  • -a: show hidden files, whose names start with a dot (such as .bashrc or .ssh).
  • -h: human-readable sizes (4.0K, 12M, 1.1G) when combined with -l.
  • -d: show the folder itself rather than its contents.

Most people combine them:

Bash
ls -lah

A typical line looks like this:

-rw-r--r-- 1 deploy deploy 3.7K Nov  2 10:14 .bashrc

From left to right: the file type and permissions, the link count, the owner, the group, the size, the modification date and the name. A line starting with d is a folder; a line starting with l is a symbolic link.

Sort by size or date

These options help you find what changed recently or what takes up space:

Bash
ls -lt          # newest first
ls -ltr         # oldest first, newest at the bottom
ls -lS          # largest first
ls -lSh         # largest first, readable sizes

ls -ltr is handy in a log folder, because the file that was written most recently appears just above your prompt.

Note: For a folder, ls -l shows the size of the folder entry itself, not of everything inside it. To see how much space a folder really uses, run du -sh /path/to/folder.

See the tree at a glance

To see folders inside folders, ls -R lists everything recursively, but the output gets long fast. The tree program is easier to read. Install it on Ubuntu and Debian with:

Bash
sudo apt install tree

On AlmaLinux and Rocky Linux:

Bash
sudo dnf install tree

Then limit the depth so it stays readable:

Bash
tree -L 2 /etc/nginx

Check it works

Run this sequence and compare each result with what you expect:

Bash
cd /var/log
pwd
ls -ltr | tail -5
cd -
pwd
ls -la ~

You should land in /var/log, see the five most recently changed logs, jump back to where you started, and then see the hidden files in your home folder.

Troubleshooting

  • No such file or directory: check the spelling and case. Documents and documents are different. Use Tab completion to be sure.
  • Permission denied when you cd into a folder: your user lacks access. Root-only folders such as /root need sudo -i, or list them with sudo ls /root.
  • Not a directory: you tried to cd into a file. Use ls -l to check what the item is.
  • Hidden files do not show: add -a. Plain ls never shows names that start with a dot.
  • A path with spaces fails: wrap it in quotes, as shown above.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.