Where to start First steps track · 15 stages
Linux Labs

ls — list directory contents

6 min Ubuntu 24.04 · GNU coreutils 9.4

The ls command lists files and directories with permissions, owner, size and modification time, hidden ones too.

Cheat sheet

11 commands Printable version
  • ls contents of the current directory
  • ls /etc contents of another directory
  • ls -l long: permissions, owner, size, time
  • ls -la long, including hidden (.bashrc etc.)
  • ls -lh sizes in K, M, G
  • ls -lt newest first
  • ls -ltr newest last — handy for logs
  • ls -lS largest first
  • ls -ld /var/log the directory itself, not its contents
  • ls -R project recursive, with subdirectories
  • ls *.conf only names matching a pattern

Overview

ls (list) prints the names of files and directories. With no arguments it lists the current directory; with an argument, the given path or file. The default order is alphabetical.

The long format, ls -l, is the main way to see permissions and ownership:

text
student@ubuntu:~$ ls -l /etc/hostname /etc/hosts /etc/ssh/ssh_config
-rw-r--r-- 1 root root    7 Oct 18 09:12 /etc/hostname
-rw-r--r-- 1 root root  221 Oct 18 09:12 /etc/hosts
-rw-r--r-- 1 root root 1649 Apr  5  2024 /etc/ssh/ssh_config
student@ubuntu:~$ ls -ld /etc/ssh
drwxr-xr-x 4 root root 4096 Oct 18 09:12 /etc/ssh

Columns from left to right: type and permissions (- file, d directory, l link), hard link count, owner, group, size in bytes, modification time, name. Files older than six months show the year instead of the time.

Names starting with a dot (.bashrc, .ssh) are hidden: you won't see them without -a. -a also adds . (the directory itself) and .. (its parent); -A shows everything except those two.

Options

14

Short options can be combined: ls -l -a -h and ls -lah are the same. Full list — man ls.

Option What it does Example
-l Long format — type, permissions, links, owner, group, size, time, name. ls -l /etc
-a Show hidden names (starting with a dot), including . and .. ls -la
-h With -l, print sizes as K, M, G instead of bytes. ls -lh
-t Sort by modification time, newest first. ls -lt
-A Like -a, but without . and .. ls -A
-r Reverse the sort order. ls -ltr
-S Sort by size, largest first. ls -lS
-d List the directory itself, not its contents. ls -ld /var/log
-R List subdirectories recursively. ls -R project
-F Append a type marker — / directory, * executable, @ link. ls -F
-i Print each file's inode number. ls -i
-n Like -l, but owner and group as numeric UID and GID. ls -n /home
-1 One name per line. ls -1
--group-directories-first List directories before files. ls -l --group-directories-first

Nothing found. Try a shorter query.

Recipes

5

Find the most recently changed file

bash
ls -lt /var/log | head -5         # the five newest
ls -ltr /var/log                   # newest at the bottom, by the prompt

See a directory's own permissions, not its files

bash
ls -ld ~/.ssh                      # drwx------ ... /home/student/.ssh
ls -ld /tmp                        # drwxrwxrwt — t: a shared directory

Without -d the command lists what is inside, and the directory's own permissions stay out of view.

Only directories or only files

bash
ls -d */                           # directories in the current one
ls -p | grep -v /                  # everything except directories
ls -F                              # / dir, * executable, @ link

Numeric UID and GID instead of names

bash
ls -n /home                        # 1000 1000, not student student

Useful when a file belongs to a deleted user: there will be a number instead of a name anyway.

Ubuntu's short aliases

Ubuntu's ~/.bashrc already defines alias ll='ls -alF', alias la='ls -A' and alias l='ls -CF'. type ll shows what a name stands for.

Troubleshooting

6

ls: cannot access 'x': No such file or directory

There is no such path relative to the current directory. Check pwd, the letter case (Notes and notes are different names), and complete the name with Tab — the shell only completes paths that exist. More on paths — paths.

ls: cannot open directory: Permission denied

Reading the listing needs r on the directory, entering it needs x. Check the directory itself: ls -ld /root. Read system directories with sudo ls.

The file isn't in the list, but it exists

Its name starts with a dot — add -a. Or it's deeper in the tree: ls -R or find (find . -name 'name*').

The directory shows 4096 bytes but holds gigabytes

ls -l shows the size of the directory entry, not of its contents. For the whole directory use du -sh dir — see disk-usage.

The name is printed in quotes: 'my file'

That's how ls shows names with spaces and special characters so you can paste them into a command. The name itself has no quotes. Quote such a name in commands or complete it with Tab.

A script parses ls output and breaks on spaces

ls output is meant for people. In scripts loop over files with a shell pattern (for f in *.log) or use find with -print0.

See also

  • files-basics — an overview of the basic file commands.
  • cd — go into a directory that ls showed.
  • paths — absolute and relative paths, ~, . and ...
  • permissions — how to read the rwxr-xr-x column.
  • stat — every timestamp and permission of one file in detail.
  • disk-usage — how much space a whole directory takes.
  • find — searching for files across the whole tree.
Loading…