Skip to main content

How to List Only Files, Not Folders, in the Terminal with find -type f

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

find <folder> lists everything under a folder: the folder itself, every folder inside it and every file. Add -type f and it lists only the files; add -type d and it lists only the folders. In PowerShell, Get-ChildItem -Recurse -File lists only the files.

This guide lists a small practice folder, with real output from macOS 26 and PowerShell 7.6.10.

Syntax​

Syntax
find <folder> -type f
find <folder> -type d
Syntax
Get-ChildItem -Recurse -File -Name <folder>
Get-ChildItem -Recurse -Directory -Name <folder>
PartWhat it means
findGoes through a folder and every folder inside it, and lists what it finds
<folder>Where to start. . means the folder you're in
-type fOnly regular files
-type dOnly folders, which the manual calls directories
Get-ChildItemThe PowerShell command that lists what's in a folder
-RecurseGoes into every folder inside it too
-File / -DirectoryOnly files / only folders
-NamePrints only the names, as paths relative to <folder>

The macOS manual page, man find, says find recursively descends the directory tree for each path listed, and describes -type t as True if the file is of the specified type, where f stands for regular file and d for directory. Microsoft's documentation for Get-ChildItem says: To get a list of files, use the File parameter. You can use the Recurse parameter with File.

The Example​

A folder called trip holds a file, packing-list.txt, and three folders: days, with day-1.txt and day-2.txt, photos, with beach.jpg, and tickets, which is empty. You list it all, then only the files, then only the folders.

Step 1: Create the Practice Folder​

Terminal
mkdir -p trip/days trip/photos trip/tickets
touch trip/packing-list.txt trip/days/day-1.txt trip/days/day-2.txt trip/photos/beach.jpg

mkdir -p creates trip and the three folders inside it, and touch creates the four empty files. Neither prints anything.

Step 2: See Everything find Lists​

Terminal
find trip
Output
trip
trip/tickets
trip/packing-list.txt
trip/days
trip/days/day-1.txt
trip/days/day-2.txt
trip/photos
trip/photos/beach.jpg

The terminal after find trip, which lists eight paths: the folder trip itself, its three folders tickets, days and photos, and the four files inside them.

Eight lines: trip itself, its three folders, and four files. Each line is a path that starts with the folder you gave. find lists each folder's contents in the order it reads them, not sorted, so your order can differ.

Step 3: List Only the Files with -type f​

Terminal
find trip -type f
Output
trip/packing-list.txt
trip/days/day-1.txt
trip/days/day-2.txt
trip/photos/beach.jpg

The terminal after find trip -type f, which lists only the four files: trip/packing-list.txt, trip/days/day-1.txt, trip/days/day-2.txt and trip/photos/beach.jpg.

Only the four files are left. The folders, including the empty tickets, are gone, but find still went into every folder to find the files inside them.

Step 4: List Only the Folders with -type d​

Terminal
find trip -type d
Output
trip
trip/tickets
trip/days
trip/photos

The terminal after find trip -type d, which lists the four folders: trip itself, trip/tickets, trip/days and trip/photos.

The opposite list: the four folders. trip is one of them, because the folder you start from is a folder too.

Step 5: Do the Same in PowerShell​

PowerShell
Get-ChildItem -Recurse -File -Name trip
Output
packing-list.txt
days/day-1.txt
days/day-2.txt
photos/beach.jpg
PowerShell
Get-ChildItem -Recurse -Directory -Name trip
Output
days
photos
tickets

The terminal, running PowerShell, after Get-ChildItem -Recurse -File -Name trip, which lists the four files as paths inside trip, and Get-ChildItem -Recurse -Directory -Name trip, which lists days, photos and tickets.

The same files and folders, with two differences from find. The paths start inside trip, because, in Microsoft's words, The names returned are relative to the value of the Path parameter. And -Directory leaves out trip itself. On Windows, the paths use backslashes, like days\day-1.txt.

Step 6: List the Files of the Folder You're In​

Move into trip, then list its files. find gets a ., which means the folder you're in; Get-ChildItem uses that folder when you leave the folder out.

Terminal
cd trip
find . -type f
Output
./packing-list.txt
./days/day-1.txt
./days/day-2.txt
./photos/beach.jpg

The terminal after cd trip and find . -type f, which lists the four files with paths that start with ./.

With find, the paths now start with ./, because they start from ..

Step 7: See What Happens When the Folder Isn't There​

Go back up to the folder that holds trip, then make a typo, trips instead of trip:

Terminal
cd ..
find trips -type f
Output
find: trips: No such file or directory

The terminal after cd .. and find trips -type f, which prints find: trips: No such file or directory.

Both name the folder they couldn't find. find also exits with status 1, which tells a script that something went wrong.

Common Mistakes​

MistakeWhat happensDo this instead
find -type f, without a folder, on macOSfind: illegal option -- t, then a usage line: macOS's find needs a folder to start fromGive a folder: find . -type f
Expecting -type d to leave out the folder you start fromThe starting folder is listed firstExpect it; it's a folder too
Expecting the list in alphabetical orderfind lists each folder's contents in the order it reads themAdd -s on macOS, as in the FAQ

FAQ​

How do I get the list in alphabetical order?​

On macOS, add -s before the folder:

Terminal
find -s trip -type f
Output
trip/days/day-1.txt
trip/days/day-2.txt
trip/packing-list.txt
trip/photos/beach.jpg

The manual describes -s as: Cause find to traverse the file hierarchies in lexicographical order, i.e., alphabetical order within each directory.

Conclusion​

find trip -type f listed only the four files under trip, from every folder inside it, and find trip -type d listed only the folders, trip included. In PowerShell, Get-ChildItem -Recurse -File -Name trip listed the same files, as paths inside trip. Give find a folder, . for the one you're in, and add -s on macOS to sort the list.

Additional Resources​