How to Create a Text File of Finder Folder and File Structure?

I found a Terminal command that can create a Tree.txt file with the filenames and folders, but it also shows hidden files and package contents.

find ~/Documents -print | sed -e 's;[^/]*/;|--->;g;s;--->|; |;g' > ~/Desktop/Tree.txt

Can somebody modify this command so that it only shows the filenames and folders that you would see in a typical finder window?

mdfind -onlyin ~/Documents \( "kMDItemFSName != '.*' || kMDItemContentTypeTree != 'com.apple.package'" \) | sort -d -f | sed -e 's;[^/]*/;|--->;g;s;--->|; |;g' > ~/Desktop/Tree.txt

Use this one instead. What this improved version does

1. mdfind -onlyin ~/Documents
Searches only inside the Documents folder using Spotlight.

2. Filter condition

kMDItemFSName != ‘.*’

Excludes hidden files.

kMDItemContentTypeTree != ‘com.apple.package’

Excludes package bundles (like .app, .pages, .numbers).

3. sort -d -f
Improved sorting:

  • -d → dictionary order (like Finder)
  • -f → case-insensitive

This makes the output closer to Finder’s display order.

4. sed formatting

sed -e ‘s;[^/]*/;|—>;g;s;—>|; |;g’

Converts full file paths into a tree-style visual structure.