ls – list directory contents

ls command
ls command

ls lists directory contents. It’s the most commonly used command for seeing what files and directories exist in a location.

Synopsis

ls [OPTIONS] [FILE...]

Common Options

OptionDescription
-lLong format (permissions, owner, size, date)
-aShow all files including hidden (dotfiles)
-hHuman-readable sizes (KB, MB, GB)
-RRecursive listing
-tSort by modification time (newest first)
-SSort by file size (largest first)
-rReverse sort order
-dList directories themselves, not contents
-1One file per line
-FAppend indicator (/ for dirs, * for executables)

Examples

Basic listing

$ ls
ansible  bash  projects  scripts  documents

Long format with human-readable sizes

$ ls -lh
total 24K
drwxr-xr-x  5 greys staff 4.0K Jan 15 10:30 ansible
drwxr-xr-x  3 greys staff 4.0K Jan 14 09:15 bash
drwxr-xr-x 12 greys staff 4.0K Jan 15 14:22 projects

Show hidden files

$ ls -la
total 48K
drwxr-xr-x 10 greys staff 4.0K Jan 15 14:30 .
drwxr-xr-x  3 root  root  4.0K Jan 10 08:00 ..
-rw-r--r--  1 greys staff  220 Jan 10 08:00 .bashrc
drwxr-xr-x  5 greys staff 4.0K Jan 15 10:30 ansible

Sort by time (newest first)

$ ls -lt
total 24K
drwxr-xr-x 12 greys staff 4.0K Jan 15 14:22 projects
drwxr-xr-x  5 greys staff 4.0K Jan 15 10:30 ansible
drwxr-xr-x  3 greys staff 4.0K Jan 14 09:15 bash

Sort by size (largest first)

$ ls -lhS
total 1.2G
-rw-r--r-- 1 greys staff 800M Jan 15 10:00 backup.tar.gz
-rw-r--r-- 1 greys staff 350M Jan 14 15:30 database.sql
-rw-r--r-- 1 greys staff  50M Jan 13 09:00 logs.txt

List specific file types

$ ls *.pdf
document.pdf  report.pdf  manual.pdf

List directories only

$ ls -d */
ansible/  bash/  projects/  scripts/

One file per line

$ ls -1
ansible
bash
projects
scripts

Use ls -l to see where symbolic links point:

List symlinks with ls

$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 9 Jan 10 08:00 /usr/bin/python -> python3.9

Show SELinux Contexts

On SELinux-enabled systems, use -Z to show security contexts:

List SELinux contexts with ls

$ ls -lZ /var/www/html
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html

Understanding ls -l Output

-rw-r--r-- 1 greys staff 4096 Jan 15 10:30 file.txt
│└──┬───┘ │ └─┬─┘ └─┬─┘ └─┬┘ └────┬─────┘ └──┬───┘
│   │     │   │     │     │       │           └─ filename
│   │     │   │     │     │       └─ modification date
│   │     │   │     │     └─ size in bytes
│   │     │   │     └─ group
│   │     │   └─ owner
│   │     └─ link count
│   └─ permissions (rwx for user, group, others)
└─ file type (- file, d directory, l symlink)

Tips

  • Combine options: ls -lah for full details with hidden files
  • Colorized output: Most modern systems enable --color=auto by default
  • Add to .bashrc: alias ll='ls -lah' for quick access
  • macOS: Use -G for color (instead of --color)
  • Check if file exists: ls filename 2>/dev/null && echo "exists"

See Also

  • cd — Change directory
  • pwd — Print working directory
  • find — Search for files
  • chmod — Change file permissions

Tutorials