How to List Every File in a Folder and Its Subfolders in the Terminal
ls lists one folder. ls -R lists that folder, then every folder inside it, all the way down. In PowerShell, Get-ChildItem -Recurse -Name lists the same files as one list of paths.
This guide lists a practice folder three levels deep, with real output from zsh 5.9, bash 3.2 and PowerShell 7.6 on macOS.
Syntax
In bash and zsh:
ls -R <folder>
In PowerShell:
Get-ChildItem -Recurse -Name <folder>
| Part | What it means |
|---|---|
ls | Lists the files and folders in a folder |
-R | A capital R. Also lists every folder inside it, and every folder inside those |
Get-ChildItem | PowerShell's command for listing what's in a folder |
-Recurse | Also lists everything in the folders inside it |
-Name | Prints only the names, as paths inside <folder> |
<folder> | The folder to start from |
The macOS manual page, man ls, describes -R as Recursively list subdirectories encountered. A subdirectory is a folder inside a folder. Microsoft's documentation for Get-ChildItem describes -Recurse as Gets the items in the specified locations and in all child items of the locations., and -Name as Gets only the names of the items in the location. It adds: The names returned are relative to the value of the Path parameter. Here, the Path parameter is <folder>.
The Example
You create a folder called trip with two files and a days folder in it. days holds two files and an extras folder with one more file. There's also a hidden file, .notes.txt, in trip.
Step 1: Create a Folder Tree to List
- bash / zsh
- PowerShell
mkdir -p trip/days/extras
touch trip/plan.txt trip/budget.txt trip/days/monday.txt trip/days/tuesday.txt trip/days/extras/museum.txt trip/.notes.txt
mkdir -p creates trip, days and extras in one go, and touch creates the six files. Neither prints anything.
New-Item -ItemType Directory -Path trip/days/extras | Out-Null
New-Item -ItemType File -Path trip/plan.txt, trip/budget.txt, trip/days/monday.txt, trip/days/tuesday.txt, trip/days/extras/museum.txt, trip/.notes.txt | Out-Null
The first New-Item creates the three folders, and the second creates the six files, one for each path in its list. | Out-Null Hides the output instead of sending it down the pipeline or displaying it.
Step 2: List the Top Folder Only
- bash / zsh
- PowerShell
ls trip
budget.txt days plan.txt
Get-ChildItem -Name trip
days
budget.txt
plan.txt
Without -R or -Recurse, the list stops at the first level: it shows the days folder, but nothing inside it.
Step 3: List Every Level
- bash / zsh
- PowerShell
ls -R trip

budget.txt days plan.txt
trip/days:
extras monday.txt tuesday.txt
trip/days/extras:
museum.txt
ls -R listed trip first, then each folder it found inside, under a heading made of the folder's path and a colon: trip/days:, then trip/days/extras:. A blank line separates one folder from the next.

bash printed the same in every step.
Get-ChildItem -Recurse -Name trip

days
budget.txt
plan.txt
days/extras
days/monday.txt
days/tuesday.txt
days/extras/museum.txt
PowerShell printed one path on each line rather than one block for each folder, every path starting from inside trip.

Step 4: Include the Hidden Files
- bash / zsh
- PowerShell
ls -AR trip
.notes.txt budget.txt days plan.txt
trip/days:
extras monday.txt tuesday.txt
trip/days/extras:
museum.txt
.notes.txt starts with a dot, so ls leaves it out until you add -A. The manual: Include directory entries whose names begin with a dot (‘.’) except for . and ..
Get-ChildItem -Recurse -Name -Force trip
days
.notes.txt
budget.txt
plan.txt
days/extras
days/monday.txt
days/tuesday.txt
days/extras/museum.txt
The documentation: By default, Get-ChildItem doesn't display hidden items. Use the Force parameter to get hidden items.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A lowercase r: ls -r trip | It printed plan.txt, days and budget.txt: the top folder in reverse order, and nothing inside days | Use a capital -R |
A folder name that doesn't exist: ls -R holiday | ls: holiday: No such file or directory, with exit status 1 | Check the spelling with plain ls in the folder above |
The same in PowerShell: Get-ChildItem -Recurse -Name holiday | Get-ChildItem: Cannot find path '/Users/you/tree-demo/holiday' because it does not exist. | Check the spelling with Get-ChildItem -Name |
The manual describes the lowercase option as Reverse the order of the sort.
FAQ
How do I make PowerShell stop after one level?
Use -Depth in place of -Recurse. Get-ChildItem -Name -Depth 1 trip listed trip and the folders one level down, and left out days/extras/museum.txt:
days
budget.txt
plan.txt
days/extras
days/monday.txt
days/tuesday.txt
The documentation: The Depth parameter determines the number of subdirectory levels that are included in the recursion and displays the contents.
Does ls -R work in PowerShell?
On macOS, yes: ls -R trip in PowerShell printed the same blocks as zsh, because ls there is the system's own ls. Microsoft's documentation lists ls as a short name for Get-ChildItem on Windows only. On every platform, the short names are dir and gci.
Conclusion
ls -R trip listed all three levels of trip, down to trip/days/extras and museum.txt, and Get-ChildItem -Recurse -Name trip printed the same files as paths, one on each line. Use a capital -R, and add -A or -Force when you want the hidden files too.
Additional Resources
man lsin your terminal: every option oflson macOS.- Get-ChildItem (Microsoft Learn):
-Recurse,-Name,-Depthand-Force.
