Navigating: pwd, ls & cd

Linux · Lesson 5 of 32 7 min read Updated

Three commands do most of the work of moving around Linux: pwd tells you where you are, ls shows what is here, and cd takes you somewhere else. You will type them hundreds of times a day, so it is worth learning their most useful options properly.

All examples use Priya's home folder, /home/priya, which contains a few folders and a CSV file of shop orders.

pwd: where am I?

The shell always has a current working directory, the folder that relative paths start from. pwd (print working directory) prints it as an absolute path:

Terminal
pwd
Output
/home/priya

When you open a new terminal you start in your home folder. The prompt also shows the current folder (~ for home), but pwd gives the full path, which is useful in scripts and when the prompt is shortened.

ls: what is here?

ls lists the contents of a directory: the current one if you give no argument, or any directory you name.

Terminal
ls
ls -F
ls projects/shop-site
Output
Documents  Downloads  notes  orders.csv  projects
Documents/  Downloads/  notes/  orders.csv  projects/
images  index.html  style.css

The -F option adds a / after directories (and a * after programs), which helps when your terminal has no colours. With colours on, directories usually appear in blue.

Reading the long listing

ls -l shows one line per entry with details. This is the view you will use most:

Terminal
ls -l
Output
total 20
drwxrwxr-x 2 priya priya 4096 Oct 29 18:40 Documents
drwxrwxr-x 2 priya priya 4096 Oct 27 09:12 Downloads
drwxrwxr-x 2 priya priya 4096 Oct 30 08:05 notes
-rw-rw-r-- 1 priya priya  281 Oct 30 08:15 orders.csv
drwxrwxr-x 3 priya priya 4096 Oct 28 21:30 projects

Take the orders.csv line apart from left to right:

PartMeaning
-Type: - is a regular file, d a directory, l a symbolic link
rw-rw-r--Permissions for owner, group and others (see Permissions & Ownership)
1Number of hard links (for directories it grows with the number of subdirectories)
priya priyaThe owner and the group
281Size in bytes
Oct 30 08:15Last modified time (files older than six months show the year instead)
orders.csvThe name

The total line at the top is the disk space used, counted in 1 KB blocks. A directory itself always shows 4096 bytes on most filesystems; that is the size of its list of names, not of the files inside it.

Useful ls options

Options combine freely. These are worth remembering:

OptionWhat it does
-aShow hidden entries (names starting with .)
-hHuman-readable sizes (87K, 2.4M) with -l
-tSort by modification time, newest first
-SSort by size, largest first
-rReverse the sort order
-RList subdirectories recursively
-dShow the directory itself, not its contents
Terminal
ls -lh Documents
ls -lt
ls -ld Documents
Output
total 104K
-rw-rw-r-- 1 priya priya 16K Oct 29 18:40 marks-2026.xlsx
-rw-rw-r-- 1 priya priya 87K Oct 29 18:38 resume.pdf
total 20
-rw-rw-r-- 1 priya priya  281 Oct 30 08:15 orders.csv
drwxrwxr-x 2 priya priya 4096 Oct 30 08:05 notes
drwxrwxr-x 2 priya priya 4096 Oct 29 18:40 Documents
drwxrwxr-x 3 priya priya 4096 Oct 28 21:30 projects
drwxrwxr-x 2 priya priya 4096 Oct 27 09:12 Downloads
drwxrwxr-x 2 priya priya 4096 Oct 29 18:40 Documents

The first command gives friendly sizes. The second puts the most recently changed item first, which is the quickest way to find "the file I just downloaded". The third shows the details of Documents itself rather than listing what is inside. A very common combination is ls -lah: everything, with details, in readable sizes.

cd: go somewhere else

cd (change directory) moves you. Give it an absolute or relative path:

Terminal
cd projects/shop-site
pwd
cd ..
pwd
cd /var/log
pwd
cd
pwd
Output
/home/priya/projects/shop-site
/home/priya/projects
/var/log
/home/priya

cd .. goes up one level. cd on its own (or cd ~) always takes you home, from anywhere. Two more shortcuts are handy: cd - jumps back to the previous directory you were in and prints it, and cd / goes to the very top of the tree.

Press Tab while typing a path and Bash completes it for you. Typing cd pro, Tab, sh, Tab becomes cd projects/shop-site/. If nothing happens, either there is no match or there are several; press Tab twice to see them.

Names with spaces

Linux allows spaces in names, but the shell uses spaces to separate arguments. A folder called College Notes must be quoted or have its space escaped with a backslash:

Terminal
cd "College Notes"
cd College\ Notes

Both commands do the same thing. Without the quotes, cd College Notes would see two arguments and fail. This is one reason many Linux users name files with dashes instead, such as college-notes. Tab completion adds the backslashes for you automatically.

Advanced: a stack of directories

When you keep switching between two or three deep folders, Bash's pushd and popd act like a stack of bookmarks. pushd /etc/nginx goes there and remembers where you were; popd takes you back. dirs -v lists the stack with numbers. Most people use cd - for two places and pushd when there are more.

Common mistakes

  • cd into a file. cd orders.csv fails with "Not a directory". Use cd only on directories; open files with cat or less.
  • Wrong capital letters. cd documents fails with "No such file or directory" because the folder is Documents. Tab completion avoids this.
  • Spaces without quotes. cd My Files tries to use two arguments. Quote names with spaces.
  • Getting lost. When in doubt, run pwd, or type cd alone to go home and start again.

Key points

  • pwd prints the current working directory as an absolute path.
  • ls -l shows type, permissions, links, owner, group, size, date and name; add -a for hidden files and -h for readable sizes.
  • ls -lt sorts newest first and ls -lS sorts largest first.
  • cd dir moves into a directory, cd .. goes up, cd alone goes home and cd - goes back.
  • Quote or escape names that contain spaces, and use Tab completion everywhere.

Exercise

Starting in your home folder, explore (you do not need to create anything). Go into /etc, list only the entries whose details show they are directories, go back to where you were with one command, and finally list your home folder so that the largest file appears first, with human-readable sizes.

Show solution
Terminal
cd /etc
ls -l | grep '^d'
cd -
ls -lhS
Output
drwxr-xr-x 3 root root 4096 Apr 23 09:15 apparmor
drwxr-xr-x 2 root root 4096 Oct 12 06:31 apt
...
/home/priya
total 20K
drwxrwxr-x 2 priya priya 4.0K Oct 29 18:40 Documents
drwxrwxr-x 2 priya priya 4.0K Oct 27 09:12 Downloads
drwxrwxr-x 2 priya priya 4.0K Oct 30 08:05 notes
drwxrwxr-x 3 priya priya 4.0K Oct 28 21:30 projects
-rw-rw-r-- 1 priya priya  281 Oct 30 08:15 orders.csv

grep '^d' keeps only lines that start with d, meaning directories (you will learn grep and the | pipe soon). cd - returns to the previous folder and prints it. With -S, the 4.0K directories sort above the 281-byte file.

Quick Quiz

1. Which command prints the full path of the folder you are in?

2. In drwxrwxr-x 3 priya priya 4096 Oct 28 21:30 projects, what does the first d mean?

3. Which command takes you back to the directory you were in before?

4. How do you list files with the newest first?

Learn Linux with a mentor

Free lessons get you started. Our Ethical Hacking & Cyber Security training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call