How to list files in a directory
The first command anyone learns is the one most people never
learn properly. You type ls, you see names, you move on. Then one
day a disk fills up and you need to know which file is eating it, or a deploy
fails and you need to know who owns a config, and the plain ls
you've been typing for years can't answer it. The good news is the answer was
there the whole time. ls is not one command. It's a dozen, and
the flags that turn it into a real tool are easy once you know they exist.
The short answer. To list files in a directory on
Linux, run ls. Add -l for the long format with
permissions, owner, size, and time. Add -a to see hidden files.
Add -S to sort by size and -t by time. That covers
most of what you'll ever need.
What ls actually does
ls is a command that lists the files and directories in a
directory. Under the hood it reads the directory and prints what it finds. A
directory on Linux is just a list of names and inode numbers, and
ls walks that list. The plain form prints the names, sorted
alphabetically, in columns that fit your terminal.
$ ls
Documents Downloads notes.txt projects scripts
That's the whole thing in its simplest form. Names, sorted, in columns.
It's fast, it's fine, and it's the version everyone learns. The problem is it
tells you nothing about the files. Which one is a directory? Which one is
huge? Which one did I touch last? Plain ls can't say.
The long format is where the information lives
Add -l and each entry becomes a full line. This is the format
you'll actually use once you know it exists.
$ ls -l
total 24
drwxr-xr-x 2 kc kc 4096 Oct 24 09:12 Documents
-rw-r--r-- 1 kc kc 512 Oct 23 16:40 notes.txt
-rw-r--r-- 1 kc kc 2048 Oct 24 08:01 deploy.sh
Read it left to right and each field is one fact.
- The first character is the type.
dmeans directory,-means a regular file,lmeans a symlink. - The next nine characters are the permissions, three groups of three. Read, write, execute for the owner, then the group, then everyone else.
- The number after that is the link count, then the owner, then the group.
- The number is the size in bytes.
4096on a directory is normal, it's the size of the directory entry itself, not its contents. - The date is the last modification time, then the name.
That one line answers most of the questions you'll ever ask a filesystem. Who owns this? What are the permissions? How big is it? When was it changed? All of it, in one command.
Hidden files are not secret, they're just hidden
Anything whose name starts with a dot is hidden from a plain
ls. That's the whole mechanism. No security, no encryption, just
a convention that dot files don't show up by default. Config files live in
your home directory as dot files, so .bashrc, .ssh,
and .git all stay out of the way until you ask for them.
$ ls -a
. .. .bashrc .ssh Documents notes.txt
Two entries show up that you didn't expect. . is the current
directory and .. is its parent. They're real entries in every
directory, and -a shows them. If you don't want them cluttering
the output, use -A instead, which shows hidden files but skips
the dot and dot dot.
$ ls -A
.bashrc .ssh Documents notes.txt
That's usually the one you want. Hidden files, no noise.
Sort by size and time when it matters
Two sorts come up constantly. Finding the file eating your disk, and finding what changed most recently.
$ ls -lS
total 24
-rw-r--r-- 1 kc kc 2048 Oct 24 08:01 deploy.sh
-rw-r--r-- 1 kc kc 512 Oct 23 16:40 notes.txt
drwxr-xr-x 2 kc kc 4096 Oct 24 09:12 Documents
-S sorts by size, largest first. When a disk fills up, this
is the command that points you at the culprit. On the Kubernetes nodes I run, a
filling disk is almost never the pods, it's a log file or an old image tarball,
and ls -lhS names it in one line. Add -h to print
sizes in human readable form, 2.0K instead of 2048,
which makes the output far easier to scan.
$ ls -lhS
total 24K
-rw-r--r-- 1 kc kc 2.0K Oct 24 08:01 deploy.sh
-rw-r--r-- 1 kc kc 512 Oct 23 16:40 notes.txt
drwxr-xr-x 2 kc kc 4.0K Oct 24 09:12 Documents
For time, -t sorts by modification time, newest first. This
is the one you reach for when you're trying to figure out what changed right
before something broke.
$ ls -lt
total 24
drwxr-xr-x 2 kc kc 4096 Oct 24 09:12 Documents
-rw-r--r-- 1 kc kc 2048 Oct 24 08:01 deploy.sh
-rw-r--r-- 1 kc kc 512 Oct 23 16:40 notes.txt
Add -r to reverse either sort. ls -lSr is
smallest first, ls -ltr is oldest first. The reverse time sort
is a classic, it shows you the files nobody has touched in a while, which is
often exactly the set you're looking for.
The ls flags at a glance
Here is every flag from this article in one table, what it does, and when to reach for it.
| Command | What it does | Reach for it when |
|---|---|---|
ls | Lists names in the current directory | You just need to see what's here |
ls -l | Long format with permissions, owner, size, time | You need facts about the files |
ls -a | Shows hidden files plus . and .. | You need to see dot files |
ls -A | Shows hidden files without . and .. | You want dot files without the noise |
ls -lS | Sorts by size, largest first | A disk is full and you need the culprit |
ls -lt | Sorts by time, newest first | You need to know what changed recently |
ls -lh | Human readable sizes | You want 2.0K instead of 2048 |
ls -d */ | Lists only directories | You want just the folders |
List one directory without leaving where you are
You don't have to cd into a directory to see what's in it.
Pass the path to ls directly.
$ ls -l /var/log
total 128
-rw-r--r-- 1 root root 40960 Oct 24 09:00 syslog
-rw-r--r-- 1 root root 2048 Oct 24 08:00 auth.log
This is the version that shows up in real work constantly. You're
somewhere else, you need to see what's in a directory, and you don't want to
change where you are to do it. ls -l /path answers it in place.
Verify it works
Run these and check the output against what you expect.
$ ls
$ ls -l
$ ls -la
$ ls -lhS
Plain ls shows names. -l shows the long format.
-la shows everything including hidden files. -lhS
shows human readable sizes sorted largest first. If each one prints more
detail than the last, you've got it.
The honest caveat
There are more flags. ls -R recurses into subdirectories,
ls -d */ lists only directories, and the color output from
ls --color is on by default in most distros. You don't need any
of them to be productive. The four flags here, -l,
-a, -S, and -t, cover the real work.
Everything else is a refinement you'll pick up when a specific problem asks
for it.
One thing worth knowing. ls is not a shell builtin, it's a
real program, and it's part of GNU coreutils. That means it behaves the same
on every Linux box you'll ever touch. Learn it once and it follows you.
The boring conclusion
ls is the first command you learn and the last one you
master, because the plain version is enough until it isn't. The long format
turns a list of names into a list of facts, and the sort flags point you at
the file that matters. None of it is hard. It's just that nobody shows you
the flags until you need them.
What's the ls flag you reach for that most people don't
know?
Frequently asked questions
How do I list files in a directory in Linux?
Run ls to list the names of files and directories in the
current directory. Add -l for the long format with permissions,
owner, size, and time. Add -a to include hidden files. Add
-S to sort by size and -t by time.
What is the difference between ls and ls -l?
ls prints just the names. ls -l prints the long
format, one line per entry, with permissions, owner, group, size, and
modification time. The long format is what you want when you need to know
anything about the files, not just that they exist.
Why does ls not show hidden files?
Files and directories whose name starts with a dot are hidden by default.
ls -a shows them, including the . and
.. entries. ls -A shows them without those two,
which is usually what you actually want.
How do I list files by size or time?
ls -lS sorts by size, largest first. ls -lt
sorts by modification time, newest first. Add -r to reverse
either one. Add -h to print sizes in human readable form.
How do I list only directories with ls?
Run ls -d */ to list only the directories in the current
directory. The -d flag lists the directory entries themselves
rather than their contents, and the */ glob matches only
directories.
What does ls -la mean?
ls -la combines -l (long format) and
-a (all files including hidden ones). It shows every file and
directory, including dot files, with permissions, owner, size, and
modification time.
Resources
Two references cover everything in this article and the flags I left out. Both are the primary sources, so what you read here matches what the tool actually does.
- GNU coreutils manual, ls invocation. The authoritative reference for every
lsflag, straight from the project that ships it. - ls(1) man page. The full flag list and exit status, the same text you get from
man ls.
Next in the series. How to read a file with cat, less, tail, and head is the next rung in the ladder.