Mac or Windows OS Question

by Jimmy Akin

in Uncategorized

I'm looking for a way to take a directory in the Mac OS and spit out a comma delimited (or similar) file with the names of all the individual files in the directory. Anyone know how to do this?

(Falling back on the Windows OS, how would you do that there?)

If you liked this post, you should join Jimmy's Secret Information Club to get more great info!

What is the Secret Information Club?I value your email privacy


James Colannino December 20, 2011 at 10:26 pm

Jimmy, if you’re on OSX, you have access a the command line via a terminal. Run this script:
FILES=( $(ls | sort) )
for FILE in ${FILES[@]}
echo $LIST
You’ll want to adjust the path to your shell (I’m not sure what it is on OSX.) You can also modify that script to exclude directories.

James Colannino December 20, 2011 at 10:27 pm

FYI: You can also run the same script on Windows if you download Cygwin (

James Colannino December 20, 2011 at 10:45 pm

Sorry for the multitude of comments 😛 Here’s a slightly better version (and I looked up the path to bash on OS-X) — I called the file
#! /bin/bash
if [ -z $1 ]; then
echo “usage: ./ [path]”
exit 1
FILES=( $(ls $1 | sort) )
for FILE in ${FILES[@]}
echo $LIST
Copy that file someplace, make it executable (chmod 750, cd to the directory where it is and type ./ followed by the name of the directory whose contents you want to list.
To output the contents to a file, use console redirection, like this:
./ directory > files.txt

Thomas December 21, 2011 at 7:20 am

If the comma isn’t required, “echo *” will give you the list of files in one line.

jeff December 21, 2011 at 7:20 am

If you are ok with any text format then on mac you can just use the finder, navigate to the directory. Select all the files in the directory. Copy and then goto a text editor and paste. (This was done on Lion)

Lowell Smith December 21, 2011 at 7:46 am

For Windows using PowerShell you would type…
(gci ‘C:\The Path To\The Folder\You Want’ -n) -join “,”
or ‘cd’ to the directory you want and just leave off the path
cd ‘C:\The Path To\The Folder\You Want’
(gci -n) -join “,”
Only downside might be that it includes directories too.

Jeramey December 21, 2011 at 8:11 am

In the Finder window, select all, hit copy and then paste the list of files into your favorite text editor.

Mark December 21, 2011 at 9:45 am

For a one-liner, from the Mac OS X command line, cd to the directory in question, then type:
ls -1 | awk 'NR==1{x=$0;next}NF{x=x", "$0}END{print x}'

Linebyline December 21, 2011 at 3:48 pm

I’d probably recommend going with something like the various scripts given above. But what I personally would do would be to write a simple program to pull up the information and output it in whatever format I want.
However, after monkeying around with the process on Windows for a bit, I came up with this:
Open up a command prompt (hold the windows key and press R, type ‘cmd’ and click OK, but you probably already knew that) and then type this:
dir C:\path\to\Jimmys\Files /B /A:-D > c:\path\where\Jimmy\wants\the\list\filename.txt
Hit enter, and voila! The list is separated by line breaks rather than commas, but it gets the job done.

Grumpy December 21, 2011 at 8:36 pm

I believe the select all/copy/paste technique fails when there are more than a certain number of items in the folder. At least there was the last time I tried it, which was probably back in the Mac OS X 10.5 era.

Jimmy Akin December 21, 2011 at 9:00 pm

Thanks for the help, gang! Problem solved!

EricEwanco January 14, 2012 at 7:09 pm

Just saw this. Probably useless by now but for future reference … I think Mac has a Unix shell (bash shell) built into it. If so you can do this:

ls | xargs -i@ echo @,

Previous post:

Next post: