Skip to content

Listing and moving around

Module
Navigating the Filesystem
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21; Ubuntu 24.04.4 LTS course VM (VirtualBox 7.2.18)
On this page

You are reading the Bash documentation in /usr/share/doc/bash, which you met in lesson B04.3, and you keep your notes in ~/lab/Documents. You check one, then the other, then the first again:

Screen
learner@rootvolt:~/lab/Documents$ cd /usr/share/doc/bash
learner@rootvolt:/usr/share/doc/bash$ ls
changelog.Debian.gz INTRO.gz README.abs-guide README.md.bash_completion.gz
COMPAT.gz NEWS.gz README.commands.gz
copyright POSIX.gz README.Debian.gz
inputrc.arrows RBASH README.gz
learner@rootvolt:/usr/share/doc/bash$ cd ~/lab/Documents
learner@rootvolt:~/lab/Documents$ cat notes.txt
Buy printer paper
Renew lab VM snapshot
learner@rootvolt:~/lab/Documents$ cd /usr/share/doc/bash

Lesson B05.2 taught you to reach any directory with one cd. But each time you switch, you type the whole path again, and one wrong letter means a No such file or directory message instead of a move.

By the end of this lesson, you can switch between two directories without typing either path, choose the ls view that answers your question, and find the hidden files in any directory, your home directory included.

Open a terminal and run cd ~/lab.

Move into Documents, then to the Bash documentation, as in the opening:

Terminal window
cd Documents
cd /usr/share/doc/bash

The prompt ends in /usr/share/doc/bash$. Now, instead of typing the way back, give cd a single dash:

Terminal window
cd -
Output
/home/learner/lab/Documents

The prompt ends in ~/lab/Documents$ again: you are back. Notice also that this cd printed something, which no cd in lessons B05.1 and B05.2 did: the absolute path of the directory it moved to.

Bash remembers the directory you were in before your last change of directory: the previous directory. cd - moves you there and prints where you landed.

You are in Documents again. Guess first: what happens if you run cd - a second time?

Run it, then compare with your guess
Screen
learner@rootvolt:~/lab/Documents$ cd -
/usr/share/doc/bash
learner@rootvolt:/usr/share/doc/bash$ cd -
/home/learner/lab/Documents
learner@rootvolt:~/lab/Documents$ pwd
/home/learner/lab/Documents

It does not go further back. When cd - moved you to Documents, the directory you left, /usr/share/doc/bash, became the previous directory. So each cd - switches between the same two directories, until you cd somewhere else.

This solves the opening: after one long cd, cd - switches in either direction.

Bash keeps the previous directory in a shell variable called OLDPWD: a name that holds a value inside your shell. Module B08 explains variables. help cd from lesson B04.3 says it in one line:

Terminal window
help cd
Output (shortened)
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable. If DIR is "-", it is converted to $OLDPWD.
…

DIR is the directory you give cd, so cd - means: change to the directory named in OLDPWD.

The replay below moves between Documents and projects. Watch the cwd marker in the Files panel jump back and forth with each cd -.

Switching between two directories with cd -
learner@rootvolt:~/lab$ cd Documentslearner@rootvolt:~/lab/Documents$ lsnotes.txt  report.txtlearner@rootvolt:~/lab/Documents$ cd ../projectslearner@rootvolt:~/lab/projects$ cd -/home/learner/lab/Documentslearner@rootvolt:~/lab/Documents$ cd -/home/learner/lab/projectslearner@rootvolt:~/lab/projects$ 

Files ~/lab

  • ~
    • lab/
      • Documents/
        • notes.txt-rw-r--r--
        • report.txt-rw-r--r--
      • .draft.txthidden-rw-r--r--
      • projects/cwd
      • todo.txt-rw-r--r--

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS (lab system, Ubuntu Desktop package set), GNU Bash 5.2.21. cd - goes back to the previous directory and prints its path; a second cd - switches back again.
Steps (14)
  1. Type cd Documents — Move into Documents
  2. Press Enter
  3. Type ls — What is in here?
  4. Press Enter
  5. Output: notes.txt report.txt
  6. Type cd ../projects — Move to projects, next to Documents
  7. Press Enter
  8. Type cd - — Back to the previous directory; cd - prints its path
  9. Press Enter
  10. Output: /home/learner/lab/Documents
  11. Type cd - — Again: back to projects
  12. Press Enter
  13. Output: /home/learner/lab/projects
  14. cd - switches between the last two directories, and prints the one it moves to.

In words: from ~/lab, cd Documents moves the marker to Documents, and ls lists notes.txt and report.txt. cd ../projects moves the marker to projects. The first cd - prints /home/learner/lab/Documents and moves the marker back to Documents. The second prints /home/learner/lab/projects and moves it to projects.

Choose the view that answers your question

Section titled “Choose the view that answers your question”

You know three views of ls from module B04: plain ls, the long format ls -l, and ls -a or ls -A for hidden names. A few more options from man ls answer the questions you ask most often. Run cd ~/lab first.

To see what is in two directories, give ls both paths:

Terminal window
ls Documents projects
Output
Documents:
notes.txt report.txt
projects:

ls lists each directory in its own block, with the path and a colon as its heading, as ls -R did in lesson B04.3. Nothing follows projects:, because projects is empty. You did not need a cd.

Which of these names are directories? In the output of plain ls, Documents and todo.txt look the same. The option -F adds a mark to the end of a name that shows what kind of entry it is:

Terminal window
ls -F
Output
Documents/ projects/ todo.txt

A / at the end of a name means the entry is a directory. The mark is not part of the name: the directory is still called Documents. The long form of -F is --classify. Other kinds of entries get other marks, such as * after a program you can run and @ after a link (module B06); in ~/lab, the only mark is /.

On the course VM, ls also colors directories, because ls is an alias for ls --color=auto (lesson B04.4). The / mark does not depend on color, and it stays when you copy the output into your notes.

The option -1 prints each name on its own line. That is the digit one, not a lowercase L:

Terminal window
ls -1
Output
Documents
projects
todo.txt

A list like this is easier to read when a directory holds many names.

To describe one file, give ls -l the path of that file:

Terminal window
ls -l Documents/notes.txt
Output
-rw-r--r-- 1 learner learner 40 Oct 1 09:00 Documents/notes.txt

One line, ending in the path you gave. Now try the same with a directory. Guess first: does ls -l projects print one line about projects, like the line above?

Run it, then compare with your guess
Output
total 0

No line about projects at all. When the argument is a directory, ls lists what is inside it, and projects is empty. Only the total line is left, and with nothing listed, it is 0.

To describe the directory itself, add -d, as you did with ls -ld / in lesson B05.1:

Terminal window
ls -ld projects
Output
drwxr-xr-x 2 learner learner 4096 Oct 1 09:00 projects

The d at the start of the line means directory. With Documents, the difference is easy to see:

You run in ~/lab Output Describes
ls -l Documents total 8, then one line for notes.txt and one for report.txt What is inside Documents
ls -ld Documents drwxr-xr-x 2 learner learner 4096 Oct 1 09:00 Documents Documents itself

You know the hidden file in ~/lab from lesson B04.1. Combine -A with -F, as short options can share one dash:

Terminal window
ls -AF
Output
Documents/ .draft.txt projects/ todo.txt

.draft.txt has no /, so it is a file. Read it:

Terminal window
cat .draft.txt
Output
Lab checklist (draft)

Now compare it with a file that is not hidden:

Terminal window
ls -l .draft.txt todo.txt
Output
-rw-r--r-- 1 learner learner 22 Oct 1 09:00 .draft.txt
-rw-r--r-- 1 learner learner 55 Oct 1 09:00 todo.txt

The first column, the permissions, is the same for both files. A name that starts with a dot is hidden only from listings: ls leaves it out unless you ask with -a or -A. The file is not locked, and cat opened it like any other. Who may read or change a file is set by its permissions, which module B11 explains.

Your home directory has many more hidden names than ~/lab. On the course VM, ls -a ~ prints:

Output (course VM)
. Public
.. snap
.bash_history .ssh
.bash_logout .sudo_as_admin_successful
.bashrc Templates
.cache .vboxclient-clipboard-tty2-control.pid
.config .vboxclient-clipboard-tty2-service.pid
Desktop .vboxclient-draganddrop-tty2-control.pid
Documents .vboxclient-hostversion-tty2-control.pid
Downloads .vboxclient-hostversion-tty2-service.pid
.local .vboxclient-seamless-tty2-control.pid
Music .vboxclient-vmsvga-session-tty2-control.pid
Pictures .vboxclient-vmsvga-session-tty2-service.pid
.profile Videos

The names without a dot are the directories that Files shows in your home directory (lesson B03.1). Most of the others are settings and data that programs keep for your account, hidden so that a plain ls stays short:

  • .bashrc holds commands that Bash runs when a terminal starts; on Ubuntu, it defines the ls alias from lesson B04.4. .profile is read when you log in.
  • .bash_history is the history file from lesson B04.2.
  • .cache, .config, and .local are directories where many programs keep temporary data, settings, and other data.
  • The .vboxclient-…pid files come from VirtualBox’s Guest Additions, the helper programs that connect the VM to your host computer, for example for the shared clipboard.

Your list can differ: programs add hidden names the first time you use them.

Read each message in three parts, as in lesson B04.4: who is speaking, which word it names, and what went wrong.

cd - in a new terminal.

Screen
learner@rootvolt:~$ cd -
bash: cd: OLDPWD not set

Bash speaks for cd. The word is OLDPWD, which you did not type: the variable that holds the previous directory. not set means it has no value yet: this shell has not changed directory since it started, so there is no previous directory. After any other cd, cd - works:

Screen
learner@rootvolt:~$ cd lab
learner@rootvolt:~/lab$ cd -
/home/learner
learner@rootvolt:~$ cd -
/home/learner/lab

A hidden name without its dot.

Screen
learner@rootvolt:~/lab$ cat draft.txt
cat: draft.txt: No such file or directory

cat is speaking, so the command name is fine. The word is draft.txt, and no file has that name: the file is .draft.txt, and the dot is part of the name. Tab (lesson B04.2) shows this too. After cat dr, Tab adds nothing, because no name starts with dr. After cat .d, Tab completes cat .draft.txt. Tab completes a hidden name only after you type its dot.

A dot at the start of a name keeps a file out of a plain ls, and attackers who get into a Linux system use this to hide their files. MITRE ATT&CK, a public catalog of attacker techniques, lists it as Hide Artifacts: Hidden Files and Directories (T1564.001). It hides nothing from ls -a. When you check what a directory contains, use -a or -A: a plain ls is not a complete list.

Goal: Look into a directory where plain ls shows nothing, and return to your notes without typing their path.

  1. Open a new terminal. Move to ~/lab/Documents with one cd.
  2. Move to /etc/skel, and list it with plain ls.
  3. List /etc/skel again so that you see its hidden entries, but not . and .., with a mark on each directory.
  4. Show the details of .bashrc only.
  5. Go back to ~/lab/Documents without typing its path, and check where you are.

Expected: Step 2 prints nothing. Step 3 shows three names, .bash_logout, .bashrc, and .profile, none of them with a /. Step 4 prints one line that ends in .bashrc. In step 5, pwd prints /home/learner/lab/Documents.

Hint

Short options can share one dash, as in ls -la. The directory you were in before /etc/skel is the previous directory.

Solution
Screen
learner@rootvolt:~$ cd ~/lab/Documents
learner@rootvolt:~/lab/Documents$ cd /etc/skel
learner@rootvolt:/etc/skel$ ls
learner@rootvolt:/etc/skel$ ls -AF
.bash_logout .bashrc .profile
learner@rootvolt:/etc/skel$ ls -l .bashrc
-rw-r--r-- 1 root root 3771 Mar 31 2024 .bashrc
learner@rootvolt:/etc/skel$ cd -
/home/learner/lab/Documents
learner@rootvolt:~/lab/Documents$ pwd
/home/learner/lab/Documents

Plain ls prints nothing, because every name in /etc/skel is hidden. -A shows them without . and .., and -F adds no /, so all three are files. The size and date of .bashrc can differ on your system.

When an account is created, the system copies the files in /etc/skel into its home directory. Your own .bashrc, .profile, and .bash_logout came from there.

  • cd - moves you to the previous directory and prints its absolute path; run it again to switch back. In a shell that has not changed directory yet, it reports OLDPWD not set.
  • ls with several paths lists each one in its own block. -F marks directories with /, and -1 prints one name per line.
  • ls -l with a file path describes that one file. With a directory, it lists what is inside; add -d to describe the directory itself.
  • A name that starts with a dot is hidden from ls, not protected. ls -a or ls -A shows it, and your home directory keeps many settings this way.

Next, lesson B05.4, Important system directories, tours the main directories in /, such as /etc and /usr, and what each one is for.