Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to copy all files with the name XYZ* into one folder. The problem is that the files are in different subfolders and that not even the depth of the folder structure is the same for all files. Luckily, at least each file has a unique name.

Of course, I thought about the cp command but I guess the depth of the folder structure needs to be the same for this to work.

share|improve this question

migrated from Sep 19 '11 at 17:53

This question came from our site for professional and enthusiast programmers.

If you want to avoid starting many separate processes (as using find ... -exec cp ... may do) using bash (and don't have zsh for its ** form) you can do a single cp as follows:

cp -p $(find path/to/src -name 'XYZ*') path/to/dest

The $(...) form is a posix version of back-tick-quoted process substitution. Following is an example.

find . -name 'd*a'
cp -p $(find . -name 'd*a') ../zeta
ls ../zeta

These commands produced the following output:

cp: will not overwrite just-created `../zeta/dx2a' with `./.tdot2/dx2a'
dx1a  dx2a  dx4a
share|improve this answer

You can use find for this:

$ find path/to/src -type f -name XYZ\* -exec cp -p {} path/todest/ \;
share|improve this answer
thanks, for helping me out here anyways. – Mike Sep 19 '11 at 16:31

If you use zsh instead of bash then you can do it with the ** file glob pattern. This recursively matches all subfolders.

cp path/to/src/**/XYZ* path/to/dest
share|improve this answer

You must log in to answer this question.