How to List All Files and Subdirectory Contents in a Folder

Jan 29, 2013 - 4 Comments

Ever wanted to see not only every file in a given directory, but all files within that directories buried subdirectories? If you’re looking for what is basically a recursive listing of files in a given folder, we’ll show a great trick for the Mac OS X Finder that allows you to do just that, and also demonstrate several more advanced approaches using the command line.

Expand All Subdirectories & List Folder Contents in the OS X Finder

To quickly see what’s in all the subdirectories of a folder within the Finder, open the parent folder and change to list view. Now you need to hold the Option key and click on the little arrow alongside the name of the directory to expand that directory and all subdirectories at the same time.

Expand subfolders in OS X Finder

The result is that each and every subfolder contained within the directory you option-clicked the arrow on will reveal its contents as well:

Recursive directory listings in Mac OS X

Option-clicking that arrow again will cause all subdirectories to close up, otherwise that becomes the new default view when clicking the arrow.

Keep in mind that if you want to view hidden files through this method, you must enable hidden files to be shown in the Mac OS X Finder separately, which will then carry out through every folder until it is disabled again.

The above approach is going to be the easiest method for most users, and the next two methods are focused on the command line and are aimed at those comfortable in the Terminal.

List All Files & Subdirectory Contents from the Command Line

To list all files recursively from the command line, you can attach the -R flag to the traditional ls command. This expands out subdirectories and lists the files contained within them. These commands will work in just about all forms of unix, from OS X to Linux or whatever else you may encounter.

ls -R ~/Desktop/

Sample output would look something like this:

/Users/macuser/Desktop//wallpapers:
Dark Tower.jpg milky-way.jpg
car.jpg ngc602.jpg
flaming-star-nebula.jpg ngc6188Kfir2000.jpg
windows.jpg m33.jpg
/Users/macuser/Desktop//trip:
volcano.jpeg itenerary.txt tickets.JPG

The output is decent, but it could be arranged better.

Using the -lah flags in addition to -R will show permissions, ownership, modification dates, and it makes reading file information much easier. The -a flag is optional, allowing hidden files to be displayed as well.

ls -lahR ~/Desktop/

Sample output would look like:

/Users/macuser/Desktop/wallpapers:
total 5464
drwxr-xr-x@ 11 macuser staff 374B Jan 14 15:32 .
drwxr-xr-x 522 macuser staff 17K Jan 28 10:20 ..
-rw-r--r--@ 1 macuser staff 254K Jan 13 15:44 Dark Tower.jpg
-rw-r--r--@ 1 macuser staff 101K Jan 14 15:32 cars.jpg
-rw-r--r--@ 1 macuser staff 141K Jan 13 15:44 star-nebula.jpg
-rw-r--r--@ 1 macuser staff 206K Jan 14 09:57 nintendo.jpg
-rw-r--r--@ 1 macuser staff 134K Jan 13 15:44 m33.jpg
-rw-r--r--@ 1 macuser staff 1.4M Jan 13 15:30 milky-way.jpg
-rw-r--r--@ 1 macuser staff 153K Jan 13 15:44 ngc602.jpg
-rw-r--r--@ 1 macuser staff 194K Jan 13 15:44 windows.jpg
/Users/macuser/Desktop/trip:
total 360
drwxr-xr-x@ 6 macuser staff 204B Dec 9 13:43 .
drwxr-xr-x 522 macuser staff 17K Jan 22 10:20 ..
-rw-r--r--@ 1 macuser staff 6.0K Dec 9 13:43 .DS_Store
-rw-r--r--@ 1 macuser staff 30K Dec 8 12:41 volcano.jpeg
-rw-r--r--@ 1 macuser staff 45K Dec 8 12:41 itinerary.txt
-rw-r--r--@ 1 macuser staff 88K Dec 9 12:31 tickets.JPG

You’ll notice the path to each file is listed above the files themselves, expanding out the subdirectories a lot like the Finder method mentioned up top. You can get the path to proceed the file name though by using a different command entirely.

List All Files Recursively with Full Directory Paths Shown

Finally, if you want a list of all files with their complete paths specified, you can turn to the find command.

find ~/Desktop/Sample/ -type f

This will dump the full path to each file in a listing:

/Users/macuser/Desktop/Sample/x11.jpg
/Users/macuser/Desktop/Sample/Files/alpha-tool-preview.jpg
/Users/macuser/Desktop/Sample/Files/alpha-tool.jpg
/Users/macuser/Desktop/Sample/Files/reveal-editing-tools-preview.jpg
/Users/macuser/Desktop/Sample/Files/save-transparent-png.jpg

If you’re looking to share a directory listing with someone, the find command likely offers superior results. In such a case, you can dump the output to a file easily by attaching a > to the end like so:

find /Path/To/List -type f > FilesWithPaths.txt

The “-type f” flag signifies regular files only, if you want to show more objects or symbolic links you can check out the man page for find for additional information.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

Posted by: William Pearson in Command Line, Mac OS X, Tips & Tricks

4 Comments

» Comments RSS Feed

  1. Alberto says:

    How to open with one click all the folder in one directory, just clicking on the little triangle of one folder?

    If I remember well it was possible with System 9 with option click.
    And now with OsX it seems that this possibility has disappeared :(

  2. mackie8i says:

    not working for me..

    i change to list view .. holding option and i don’t see the arrow from the left comes up..

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates