Skip to content

Linux Foundations / Find your way around Linux ยท Step 15 of 63

Lesson: ls (List Directories)Switch
โ† Linux Foundations

Shell and files

ls (List Directories)

List files with ls, show hidden entries and read the long listing format.

ls lists what is in a directory. It is one of the commands you will use most, because it shows you what you are working with.

Basic listing

$ ls
Documents  Downloads  notes.txt

With no argument, ls shows the current directory. Give it a path to look elsewhere without moving there:

$ ls /etc/apt

Colours depend on your settings. Many distributions set up an alias that adds them.

Hidden files

Names that start with a dot are hidden from a plain listing. Use -a to show everything:

$ ls -a
.  ..  .bashrc  Documents  Downloads  notes.txt

. is the directory itself and .. is its parent. -A shows hidden files but leaves those two out. Hiding is a way to reduce clutter, not a security feature.

Long format

-l shows one entry per line with details:

$ ls -l
total 8
drwxr-xr-x 2 pete pete 4096 Nov 20 09:15 Documents
-rw-r--r-- 1 pete pete  120 Nov 21 17:02 notes.txt

From left to right: type and permissions, link count, owner, group, size in bytes, last modification time, name. The first character is the type: d for a directory, - for a regular file, l for a symbolic link. The total line counts disk blocks used by the listed files.

Useful options

Options can be combined, so ls -la means -l and -a together.

  • -h with -l prints sizes such as 4.0K or 2.5M.
  • -t sorts by modification time, newest first.
  • -r reverses the order.
  • -R lists subdirectories too.
  • -d shows a directory itself instead of its contents.
$ ls -ld Documents
drwxr-xr-x 2 pete pete 4096 Nov 20 09:15 Documents

GNU ls on Linux and the BSD ls on macOS share these short options, though colour settings and date formats differ.

The shell navigation lab is a good place to explore listings.

Try it

  1. Run ls and then ls -a in your home directory.
  2. Run ls -lh /var/log.
  3. Find the newest file with ls -lt.
  4. Compare ls /tmp with ls -ld /tmp.

Check your understanding

ls (List Directories)

All 5 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.