Flattening a directory structure on Windows

The other day I needed to copy all the files within a hierarchical directory structure in a shared network directory into a single directory. Here’s how I did it.

1) install the following GnuWin32 utilities from http://gnuwin32.sourceforge.net/packages.html (this is much simpler than and add the bin directory (c:\Program Files\GnuWin32\bin) to your PATH environment variable.

  • CoreUtils
  • FindUtils
  • sed

2) Check the shared network directory for files with the same name, and either change names or delete files before copying. My shared network directory is mapped as I:\Share.

C:\>I:
I:\>cd I:\Share
I:\Share>"c:\Program Files\GnuWin32\bin\find.exe" . -type f | sed "s/.*\///" | sort | uniq -d

The full path to “find” is needed because, although the GnuWin32 bin directory is on my command path, the Windows “find” command is found on my path before the GnuWin32 “find”. This can take some time – 15 minutes on a 5 year old laptop with a shared directory having 170k files and 22K directories!

3) Copy the files into a new “files” directory on X-drive:

I:\Share>mkdir X:\files
I:\Share>cp `find . -type f` X:\files

Done!

P.S. Thanks to ldenneau for the idea (http://ask.metafilter.com/62308/Easy-Windows-directory-flattening-with-minimal-tools)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.