Save a List of Files & Folder Contents Into a Text File

Oct 11, 2012 - 17 Comments

A directory listing saved as a text file

Saving a complete listing of files contained with a folder is easy, and there are two quick ways to save that list as a text file.

Save a List of Files from Finder

The first approach may be easiest for most users and is done through the OS X Finder and TextEdit app, it’s a simple matter of copying and pasting:

  • Open the folder you want to get a content listing of and hit Command+A (Select All) followed by Command+C (Copy)
  • Now launch TextEdit and pull down the “Edit” menu and select “Paste and Match Style”, or hit Command+Option+Shift+V
  • Save the directory listing as either a .txt or .rtf

Save a list of files into a text document

Saving a Detailed List of Files from Terminal

The second approach uses the command line, and despite being done through Terminal is not much more complicated than the copy & paste approach outlined above. Launch Terminal from /Applications/Utilities/ to get started.

At it’s most basic, the command is as follows:

ls > contents.txt

Including hidden files in the list requires the -a flag:

ls -a > allcontents.txt

To dump the contents of a specific folder, specify the directory path as follows:

ls /Library/Preferences/ > LibPrefsList.txt

Attaching certain flags to the ls command will allow the list to reveal more than just a file content list, the -l flag will also list permissions, file ownership, and modification dates:

ls -la /Library/Preferences/ > detailedprefsinfo.txt

Because the ls command accepts flags that detail additional attributes of files and folders, it can be much more informative than the Finder & TextEdit approach, which does not display details like file ownership or document permissions.

The command line approach also lets you do things like compare two directory listings by using the diff command, that can either be done by comparing the output files against each other, or even directly comparing the folders and saving difference those results as a text file.

.

Related articles:

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

17 Comments

» Comments RSS Feed

  1. fab says:

    what does the terminal string look like if I am trying to make a list of everything (folders and contents) on an external drive? I keep getting “No such file or directory” error – thanks

  2. Monica says:

    How can I get a list of subfolders and their files. I’d like a list of all files including subfolder names and the files they contain.

  3. Diesel says:

    I have a folder with a content of 25401 files (images) – of course the copy/paste method would completely destroy my machine with a size of ~8 MB per file.

    Splitting the folder is (unfortunately) not an option.
    Any idea how to generate a list of names from this giant?

    Greetings
    diesel

    • Paul says:

      I’d say try the command line approach:

      First in Terminal change to the directory path:

      cd /Path/To/Image/Folder/

      Then use ls with redirection to a text file containing the listings file names:

      ls > FolderContents.txt

  4. roland diaz says:

    THANK YOU , THANK YOU THANK YOU!!!!!!!

  5. Janet Austin says:

    Thank you!

    I figured out the copy/paste method on my own, but have been looking for the detailed info method for YEARS!

    I have never used Terminal, but was easily able to create the file and then customize the command line in just minutes following your explanation and steps. Well done!

    Now I’m thirsting to learn more so I can make the line do exactly what I want and use what I get.

    Where do I go to learn, for example:
    what are the other file characteristics being written by ls -la along with file/folder name and mod date?
    how can I write to a tab delimited file or csv that will open parsed into columns by Numbers?
    Can I drill down to subfolders with one command?

    Whether you have time to answer, I still say THANK YOU!

  6. Joseph Brown says:

    Copy / paste method does not work.

    • Paul says:

      It does work.

      The copy/paste method for listing files works fine.

      Be sure you paste the list into a file that is plain text format, like in TextEdit, BBEdit, TextWrangler, etc. If it is not plain text it will attempt rich text pasting with the documents instead.

  7. John says:

    Few years late on this question, but couldn’t hurt – can I use this method or a similar method to get info on file sizes for a large batch of folders? I’m talking over 300.

  8. daniel says:

    Thank u very much!

  9. Ali Jose says:

    gr8!! thanks it helped me save a lot of time at my work

  10. Troy says:

    What flag do you use with the ls command to arrange by file type?

  11. h00jmiwoko says:

    This looks like advanced hacker stuff! Will it break my Mac?

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

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

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site