By Matt Butcher
unix
Linux/UNIX/OS X: How to find and combine multiple files
Submitted by matt on Fri, 2010-04-16 14:26This explains how to use a UNIX-like command line (including Linux and OS X) and the find command to search through a subdirectory and find all of the files with a certain extension, and then combine those all into one file. Surprisingly, this isn't a difficult task. It can be accomplished with one command on the command line:
$ find ./src -name '*.txt' -exec cat '{}' \; > test.txtThe above looks through everything in the ./src directory (including all subdirectories) for any files with the .txt extension. Each file it finds, it adds to test.txt. So at the end of the command's run, all of the text files will be combined together into text.txt. You can use this strategy to easily combine lots of files into one.
Using find, it's easy to customize the command above to do all kinds of things with files. I gave a few examples in an earlier post about the UNIX find command.
Using the UNIX find Command
Submitted by matt on Mon, 2009-08-03 19:48Today I needed to run a simple script against thousands of identically named files nested in a huge directory structure. What I needed was a quick way to recurse through all of the directories, ignore all of the files I didn't care about, but run a specified command on any files with a particular name.
While this sounds like the sort of thing that will require a couple dozen lines of shell scripting, it can actually be accomplished on a single line with the command find. find has been around for decades, and can be found on almost any UNIX-like file system. It is a simple to used for searching for files within the UNIX directory hierarchy. Honestly, there's not much more to it. Like all good UNIX tools, it does one thing well.








