Click the virtual machine below to start practicing
- Setting up virtual machine…
- Preparing lab files…
- Starting terminal services…
This lab machine has stopped
Your progress through the guide is saved. Start a fresh machine to keep practicing.
Click the virtual machine below to start practicing
Your progress through the guide is saved. Start a fresh machine to keep practicing.
Every time you open a terminal you land in a directory, and the first question is usually "what's in here?". The ls command answers it.
In this lab you'll list the files in a small project folder, uncover hidden files, read the details ls can show, and sort the output. Each step checks your terminal to confirm you ran the command.
Your terminal is on the left. It's already in ~/project, which has a few files for you to explore.
Type ls on its own and press Enter:
ls
You should see something like this:
backup.img notes reports scores.csv todo.txt
ls prints the names of everything in the current directory. Many terminals colour directories (notes, reports) differently from regular files.
In Linux, any file whose name starts with a dot is hidden. Plain ls skips them, so settings files don't clutter the view. Add the -a (all) option to see them:
ls -a
. .. .settings backup.img notes reports scores.csv todo.txt
Two more entries appear besides .settings:
. is the current directory itself... is the parent directory, one level up.Every directory contains these two, which is why cd .. takes you up a level.
Names alone don't tell you much. The -l option switches to the long listing, one entry per line with details:
ls -l
-rw-r--r-- 1 learner learner 250000 Oct 18 10:02 backup.img
drwxr-xr-x 2 learner learner 4096 Oct 18 10:02 notes
...
Reading a line from left to right:
d means directory, - means a regular file; the rest says who may read, write, and execute.learner.250000 bytes is hard to read at a glance. The -h (human-readable) option turns sizes into K, M, and G. It only changes the long listing, so combine it with -l:
ls -lh
-rw-r--r-- 1 learner learner 245K Oct 18 10:02 backup.img
Single-letter options can be squeezed together: ls -lh, ls -hl and ls -l -h all do the same thing.
ls sorts alphabetically by default. To find what changed most recently, sort by modification time with -t:
ls -lt
The newest file is at the top. Add -r to reverse any sort order, so the oldest comes first:
ls -ltr
scores.csv was last changed three days ago, so it sits at the top of the reversed list. Run both commands.
You don't have to move into a directory to see what's inside it. Give ls the directory's name:
ls -l notes
-rw-r--r-- 1 learner learner 17 Oct 18 10:02 monday.txt
-rw-r--r-- 1 learner learner 16 Oct 18 10:02 tuesday.txt
Options come before the name, and you can list several directories at once: ls notes reports.
You can now use ls to see what's in any directory and pick the level of detail you need.
ls lists names; ls -a includes hidden dot-files.ls -l shows type, permissions, owner, size, and date.ls -lh makes sizes readable.ls -lt and ls -ltr sort by time, newest or oldest first.ls -l notes lists another directory without moving into it.