2

I've got a folder with about a hundred sub folders and again each ones of those has between 10 and 20 sub folders, so all in all a pretty large folder tree.

Is there a simple way I can explode or export all of the files in the tree to a new folder which will just be one folder contain the files (no folders, no trees)?

I'm running OS X 10.8, although I've also got Parallels so if there is a Windows solution I could just run that as it's not something I need to do everyday.

Oliver Salzburg
  • 89,072
  • 65
  • 269
  • 311
sam
  • 4,359

3 Answers3

5

In OS X, this should work:

find /top/source/directory -type f -exec mv {} /destination/directory \;

The find command searches all subdirectories of the folder /top/source/directory, and finds only the files (the option -type f). When it finds one it executes (-exec) the command mv on the file it just found ({}) to move it to the new directory, /destination/directory.

Please notice that the space before \; is absolutely necessary.

MariusMatutiae
  • 48,517
  • 12
  • 86
  • 136
0

This should work on the Windows side in PowerShell:

Get-ChildItem -Path "C:\Source" -Recurse -Include *.* | Move-Item -Destination "C:\Destination\"

Josh
  • 5,333
0

This answer is based on Python so it should work on OSX as well as on Windows, assuming you install Python (and for the record, is from the top of my head).

import os
import shutil

path = "C:/dir"    # the directory tree you want to "explode"
store= "C:/store"  # where all files will be stored

for dirpath, dirnames, filenames in os.walk(path):

    name = ""
    for c in dirpath:
        if c != "/":    # if the character is different than the current directory character
            name += s
        else:
            name += "_" # "quick and dirty" way of resolving name conflicts

    for files in filenames:

        orig_loc = os.path.join(dirpath,files)
        copy_loc = os.path.join(store,name + "_" + files)
        shutil.copy2(orig_loc,copy_loc)

This should copy all files (plus metadata) from the path structure to the store folder.

If you don't care about name conflicts, don't use the code between the name = "" and the for files in filenames statements. What that code does is to convert / to _ and afterwards prepend that cleared directory name to the files' names.

This will retain the original structure, that you can delete afterwards with shutil.rmtree(path)