ls — list directory contents
The ls command lists files and directories with permissions, owner, size and modification time, hidden ones too.
Cheat sheet
11 commands Printable version-
lscontents of the current directory -
ls /etccontents of another directory -
ls -llong: permissions, owner, size, time -
ls -lalong, including hidden (.bashrc etc.) -
ls -lhsizes in K, M, G -
ls -ltnewest first -
ls -ltrnewest last — handy for logs -
ls -lSlargest first -
ls -ld /var/logthe directory itself, not its contents -
ls -R projectrecursive, with subdirectories -
ls *.confonly 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:
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
14Short 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
5See a directory's own permissions, not its files
Without -d the command lists what is inside, and the directory's own permissions
stay out of view.
Only directories or only files
Numeric UID and GID instead of names
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
6ls: 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
lsshowed. - paths — absolute and relative paths,
~,.and... - permissions — how to read the
rwxr-xr-xcolumn. - 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.
Found an inaccuracy? Report an error in the article