Wednesday, February 11, 2015

Flatten a Nested Directory & File Hierarchy from Command Line of OS X http://goo.gl/7Z9kZP



Nested directory structure to flatten as shown in the Finder of Mac OS X

Terminal in OS X Have you ever needed to flatten a directory structure, moving all file contents from a directories child folders into a single folder? While you can do this manually by moving around files and folders from the file system of OS X or Linux, a faster option is to turn to the command line. Maybe at one point you created a nested hierarchy of directories that you now need to undo by moving all files out of those nested folders and back into a single directory, or maybe you’re looking to simplify a directory structure, whatever the reason, this trick works quite well.




Using the command line to accomplish flattening of files and directory structures is obviously best reserved for advanced users who are comfortable with using terminal in general, if that doesn’t describe you, consider doing it manually through Finder, or using the Mac Automator app to accomplish similar automation of file system activities. We’re focusing on directory flattening from the command line here, however.





Example of Flattening a Nested File Directory


To better understand what we’re trying to accomplish, let’s take an example imaginary directory structure called TestDirectory located in a user Home folder. In this example, TestDirectory contains subfolders like SubDirectory1, SubDirectory2, SubDirectory3, etc, each with files in those respective folders. What we’re looking to do here is flatten the directory structure, moving all files from SubDirectory(X) to the parent directory “TestDirectory”. The initial directory and contents shown recursively with the could look something like this:


$ find ~/TestDirectory/ -type f

~/TestDirectory/rooty.jpg

~/TestDirectory/SampleDirectory1/beta-tool-preview.jpg

~/TestDirectory/SampleDirectory1/alphabeta-tool.jpg

~/TestDirectory/SampleDirectory2/test-tools.jpg

~/TestDirectory/SampleDirectory3/test-png.jpg

~/TestDirectory/SampleDirectory3/test1.jpg

~/TestDirectory/SampleDirectory3/test2.jpg


To flatten this directory and subdirectory contents out back into the TestDirectory folder, you would use the following command string:


find TargetDirectory/ -mindepth 2 -type f -exec mv -i '' TargetDirectory/ ';'


After the directory contents have been flattened, it should look like this when listed out:


~/TestDirectory/rooty.jpg

~/TestDirectory/beta-tool-preview.jpg

~/TestDirectory/alphabeta-tool.jpg

~/TestDirectory/test-tools.jpg

~/TestDirectory/test-png.jpg

~/TestDirectory/test1.jpg

~/TestDirectory/test2.jpg


Note the subdirectories will still exist, they’ll just be empty. Make sense? If not, or if that doesn’t demonstrate what you want to accomplish, you probably don’t want to flatten a directory at all, maybe you’re looking to merge or use ditto to do a complex copy to elsewhere.


Flattening a Directory Structure & Nested File Hierarchy with the Command Line


Ready to proceed? The command string we’re going to use to flatten a directory structure and move all files from subdirectories to the base of the target directory is as follows:


find [DIRECTORY] -mindepth 2 -type f -exec mv -i '' [DIRECTORY] ';'


Replace [DIRECTORY] with the directory of your choice to flatten, as demonstrated in the example above.


Yes, the directory appears twice in the command string, the first time is the directory being searched to flatten subdirectories of, and the second time as the destination for the found items.


Be precise with the specified destination, because this is not reversible (well, at least without a lot of manual work on your part), so only do this if you’re absolutely sure you want to relocate all the files in the target directories child directories back to the target root folder.


As mentioned before, you could also do this in the Finder of OS X, or at least observe the file and folder changes in the Finder. Option+clicking the little arrows in the List view opens up all subdirectories, showing the folder hierarchy like this:


Nested directory structure to flatten as shown in the Finder of Mac OS X


After fiddling with a variety of bash and zsh alternatives, this handy trick was left by a commenter on StackExcange and it ended up being the easiest and most compatible method. If you know of a better way to flatten a nested directory, do let us know in the comments!




Source Article from http://feedproxy.google.com/~r/osxdaily/~3/_Ze8KST5dWM/ http://revealedtech.com/wp-content/uploads/2015/02/nested-directory-structure-to-flatten-610x400.jpg
Flatten a Nested Directory & File Hierarchy from Command Line of OS X

No comments:

Post a Comment