Listing Files with ls

ls shows the contents of a directory, with options to reveal details and hidden files.

Syntaxls [options] [path]

ls lists the files and folders in a directory. On its own it lists the current directory; give it a path to list somewhere else.

The most useful options

  • -l — long format: permissions, owner, size and date.
  • -a — show hidden files (names starting with a dot).
  • -h — human-readable sizes (KB, MB, GB) — use with -l.
  • -t — sort by modification time, newest first.
  • -R — list sub-directories recursively.

These are almost always combined, and ls -lah is a favourite of experienced users.

Example

Example · bash
ls -lah
# total 20K
# drwxr-xr-x 3 alice alice 4.0K Jul 15 09:12 .
# drwxr-xr-x 5 root  root  4.0K Jul 10 08:00 ..
# -rw-r--r-- 1 alice alice  220 Jul 15 09:10 .bashrc
# -rw-r--r-- 1 alice alice 1.2K Jul 15 09:12 notes.txt

When to use it

  • A developer runs 'ls -lh /var/log/nginx' to check log file sizes before deciding which to archive.
  • A DevOps engineer uses 'ls -la ~/.ssh' to verify that SSH key files have the correct restrictive permissions.
  • A sysadmin runs 'ls -lt /tmp' to find the most recently modified temporary files during an incident investigation.

More examples

List files in long format

Shows permissions, owner, size (human-readable), and modification date for each file.

Example · bash
ls -lh /var/log
# drwxr-xr-x  2 root   root    4.0K Jul 10 08:00 apt
# -rw-r--r--  1 root   root   128K Jul 16 14:23 syslog

Show hidden files and sort by time

Combines -l (long), -a (all including hidden), and -t (sort by time) to find recently changed dotfiles.

Example · bash
ls -lat ~
# Lists all files including dotfiles, newest first
# drwxr-xr-x  8 alice alice 4.0K Jul 16 14:20 .
# -rw-------  1 alice alice 5.2K Jul 16 14:19 .bash_history

List only directories

Uses the glob pattern '*/' with -d to list only subdirectories without recursing into them.

Example · bash
ls -d /etc/*/
# /etc/apt/   /etc/cron.d/   /etc/nginx/   /etc/ssh/

# Or using -l for details
ls -ld /etc/*/

Discussion

  • Be the first to comment on this lesson.