ls Command Explained
ls lists directory contents, and it is very likely the single most-typed command by anyone who has ever used a Linux terminal. Despite its simplicity, its flags cover a surprising amount of ground once you get past the basics.
Basic usage
ls
# Documents Downloads Pictures projects
ls /var/log
# syslog auth.log nginx/ mysql/
ls ~/projects/linuxdork
With no arguments, ls lists the current working directory. Given a path, it lists that path instead, without changing your actual location.
Long format: ls -l
ls -l
# drwxr-xr-x 4 colton colton 4096 Jul 9 09:12 Documents
# -rw-r--r-- 1 colton colton 2048 Jul 8 14:33 notes.txt
Each line has, in order: permission string, link count, owner, group, size in bytes, last modification date, and name. The permission string is the part most worth learning to read fluently:
-rwxr-xr--
│└┬┘└┬┘└┬┘
│ │ │ └─ others: read only
│ │ └──── group: read + execute
│ └─────── owner: read + write + execute
└───────── file type (- = regular file, d = directory, l = symlink)
Showing hidden files: ls -a
ls -a
# . .. .bashrc .config Documents Downloads
ls -A
# .bashrc .config Documents Downloads
Anything starting with a dot is hidden from a plain ls. -a reveals everything, including the special . (this directory) and .. (parent directory) entries. -A shows hidden files without those two special entries cluttering the output, which is usually what you actually want.
Combining flags: ls -la
ls -la
# drwxr-xr-x 20 colton colton 4096 Jul 9 09:00 .
# drwxr-xr-x 6 root root 4096 Mar 1 10:00 ..
# -rw------- 1 colton colton 312 Jul 8 22:14 .bash_history
# drwxr-xr-x 4 colton colton 4096 Jul 9 09:12 Documents
This is probably the single most common ls invocation in practice: long format plus hidden files, giving you a complete, detailed view of everything in a directory in one command.
Human-readable sizes and color
ls -lh
# -rw-r--r-- 1 colton colton 2.0K Jul 8 14:33 notes.txt
# -rw-r--r-- 1 colton colton 1.3G Jul 8 09:00 backup.tar.gz
ls --color=auto
-h converts raw byte counts into human-friendly units (K, M, G) when combined with -l. Color output, usually enabled by default through a shell alias (alias ls='ls --color=auto' in .bashrc), distinguishes directories, executables, symlinks, and archives visually without needing to parse the permission string every time.
Sorting output
ls -lS # largest file first
ls -lSr # smallest file first
ls -lt # most recently modified first
ls -ltr # oldest modification first (often the most useful direction)
ls -lX # group by file extension
ls -ltr deserves a special mention: sorting oldest-first means the most recently changed files appear at the bottom of the output, right where your cursor already is, which is generally more convenient than scrolling up to find them at the top.
Listing recursively and showing directories only
# List a directory and every subdirectory beneath it
ls -R
# List only directories, not files, in the current location
ls -d */
# Show a directory's own metadata instead of its contents
ls -ld /var/log
ls -d */ is a common pattern for getting a quick list of just the subdirectories in the current location, filtering out regular files entirely. ls -ld on a specific path shows that directory’s own permission line, rather than listing what is inside it, which is useful when checking a directory’s own ownership and permission bits.
Practical combinations worth memorizing
ls -lah # long, all files, human-readable sizes: the everyday default
ls -lt | head # ten most recently modified entries
du -sh */ # size of every subdirectory (a common companion to ls)
Frequently Asked Questions
What does the ls command do?
ls lists the files and directories inside a given location, defaulting to the current working directory if no path is provided. It is one of the most frequently typed commands in Linux, used to see what exists in a folder before deciding what to do next.
What is the difference between ls, ls -l, and ls -la?
Plain ls prints just the names of files and directories, often in a compact multi-column layout. ls -l switches to long format, showing one entry per line with permissions, owner, group, size, and modification date. ls -la adds the -a flag, which also shows hidden files and directories, those whose names start with a dot, which are excluded from a plain ls or ls -l listing by default.
How do I read the permission string in ls -l output, like -rwxr-xr—?
The first character indicates the file type: a dash for a regular file, d for a directory, l for a symbolic link. The next nine characters are three groups of three: read, write, execute permissions for the owner, the group, and everyone else, in that order. rwxr-xr— means the owner has full read, write, and execute access, the group has read and execute but not write, and everyone else has only read access.
How do I sort ls output by size or by modification time?
ls -lS sorts by file size, largest first. ls -lt sorts by modification time, most recently modified first. Adding -r to either reverses the order, so ls -lSr shows smallest files first and ls -ltr shows oldest modifications first, which is often more useful than newest-first when scanning for the most recent changes at the bottom of a long listing.
How do I make ls output easier to read, with colors and human-readable sizes?
ls —color=auto enables color coding by file type (directories, executables, symlinks, and archives typically get distinct colors), and is enabled by default via an alias in most distributions’ shell configuration. ls -lh shows file sizes in human-readable units like 4.2K or 1.3G instead of a raw byte count, which is far easier to scan visually in a long listing.
Why does ls not show all my files by default?
ls hides any file or directory whose name begins with a dot, a Unix convention for marking a file as hidden, typically used for configuration files and directories that would otherwise clutter a normal directory listing, such as .bashrc or .config. Use ls -a to include these hidden entries, or ls -A to include them while still excluding the special . and .. entries that represent the current and parent directory.