fast way to flatten a folder, anyone?

Discussion in 'OS X Mavericks (10.9)' started by Brutalbrutus, Feb 26, 2015.

  1. Brutalbrutus macrumors newbie


    Nov 24, 2011
    Hello. I have a question about organizing folders (directories) and the files within them. I'm sure that if I were better at this stuff I'd be able to dash off an Automator script to accomplish what I want in just a few moments; doing this manually is driving me nuts; maybe one of you guys/gals can help me.

    I have a big folder called EBooks on my desktop. Inside EBooks are two different kinds of objects: First, there are individual files that are electronic books, such as .epub files and .pdf files. There are, say, forty of those. Then there are folders that, in turn, themselves contain individual electronic book files (.epub and/or .pdf files). These folders sometimes also contain other stuff--README files, tables of contents, etc. Usually the biggest files contained in these folders are the electronic book files themselves. Sometimes, however, these folders nest within themselves recursively up to, say, four layers deep. What I mean by this is that, say, inside

    is a folder called Poe, Edgar Allan; and inside that folder are
    Poe Poetry and Poe Prose and Poe Bio; and inside those folders are
    even further folders until we get down to the level of individual .epubs or .pdfs. As an example, assume that inside Poe Prose (the folder) there are the files TelltaleHeart.epub and Amontillado.pdf.

    Complicating this situation, there is a *duplicate* of the TelltaleHeart.epub file that I just mentioned lying directly inside EBooks, only one layer down! :(

    I apologize, patient reader, but I just don't have the language to describe this situation concisely. Please now imagine that inside the folder EBooks there are many items with exactly the same names, but some are folders, while others are .epub or .pdf files! So there is, for example, an FScottFitzgerald folder that contains lots of .epubs, but at the same time there is an FScottFitzgerald.epub file that comprises several short stories all mushed together into one .epub file.

    What I would like to do is create a FLAT file hierarchy inside EBooks. Except where absolutely necessary, I would like EBooks to contain a set of folders, each bearing one author's name. Inside each of those folders one could find all of the document/file/epubs I own written by that particular writer.

    Is there a relatively easy way to bring about this state of affairs?

    Thanks SO MUCH for your anticipated help!

  2. kaibob macrumors regular

    Jun 21, 2010
    Prescott, Arizona
    I don't believe Automator will do what you want. Probably the best approach is a Bash script that utilizes the find command to cycle through all files in the eBooks directory and subdirectories. The script would then create necessary directories, check for duplicate files, and move the files into the appropriate directory. This is not difficult but does take some time to write and debug.

    If you want to pursue this, I suggest that you ask a moderator to move this thread to the Mac Programming forum. Hopefully, someone there, who is current on their Bash skills, will be able to help.

    To directly answer your question, I don't believe there is a really easy way to do what you want. Hopefully, another forum member will prove me wrong.

    Just as an aside, the Bash find command has a -type option that is able to distinguish between directories and files, so this is not a problem.
  3. navier macrumors regular


    Aug 4, 2011
    Posted a week ago on Lifehacker (via Twitter)

    "@lifehacker: Quickly flatten a directory and move all the subfolders into one main folder on OS X with this Terminal command:"


Share This Page