ls Command Guide for Listing Directory Contents
This guide helps you understand and use the ls (list) command, the primary way to see what files and folders exist in a directory.
What is ls, and Why Use It?
The ls command lists the contents of a directory — the files, folders, and other entries it contains. On its own it shows names only, in columns, with no details.
Why Learn ls?
- See what’s there: Check a folder’s contents before copying, moving, or deleting anything.
- Inspect details: With flags you can see sizes, permissions, owners, and modification times.
- Find things fast: Combined with sorting and filtering,
lsis a quick search tool. - Confirm your work: After a
cpormv,lsis how you verify the result.
lsis the command you run most often of all. Getting comfortable with its flags pays off immediately.
1. List the Current Directory
ls
- Shows the names of files and folders in your current working directory.
Example:
ls
Documents Pictures notes.txt scripts
2. List a Specific Directory
ls /etc/nginx/
- Works with any absolute path.
ls ../logs
- Works with any relative path.
ls ~/Downloads
~expands to your home directory, so this lists your Downloads folder.
3. List Multiple Directories at Once
ls dir1 dir2 dir3
- Each directory is shown under its own heading.
ls *
- The shell expands
*to every entry in the current directory, so this behaves like several arguments tols.
4. Show Hidden Files
ls -a
-a(all) includes entries beginning with a dot, such as.bashrcand.git.
ls -A
-A(almost all) also hides.and.., which is usually what you want in scripts.
Hidden files are normal configuration files. Forgetting
-ais the most common reason a folder “looks empty”.
5. Show Detailed Information
ls -l
- Long format. One entry per line with permissions, link count, owner, group, size, modification time, and name.
Example:
ls -l notes.txt
-rw-r--r-- 1 alice staff 2048 4 Dec 10:32 notes.txt
Reading that line:
| Field | Value | Meaning |
|---|---|---|
| Permissions | -rw-r--r-- | Who can read and write the file |
| Link count | 1 | Number of hard links |
| Owner | alice | User who owns the file |
| Group | staff | Group that owns the file |
| Size | 2048 | Size in bytes |
| Modified | 4 Dec 10:32 | Last modification timestamp |
| Name | notes.txt | The file itself |
6. Combine the Common Flags
ls -la
- Long format and hidden files — the everyday “show me everything” command.
ls -alh
- Adds
-h(human-readable), so sizes appear as2K,1.5M, or3.2Ginstead of raw bytes.
ls -lt
- Sorts by modification time, newest first.
ls -ltr
-r(reverse) flips the sort, so the oldest entries come last — handy for tailing a log directory.
7. Sort and Filter Output
ls -lS
- Sort by file size, largest first.
ls -lU
- Sort by directory order rather than alphabetically (faster, but unsorted).
ls *.txt
- Only entries matching the pattern. Combine with
-lfor details.
ls -d */
- Lists directory names only, not their contents.
-dmeans “describe the directory itself”.
8. Classify Entries
ls -F
- Appends a character to each name:
/for directories,*for executables,@for symlinks,|for pipes.
ls -G
- Colourises the output by file type. On many systems this is on by default; if colours are missing, add the flag explicitly.
ls --color=never
- Forces colour off, which is useful when piping output into other tools.
9. Show Files in a Recursively Structured Way
ls -R
- Recurses into subdirectories and lists each one.
For a proper visual tree, prefer
tree, which draws the hierarchy with box-drawing characters.ls -Ris hard to read past a couple of levels.
10. Sort Human Readably by Version
ls -v
- Natural sort order, so
file2.txtcomes beforefile10.txtinstead of after it.
Common Pitfalls
lswith no flags hides dotfiles. Use-awhen looking for configuration files.ls -l directory/lists the contents, not the directory. Usels -ld directory/to see the directory’s own line.- Aliases can mislead. Some systems alias
lstols --color=auto;ls -l | catshows the unadorned output. - Sorting flags override each other. The last one on the command line wins:
ls -ltSsorts by size, not time.
Related Commands
| Command | Description |
|---|---|
cd | Move into a directory |
pwd | Print the current directory path |
tree | Draw a directory tree |
file | Identify a file’s type |
stat | Show detailed file metadata |
du | Report directory disk usage |
find | Search for files by name, size, or time |
locate | Find files quickly using a prebuilt database |
ls Cheatsheet
| Command | Description |
|---|---|
ls | List current directory |
ls path/ | List a specific directory |
ls -a | Include hidden (dot) files |
ls -l | Long format with permissions and sizes |
ls -h | Human-readable sizes |
ls -la | Everything, with details |
ls -lh | Detailed with human-readable sizes |
ls -lt | Sort by modification time, newest first |
ls -lS | Sort by size, largest first |
ls -r | Reverse the sort order |
ls -d */ | List directory names only |
ls -F | Mark directories and executables |
ls -R | Recurse into subdirectories |
ls -v | Natural (version) sort order |
ls --color=never | Disable colour output |