How to Use ls Command in Linux/Unix

How to Use ls Command in Linux/Unix

Seeing what’s in a folder is one of the first things you’ll do in a Linux terminal. The ls command lists a folder’s contents, and its options can show hidden files or add details like size and modification time.

Below are the ls options that change which entries appear and how they’re sorted.

What is the ls command?

With no path, ls lists entries in the current working directory, showing names or the requested details.

A file operand asks ls to show that file’s information. A directory operand normally makes it list the directory’s contents, and options change which entries appear or which details accompany each name.

These examples use GNU ls on Linux. Other Unix variants can differ in supported options and output.

ls

Step 1: List entries in a directory

Run ls without a path to list the current directory, or name a directory to list somewhere else. Add -1 when you want each entry on its own line rather than in columns.

ls
ls -1
ls /etc

The retained terminal image shows names and colors arranged in columns, as an example from its own terminal rather than files on your machine.

Example terminal output from the ls command
Example output from ls in a terminal, with entries arranged in columns.

When you pass a directory operand, ls normally shows the names inside it, while a file operand displays information about that single file. If you need a test file, see how to create a file in Linux first.

A listing shows names rather than file contents, so use the site’s guide to read a file with cat or more when you need to inspect its text.

Step 2: Show hidden entries

Plain ls omits names that begin with a dot, while -a includes them and -A leaves out the implied current and parent directory entries.

ls -a
ls -A
  • -a includes every entry, including . and ..
  • -A includes dot-prefixed names but excludes . and ..
ls -a output with dot-prefixed names
The screenshot shows hidden names alongside ordinary entries.

The leading dot is a naming convention that ls recognizes, not a permission setting. For more on dot-prefixed names, see the guide to show hidden files in Linux.

Step 3: Read file details with long format

The -l option adds a row of metadata for each entry. On GNU ls, add -h to show readable size units.

ls -l
ls -lh
FieldWhat it reports
First character and permission bitsEntry type, followed by its access modes.
Owner and groupThe user and group recorded for the entry.
Size and timestampThe reported entry size and modification time.
NameThe entry name, or a symbolic link and its target.
Long ls listing with permissions, owners, sizes, and timestamps
A long listing places metadata beside each entry.

The size beside a directory describes its directory entry, not the space used by files beneath it. For ownership changes, see the chown command guide, and use the guide to symbolic links in Linux to understand link targets.

Step 4: Sort entries by size or modification time

GNU ls sorts names alphabetically by default. Use -S for descending reported size, or -t for newest modification time first, then add -r to reverse that order.

ls -lSh
ls -lt
ls -ltr

In the first command, -S chooses the order and -h changes the displayed units. The size sort still uses the reported size, not the human-readable label.

I saw ls -lt put the newest modification times first. Adding -r in ls -ltr reversed that order.

Long ls listing sorted by modification time with newest entries first
The -t option places entries with newer modification times above older ones.

Time and size are different sort keys. The retained size example uses -S, while -h changes the units it prints.

ls output sorted by size with and without readable units
The order stays the same when -h changes the displayed size units.

When a row is a directory, its size still describes that directory entry rather than its contents. For total directory usage, use du instead of treating -S as a recursive size sort.

Step 5: List a directory entry or descend into it

A directory operand normally opens that directory for listing. GNU ls -d reports each operand itself, while -R descends through subdirectories.

ls -d *
ls -ld */
ls -R
CommandWhat it lists
ls -d *Each non-hidden name matched by the shell, including files and directories.
ls -ld */Paths that match */ in the shell, shown with their long details.
ls -REntries beneath the current directory and its subdirectories, grouped by path.

I ran ls -d * in the example directory, and its output included directories, a symbolic link, and regular files. The shell expanded * before ls received the names, so -d did not filter for directories.

Terminal output of ls -ld */ showing the Docs and bin directories themselves
The shell expands */ to matching directory entries, and -d prints those entries instead of listing their contents.

The screenshot shows the matched directories themselves. Use the retained recursive listing when you need the names inside each directory, not just the directory entries.

Recursive ls output grouped by directory path
The -R output labels each directory path before listing its entries.

For counting rather than displaying names, use the guide to count files in a directory.

When ls output looks different

An unexpected listing often comes from an option, shell setting, or a misunderstanding of directory metadata. Check which command the shell resolves before changing files.

What you seeWhy it happensNext action
Dot-prefixed names are absentPlain ls omits them.Run ls -A or ls -a.
A directory’s size seems smallLong format reports the directory entry.Run du -sh Docs for its tree usage.
Names have unexpected colorsAn alias or color option changes presentation.Run type ls to inspect the shell command.
A wildcard result includes filesThe shell expands the wildcard before ls runs.Use a directory glob such as */.

Use du to measure directory-tree usage, then check the local manual when an option behaves differently on your system.

du -sh Docs
man ls
ls --help

ls –help is available on GNU systems, while man ls opens the manual installed for the local implementation. The Linux ls manual page documents GNU/Linux options, so use your system’s own manual when you are working on another Unix family.

GNU ls can add colors for terminal output, but color is presentation rather than proof of permissions or file type. In an interactive Bash shell, define a shortcut with an alias. It applies only in that shell unless you add it to a startup file.

type ls
alias lsa='ls -la'
lsa
ls --color=auto

For directory usage sorted by size, see the site’s guide to listing directories by size. An ls size sort and a disk-usage report answer different questions.

Keep the path attached to the listing

A listing is useful only when you know which directory it describes. Pass the path explicitly when you are checking a location other than the current working directory.

ls -lh /var/log

For the manual command, see the site’s guide to using man pages in Linux.

Frequently asked questions about ls

Does ls -l show the total size of a directory?

No. The size field describes the directory entry itself, not the space used by its contents. Run du -sh Docs when you want a total for that directory tree.

How do I print one entry per line?

Run ls -1. The flag forces one entry per output line instead of arranging names in columns.