Listing and moving around
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:
learner@rootvolt:~/lab/Documents$ cd /usr/share/doc/bashlearner@rootvolt:/usr/share/doc/bash$ lschangelog.Debian.gz INTRO.gz README.abs-guide README.md.bash_completion.gzCOMPAT.gz NEWS.gz README.commands.gzcopyright POSIX.gz README.Debian.gzinputrc.arrows RBASH README.gzlearner@rootvolt:/usr/share/doc/bash$ cd ~/lab/Documentslearner@rootvolt:~/lab/Documents$ cat notes.txtBuy printer paperRenew lab VM snapshotlearner@rootvolt:~/lab/Documents$ cd /usr/share/doc/bashLesson 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.
Before you start
Section titled “Before you start”- Absolute and relative paths (B05.2): absolute paths, relative paths, and
~/…. - The
~/labpractice directory from lesson B04.1 (setup).
Open a terminal and run cd ~/lab.
Go back with cd -
Section titled “Go back with cd -”Move into Documents, then to the Bash documentation, as in the opening:
cd Documentscd /usr/share/doc/bashThe prompt ends in /usr/share/doc/bash$. Now, instead of typing the way back, give cd a single dash:
cd -/home/learner/lab/DocumentsThe 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
learner@rootvolt:~/lab/Documents$ cd -/usr/share/doc/bashlearner@rootvolt:/usr/share/doc/bash$ cd -/home/learner/lab/Documentslearner@rootvolt:~/lab/Documents$ pwd/home/learner/lab/DocumentsIt 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:
help cdcd: 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.
Switch between two lab directories
Section titled “Switch between two lab directories”The replay below moves between Documents and projects. Watch the cwd marker in the Files panel jump back and forth with each 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--
Steps (14)
- Type
cd Documents— Move into Documents - Press Enter
- Type
ls— What is in here? - Press Enter
- Output:
notes.txt report.txt - Type
cd ../projects— Move to projects, next to Documents - Press Enter
- Type
cd -— Back to the previous directory; cd - prints its path - Press Enter
- Output:
/home/learner/lab/Documents - Type
cd -— Again: back to projects - Press Enter
- Output:
/home/learner/lab/projects - 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.
List several directories at once
Section titled “List several directories at once”To see what is in two directories, give ls both paths:
ls Documents projectsDocuments: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.
Mark the directories
Section titled “Mark the directories”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:
ls -FDocuments/ projects/ todo.txtA / 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.
Print one name per line
Section titled “Print one name per line”The option -1 prints each name on its own line. That is the digit one, not a lowercase L:
ls -1Documentsprojectstodo.txtA list like this is easier to read when a directory holds many names.
Ask about one entry
Section titled “Ask about one entry”To describe one file, give ls -l the path of that file:
ls -l Documents/notes.txt-rw-r--r-- 1 learner learner 40 Oct 1 09:00 Documents/notes.txtOne 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
total 0No 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:
ls -ld projectsdrwxr-xr-x 2 learner learner 4096 Oct 1 09:00 projectsThe 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 |
Find the hidden files
Section titled “Find the hidden files”You know the hidden file in ~/lab from lesson B04.1. Combine -A with -F, as short options can share one dash:
ls -AFDocuments/ .draft.txt projects/ todo.txt.draft.txt has no /, so it is a file. Read it:
cat .draft.txtLab checklist (draft)Now compare it with a file that is not hidden:
ls -l .draft.txt todo.txt-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.txtThe 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.
The hidden files in your home directory
Section titled “The hidden files in your home directory”Your home directory has many more hidden names than ~/lab. On the course VM, ls -a ~ prints:
. Public.. snap.bash_history .ssh.bash_logout .sudo_as_admin_successful.bashrc Templates.cache .vboxclient-clipboard-tty2-control.pid.config .vboxclient-clipboard-tty2-service.pidDesktop .vboxclient-draganddrop-tty2-control.pidDocuments .vboxclient-hostversion-tty2-control.pidDownloads .vboxclient-hostversion-tty2-service.pid.local .vboxclient-seamless-tty2-control.pidMusic .vboxclient-vmsvga-session-tty2-control.pidPictures .vboxclient-vmsvga-session-tty2-service.pid.profile VideosThe 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:
.bashrcholds commands that Bash runs when a terminal starts; on Ubuntu, it defines thelsalias from lesson B04.4..profileis read when you log in..bash_historyis the history file from lesson B04.2..cache,.config, and.localare directories where many programs keep temporary data, settings, and other data.- The
.vboxclient-…pidfiles 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.
Mistakes to recognize
Section titled “Mistakes to recognize”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.
learner@rootvolt:~$ cd -bash: cd: OLDPWD not setBash 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:
learner@rootvolt:~$ cd lablearner@rootvolt:~/lab$ cd -/home/learnerlearner@rootvolt:~$ cd -/home/learner/labA hidden name without its dot.
learner@rootvolt:~/lab$ cat draft.txtcat: draft.txt: No such file or directorycat 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.
Security Angle
Section titled “Security Angle”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.
Try it yourself
Section titled “Try it yourself”Goal: Look into a directory where plain ls shows nothing, and return to your notes without typing their path.
- Open a new terminal. Move to
~/lab/Documentswith onecd. - Move to
/etc/skel, and list it with plainls. - List
/etc/skelagain so that you see its hidden entries, but not.and.., with a mark on each directory. - Show the details of
.bashrconly. - Go back to
~/lab/Documentswithout 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
learner@rootvolt:~$ cd ~/lab/Documentslearner@rootvolt:~/lab/Documents$ cd /etc/skellearner@rootvolt:/etc/skel$ lslearner@rootvolt:/etc/skel$ ls -AF.bash_logout .bashrc .profilelearner@rootvolt:/etc/skel$ ls -l .bashrc-rw-r--r-- 1 root root 3771 Mar 31 2024 .bashrclearner@rootvolt:/etc/skel$ cd -/home/learner/lab/Documentslearner@rootvolt:~/lab/Documents$ pwd/home/learner/lab/DocumentsPlain 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 reportsOLDPWD not set.lswith several paths lists each one in its own block.-Fmarks directories with/, and-1prints one name per line.ls -lwith a file path describes that one file. With a directory, it lists what is inside; add-dto describe the directory itself.- A name that starts with a dot is hidden from
ls, not protected.ls -aorls -Ashows 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.
Further reading
Section titled “Further reading”man lson your system, or the Ubuntu 24.04 ls(1) page online: the options-a,-A,-d,-F, and-1.- GNU Coreutils manual: ls invocation, with the sections Which files are listed (
-a,-A,-d) and General output formatting (-F,-1). - GNU Bash Reference Manual: Bourne Shell Builtins, the entry for
cd, including-andOLDPWD. On your system,help cdprints a short version.