Skip to main content

How to List the Files in a Folder in the Terminal with ls

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
ls [<options>] [<folder>]
PartWhat it means
lsShort 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​

Terminal
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​

Terminal
ls notes

macOS-style terminal window titled Terminal with the command ls notes, which lists the files and folders inside the notes folder.

Output
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:

Terminal
cd notes
ls
Output
ideas.txt old todo.txt

Step 3: Change How the List Looks​

Terminal
ls -1
Output
ideas.txt
old
todo.txt

-1 is the digit one. The manual: Force output to be one entry per line.

Terminal
ls -F
Output
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.

Terminal
ls -a
Output
. .. .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​

Terminal
cd ..
ls archive
echo $?
Output
ls: archive: No such file or directory
1

macOS-style terminal window titled Terminal showing a real zsh session: ls lists ideas.txt, old and todo.txt, then lists them one per line, with old/ marked, with the hidden .draft.txt, and finally prints No such file or directory for archive.

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​

MistakeWhat happensDo this instead
A folder name with a space, not quoted: ls my notesls: my: No such file or directory, then the contents of notes, a different folderQuote the name: ls 'my notes' printed plan.txt
Looking for a hidden file with plain lsls notes doesn't show .draft.txtAdd -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:

Output
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 ls in your terminal: every option of ls on macOS.