Skip to main content

How to List Every File in a Folder and Its Subfolders in the Terminal

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

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:

Syntax
ls -R <folder>

In PowerShell:

Syntax
Get-ChildItem -Recurse -Name <folder>
PartWhat it means
lsLists the files and folders in a folder
-RA capital R. Also lists every folder inside it, and every folder inside those
Get-ChildItemPowerShell's command for listing what's in a folder
-RecurseAlso lists everything in the folders inside it
-NamePrints 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​

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

Step 2: List the Top Folder Only​

Terminal
ls trip
Output
budget.txt days 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​

Terminal
ls -R trip

macOS-style terminal window titled Terminal with the command ls -R trip, which lists the trip folder and every folder inside it.

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

macOS-style terminal window titled Terminal showing a real zsh session: the trip folder tree is created, ls trip lists only the top level, and ls -R trip lists trip, trip/days and trip/days/extras.

bash printed the same in every step.

Step 4: Include the Hidden Files​

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

Common Mistakes​

MistakeWhat happensDo this instead
A lowercase r: ls -r tripIt printed plan.txt, days and budget.txt: the top folder in reverse order, and nothing inside daysUse a capital -R
A folder name that doesn't exist: ls -R holidayls: holiday: No such file or directory, with exit status 1Check the spelling with plain ls in the folder above
The same in PowerShell: Get-ChildItem -Recurse -Name holidayGet-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:

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