Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 facebook.stackoverflow.com Sep 19 '11 at 17:53

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

3 Answers 3

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:

./.?/dx2a
./.?/dx4a
./.tdot1/dx1a
./.tdot2/dx2a
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.