<p>On the UNIX bash shell (specifically Mac OS X Leopard) what would be the simplest way to copy every file having a specific extension from a folder hierarchy (including subdirectories) to the same destination folder (without subfolders)?</p>

<p>Obviously there is the problem of having duplicates in the source hierarchy. I wouldn't mind if they are overwritten.</p>

<p>Example: I need to copy every .txt file in the following hierarchy</p>

<pre><code>/foo/a.txt
/foo/x.jpg
/foo/bar/a.txt
/foo/bar/c.jpg
/foo/bar/b.txt
</code></pre>

<p>To a folder named 'dest' and get:</p>

<pre><code>/dest/a.txt
/dest/b.txt
</code></pre>

In bash:
```
```
find /foo -iname '*.txt' -exec cp \{\} /dest/ \;
```
```
```
find
```
will find all the files under the path
```
/foo
```
matching the wildcard
```
*.txt
```
, case insensitively (That's what
```
-iname
```
means). For each file,
```
find
```
will execute
```
cp {} /dest/
```
, with the found file in place of
```
{}
```
.