How to List the Files in a Folder in the Terminal with ls
ls lists the files and folders inside a folder. Give it a folder's name to list that folder, or no name at all to list the one you're in.
This guide lists a small practice folder, with real output from zsh 5.9 and bash 3.2 on macOS.
Syntax
ls [<options>] [<folder>]
| Part | What it means |
|---|---|
ls | Short for list |
[<options>] | Optional. Letters after a dash that change the list, such as -a |
[<folder>] | Optional. The folder to list. Without it, ls lists the folder you're in |
The macOS manual page, man ls, calls a name you give the command an operand, and a folder a directory: For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information. And with no name: If no operands are given, the contents of the current directory are displayed.
The Example
You create a folder called notes with two files, a folder named old and a hidden file, .draft.txt, and list it in different ways.
Step 1: Create a Folder to List
mkdir -p notes/old
touch notes/todo.txt notes/ideas.txt notes/.draft.txt
mkdir -p creates notes and the old folder inside it, and touch creates the three files. Neither prints anything.
Step 2: List the Folder
ls notes

ideas.txt old todo.txt
ls printed the names in notes, side by side. The manual on -C: Force multi-column output; this is the default when output is to a terminal. .draft.txt isn't in the list, because its name starts with a dot. Step 3 shows how to see it.
Without a folder name, ls lists the folder you're in. Move into notes with cd and run it on its own:
cd notes
ls
ideas.txt old todo.txt
Step 3: Change How the List Looks
ls -1
ideas.txt
old
todo.txt
-1 is the digit one. The manual: Force output to be one entry per line.
ls -F
ideas.txt old/ todo.txt
-F marks the folders, so old/ stands out from the files. The manual: Display a slash (‘/’) immediately after each pathname that is a directory.
ls -a
. .. .draft.txt ideas.txt old todo.txt
-a shows the names that start with a dot: Include directory entries whose names begin with a dot (‘.’). .draft.txt is there now, and so are two entries called . and ...
Step 4: See What ls Says About a Folder That Isn't There
cd ..
ls archive
echo $?
ls: archive: No such file or directory
1

cd .. moves back up out of notes. There's no archive folder, so ls printed its name followed by No such file or directory. echo $? prints the exit status of the command before it, and it printed 1. The manual: The ls utility exits 0 on success, and >0 if an error occurs. bash printed the same in every step.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A folder name with a space, not quoted: ls my notes | ls: my: No such file or directory, then the contents of notes, a different folder | Quote the name: ls 'my notes' printed plan.txt |
Looking for a hidden file with plain ls | ls notes doesn't show .draft.txt | Add -a, or -A |
In the first row, the space split the name in two. ls got my, which doesn't exist, and notes, which does, so it listed notes under a notes: heading.
FAQ
Why does ls sometimes print one name per line?
Because it only uses columns on a screen. ls notes | cat sends the list through another command first, and it came out one name per line:
ideas.txt
old
todo.txt
The manual: By default, ls lists one entry per line to standard output; the exceptions are to terminals or when the -C or -x options are specified.
Can I see hidden files without . and ..?
Yes, with -A. ls -A notes printed .draft.txt, ideas.txt, old and todo.txt. The manual: Include directory entries whose names begin with a dot (‘.’) except for . and ..
Conclusion
ls notes listed ideas.txt, old and todo.txt. -F marked old/ as a folder, -a showed the hidden .draft.txt, and a folder that isn't there gave No such file or directory with exit status 1. Quote any folder name that has a space in it.
Additional Resources
man lsin your terminal: every option oflson macOS.
