Extract files recursively from child folders in the current directory
- Written by Dave on Saturday 6 August 2011
Yesterday I was rearranging my photo albums, and decided to get rid of my old folder structure. All files were located into several nested directories, and I needed to collect all .jpg files into one directory so I could start rearranging them into new albums in iPhoto. The find command-line tool can do this in a simple one-liner. I will show you the build-up:
find . -type f -name '*.jpg'
This will recursively search all folders in the active directory in order to return a list of matching file locations. -type f means we're looking for files. -name accepts a search query, which may contain wildcards.
We could now pipe the line above to work with the output, but find has built-in functionality in the form of the -exec parameter. So the end result will be:
find . -type f -name '*.jpg' -exec mv -fv '{}' '.' ';'
This loops all lines of the output and initiates a mv command on the active line in the loop ('{}'), and moves them to the specified destination folder ('.')