# ls — List Directory Contents

> List directory contents with ls — permissions, owner, size and modification time, plus sorting, filtering and useful flag combinations.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/ls/

<!-- PROSE:intro -->
`ls` lists the contents of directories, which makes it one of the commands you type most often in the terminal. Without options it shows only names, but the flags are what make it genuinely useful: `-l` gives the long format with permissions, owner, size and modification date, `-a` reveals hidden files, `-h` makes sizes readable, and `-t` or `-S` sort by time or size respectively. The options shown here refer to GNU `ls` from coreutils, the default on Linux – BSD/macOS `ls` lacks some flags or assigns them differently. In practice you simply combine the letters, e.g. `ls -lahS`, and pipe onward to `head`, `wc` or `grep`.
<!-- PROSE:intro:end -->

## Basic Usage

`ls` — List files and directories in the current directory.

```bash
ls
```

`ls <dir>` — List contents of a specific directory.

```bash
ls /var/log
```

`ls <file1> <file2>` — List specific files or multiple paths.

```bash
ls index.html style.css
```

`ls -l` — Long format. Show permissions, owner, size, and modification date.

```bash
ls -l
```

`ls -la` — Long format including hidden files (dotfiles).

```bash
ls -la ~/
```

`ls -lh` — Long format with human-readable file sizes (K, M, G).

```bash
ls -lh /var/log/
```

## Showing Hidden Files

`ls -a` — Show all files including hidden (dotfiles), . and ..

```bash
ls -a
```

`ls -A` — Show hidden files but exclude . and .. entries.

```bash
ls -A
```

`ls -d .*` — List only hidden files and directories.

```bash
ls -d .*
```

## Sorting

`ls -t` — Sort by modification time, newest first.

```bash
ls -lt
```

`ls -tr` — Sort by modification time, oldest first (reversed).

```bash
ls -ltr
```

`ls -S` — Sort by file size, largest first.

```bash
ls -lSh
```

`ls -Sr` — Sort by file size, smallest first.

```bash
ls -lSrh
```

`ls -X` — Sort alphabetically by file extension.

```bash
ls -lX
```

`ls -v` — Natural sort of version numbers within filenames.

```bash
ls -v
```

`ls -U` — Do not sort. List entries in directory order.

```bash
ls -U
```

`ls -r` — Reverse the sort order.

```bash
ls -lr
```

## Display Formats

`ls -1` — List one file per line.

```bash
ls -1
```

`ls -m` — List files as comma-separated values.

```bash
ls -m
```

`ls -C` — List in columns (default when output is a terminal).

```bash
ls -C
```

`ls -x` — List entries by lines instead of columns.

```bash
ls -x
```

`ls -Q` — Enclose filenames in double quotes.

```bash
ls -Q
```

`ls --color=auto` — Colorize output. Directories, executables, links get distinct colors.

```bash
ls --color=auto
```

## File Information

`ls -i` — Show inode number of each file.

```bash
ls -li
```

`ls -n` — Long format with numeric UID and GID instead of names.

```bash
ls -ln
```

`ls -o` — Long format without group information.

```bash
ls -o
```

`ls -g` — Long format without owner information.

```bash
ls -g
```

`ls --full-time` — Long format with full date and time including nanoseconds.

```bash
ls --full-time
```

`ls -s` — Show allocated block size for each file.

```bash
ls -ls
```

## Directories & Recursion

`ls -d <dir>` — List directory entry itself, not its contents.

```bash
ls -ld /var
```

`ls -d */` — List only directories in the current directory.

```bash
ls -d */
```

`ls -R` — List contents recursively through all subdirectories.

```bash
ls -R /etc/nginx
```

`ls -R <dir> | grep ':$'` — List only subdirectory names from recursive output.

```bash
ls -R /var | grep ':$'
```

## Symbolic Links

`ls -l <link>` — Show where a symbolic link points to.

```bash
ls -l /usr/bin/python3
```

`ls -L` — Show information about the target of a symlink, not the link itself.

```bash
ls -lL /usr/bin/python3
```

`ls -F` — Append indicator to entries: / directory, @ symlink, * executable.

```bash
ls -F
```

`ls -p` — Append / indicator to directories only.

```bash
ls -p
```

## Filtering with Globs

`ls *.txt` — List all files with .txt extension.

```bash
ls *.txt
```

`ls *.{jpg,png,gif}` — List files matching multiple extensions using brace expansion.

```bash
ls -lh *.{jpg,png,gif}
```

`ls [A-Z]*` — List files starting with an uppercase letter.

```bash
ls [A-Z]*
```

`ls -I '<pattern>'` — Exclude files matching a pattern from the listing.

```bash
ls -I '*.bak' -I '*.tmp'
```

`ls --hide='<pattern>'` — Hide files matching pattern (overridden by -a).

```bash
ls --hide='*.pyc'
```

## Common Combinations

`ls -lahS` — All files, long format, human-readable, sorted by size.

```bash
ls -lahS
```

`ls -latr` — All files, long format, sorted by time (oldest first).

```bash
ls -latr
```

`ls -lhS | head -n 10` — Show the 10 largest files in a directory.

```bash
ls -lhS | head -n 10
```

`ls -lt | head -n 5` — Show the 5 most recently modified files.

```bash
ls -lt | head -n 5
```

`ls -1 | wc -l` — Count the number of files in a directory.

```bash
ls -1 | wc -l
```

`ls -1 | sort -f` — List files sorted case-insensitively.

```bash
ls -1 | sort -f
```

`ls -d .* 2>/dev/null` — List dotfiles only, suppressing errors if none exist.

```bash
ls -ld .* 2>/dev/null
```

`ls -lR | grep '^-' | wc -l` — Count all files recursively (excluding directories).

```bash
ls -lR | grep '^-' | wc -l
```

<!-- PROSE:outro -->
## Conclusion

`ls` is deceptively simple: its real value comes from combining options. For everyday work, `ls -lah` (everything, readable, with detail), `ls -ltr` (time-sorted, newest at the bottom) and a pipe to `head`, `wc -l` or `grep` cover most needs. Keep in mind that the flags shown assume GNU coreutils – on macOS or BSD, `--full-time`, `-X` or `--hide` behave differently or are missing entirely; there, `gls` from coreutils often helps. `ls` shows ownership and permissions only at a glance – for the full metadata reach for `stat`, for the real file type reach for `file`, and to search across whole trees reach for `find`.

## Further Reading

- [GNU Coreutils: ls invocation](https://www.gnu.org/software/coreutils/manual/html_node/ls-invocation.html) – official reference for every option of GNU ls
- [Wikipedia: ls](https://en.wikipedia.org/wiki/Ls) – background on the Unix command and its history
<!-- PROSE:outro:end -->

## Related Commands

- [stat](https://www.jpkc.com/db/en/cheatsheets/files-text/stat/) – full file metadata beyond ls -l
- [file](https://www.jpkc.com/db/en/cheatsheets/files-text/file/) – determine the real file type by content
- [find](https://www.jpkc.com/db/en/cheatsheets/files-text/find/) – search and filter files recursively

