How to extract the names of folders to a list?
How can I extract the names of folders (e.g. in Documents) to a list in whatever readable format, e.g. text file, excel file or word file)?
MacBook Pro, OS X Mavericks (10.9.2), 8GB 1333 MHz DDR3
How can I extract the names of folders (e.g. in Documents) to a list in whatever readable format, e.g. text file, excel file or word file)?
MacBook Pro, OS X Mavericks (10.9.2), 8GB 1333 MHz DDR3
Open Applications/Utilities/Terminal. Type or paste in this line at the prompt:
ls -d /Users/xxx/Documents/*/
(the first character is lower-case L) substituting the name of your Home Folder for xxx
Hit return. The output looks like this:
/Users/xxx/Documents/Acrobat User Data/
/Users/xxx/Documents/Address Book backup/
/Users/xxx/Documents/AdobeStockPhotos/
/Users/xxx/Documents/Amazon MP3/
/Users/xxx/Documents/AppleWorks User Data/
/Users/xxx/Documents/BUDGET/
and so on. You can copy this into TextEdit and if required use Find and replace to remove /Users/xxx/Documents/
N.B.: don't get creative with Terminal; enter only exactly what is above. You can do a lot of damage with the wrong entries.
Great! Can I also include some attributes, say, size, date created, comments, tags...?
I don't know of a way of doing this, though of course there may well be one. This page lists the relevant commands:
https://www.mkssoftware.com/docs/man1/ls.1.asp
I tried the -s -k commands but it returned 0 at the beginning of each line so may only apply to files.
There used to be an application called 'List folders' but I have a feeling it was PPC only. However it might be worth searching to see whether anything similar is available.
Roger, thanks for sharing.
Continue to look forward to an answer...
To get filenames into a document...
Open Textedit with new doc.
Then menu Format > Make plain text.
In the Finder, select all (Command A) then copy (Command C) and paste into the Textedit doc.
Thank you Chris.
I still want a solution to Extract a list of folders (and / or files, ideally) with size, date created and comments fields in a table format.
(I spent a lot of time adding comments to some folders and files. I don't want to re-type them into a document.)
Here is a Bash script that is run from the Terminal. It retrieves a hierarchical, sorted list of folders in your Documents folder. For each folder in this hierarchy, it obtains (and prints) the following to a specified report file.
A report is written to the Desktop in comma separated values (CSV) text format so you can pull it into Excel as data.
#!/bin/bash
#
REPORT="$HOME/Desktop/Folder.txt"
DIR="$HOME/Documents"
while read -r dpath
do
# Get these attributes from each folder in the retrieval
DIRNAME="${dpath}"
DIRSIZE=$(/usr/bin/** -sh "${dpath}" | cut -f 1)
DIRCREATE=$(mdls -name kMDItemContentCreationDate "${dpath}" | \
egrep -o "\s+(\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2})")
DIRCOMMENT=$(mdls -name kMDItemFinderComment "${dpath}" | \
egrep -o '"(.+)"')
DIRTAGS=$(mdls -name kMDItemUserTags "{${dpath}" | egrep -o '"(.+)"')
[[ DIRCOMMENT == "(null)" ]] || DIRCOMMENT="None"
[[ DIRTAGS == "(null)" ]] || DIRTAGS="None"
printf "%s,%s,%s,%s,%s\n" "${DIRNAME}" $DIRSIZE "${DIRCREATE}" "${DIRCOMMENT}" "${DIRTAGS}" >> "${REPORT}"
# retrieve all folder hierarchy below specified location in dictionary sorted order
done < <(/usr/bin/find -s "${DIR}" -type d | egrep -v '[.]')
Copy and paste this into a programming editor, not a word processor, save it as dirget.sh, and then in the terminal, make it executable.
chmod +x dirget.sh
and run it as:
./dirget.sh
Here is a Bash script that is run from the Terminal. It retrieves a hierarchical, sorted list of folders in your Documents folder. For each folder in this hierarchy, it obtains (and reports) the following information:
A report is written to the Desktop in comma separated values (CSV) text format so you can pull it into Excel as data.
#!/bin/bash
#
REPORT="$HOME/Desktop/Folder.txt"
DIR="$HOME/Documents"
while read -r dpath
do
# Get these attributes from each folder in the retrieval
DIRNAME="${dpath}"
DIRSIZE=$(/usr/bin/** -sh "${dpath}" | cut -f 1)
DIRCREATE=$(mdls -name kMDItemContentCreationDate "${dpath}" | \
egrep -o "\s+(\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2})")
DIRCOMMENT=$(mdls -name kMDItemFinderComment "${dpath}" | \
egrep -o '"(.+)"')
DIRTAGS=$(mdls -name kMDItemUserTags "{${dpath}" | egrep -o '"(.+)"')
[[ DIRCOMMENT == "(null)" ]] || DIRCOMMENT="None"
[[ DIRTAGS == "(null)" ]] || DIRTAGS="None"
printf "%s,%s,%s,%s,%s\n" "${DIRNAME}" $DIRSIZE "${DIRCREATE}" "${DIRCOMMENT}" "${DIRTAGS}" >> "${REPORT}"
# retrieve all folder hierarchy below specified location in dictionary sorted order
done < <(/usr/bin/find -s "${DIR}" -type d | egrep -v '[.]')Ideally, you should copy/paste the script into a programmer's editor, and not a word processor that may add unwanted attributes. You will need to make the script executable in the Terminal app:
chmod +x dirget.sh
and run it as:
./dirget.sh
Well, my first post was apparently lost by the new hosting package and did not show up after a signout/sign-in cycle, so I posted again. I have since cleaned up the code, and added a dictionary-like lookup that reports the color of the tag used on a folder, if it is set.
Here is a Bash script revision that is run from the Terminal. It retrieves a hierarchical, sorted list of folders in your Documents folder. For each folder in this hierarchy, it obtains (and reports) the following information:
A report is written to the Desktop in comma separated values (CSV) text format so you can pull it into Excel as data.
#!/bin/bash
#
REPORT="$HOME/Desktop/Folder.txt"
DIR="$HOME/Documents"
export GREP_OPTIONS="--color=never --extended-regexp"
# default Finder Tags... colors
declare -a tagchroma=( ["1"]="Red" ["2"]="Yellow" ["3"]="Green" \
["4"]="Blue" ["5"]="Pink" ["6"]="Gray" )
while read -r dpath
do
# Get these attributes from each folder in the retrieval
dirname="${dpath}"
dirsize=$(/usr/bin/** -sh "${dirname}" | cut -f 1)
dircreate=$(mdls -name kMDItemContentCreationDate "${dirname}" | \
egrep -o "\s+(\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2})")
dircomment=$(mdls -name kMDItemFinderComment "${dirname}" | \
egrep -o '"(.+)"')
findercolor=$(mdls -name kMDItemFSFinderFlags "${dirname}")
# Expect color to be integer in range 1 - 6 as key for faux Dict lookup
[[ $findercolor ]] && tagcolor="${tagchroma["${findercolor}"]}"
dirtags=$(mdls -name kMDItemUserTags "${dirname}" | egrep -o '"(.+)"')
printf "%s,%s,%s,%s,%s,%s\n" "${dirname}" $dirsize "${dircreate}" "${dircomment:-None}" \
"${dirtags:-None}" "${tagcolor:-None}" >> "${REPORT}"
# retrieve all folder hierarchy below specified location in dictionary sorted order
done < <(/usr/bin/find "${DIR}" -type d | egrep -v '[.]' | sort -d)
unset tagchroma
exit 0
It's so good! Very appreciated. Thanks VikingOSX.
I walked through the lines and it wakes up my decades old memory writing scripts.... I tried to pick up the skill but need more help from you to set up the working bench please.
1. You mentioned using a programming editor. What is available on OS X Mavericks or free to download and use? I tried opening and paste your scripts to TextEdit and Apple Script Editor. They both said .sh cannot be saved, or can be saved both. Are these the programming editors in your mind?
2. What is the common / best practice to save the scripts (developing and final)? Should they under Documents, Applications or what?
3. When I opened TextEdit and Apple Script Editor, I discovered that I can save the script to ICloud. If I do so, can I just run the script from your instructions above? What things I need to care for?
4. I have mixed up some folders and files under Documents (or other folders I want to use this script). So I need to include the Kind as well. Can you help to include it? (I don't want to be lazy or dependent further. Where can I find a list of commands / attribute names I can use? (including Name, Date Modified, Date Created, Date Last Opened, Date Added, Size, Version, Kind, Comments, Tags... any more?) Is there a document from Apple developer resource?
Ivan,
Here is the revised script version. Notice some alternatives in the commented lines.
#!/bin/bash
# dirget.sh - traverse down from starting folder, finding subfolders.
# Creates CSV Report of directory name, directory size,
# directory creation date, directory and tag comments, if
# available.
# Author: VikingOSX, 2014-08-16, Apple Support Community
# Version: Final.
REPORT="$HOME/Desktop/Folder.txt"
DIR="$HOME/Documents"
#interactive version -- specify full folder path
#read -p "Enter the Starting Folder: " DIR
export GREP_OPTIONS="--color=never --extended-regexp"
while read -r dpath
do
# Get these attributes from each folder in the retrieval
dirname="${dpath}"
# just the directory name, not the path to it
#dirname="${dpath##*/}"
dirsize=$(/usr/bin/** -sh "${dirname}" | cut -f 1)
dircreate=$(mdls -name kMDItemContentCreationDate "${dirname}" | \
egrep -o "\s+(\d{4}-\d{2}-\d{2} \d{1,2}:\d{2}:\d{2})")
dircomment=$(mdls -name kMDItemFinderComment "${dirname}" | \
egrep -o '"(.+)"' | egrep '[^"]')
dirtags=$(mdls -name kMDItemUserTags "${dirname}" | egrep -o '"(.+)"')
printf "%s,%s,%s,%s,%s\n" "${dirname}" $dirsize "${dircreate}" "${dircomment:-None}" \
"${dirtags:-None}" >> "${REPORT}"
# retrieve all folder hierarchy below specified location in dictionary sorted order
done < <(/usr/bin/find "${DIR}" -type d | egrep -v '[.]' | sort -d)
# Alternate folder locator, but if run in home directory, will drill down into all
# subfolders (hidden and Library) -- so must specify explicit starting folder as onlyin
# argument.
#done < <(mdfind -onlyin "${DIR}" -name 'kMDItemKind == "Folder"cwd')
exit 0
Thank you very much for a very useful tutorial to me. I'm excited that it can be done so my thousands of pictures with comments can be categorised and searched! I just downloaded the TextWrangler. It will take me a day, if not days, to learn. It's so good. Thank you VikingOSX.
No time to read today.... printed anyway, so it won't be lost!
Thank you.
How to extract the names of folders to a list?