Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to copy a file "test.sh" in directories which have all a subdirectory 2_bas (ex. prod1/2_bas, prod2/2_bas, etc...)

I tried:

cp test */2_bas/

but I get the error message:

cp: omitting directory `prod1/2_bas'
cp: omitting directory `prod2/2_bas' 

A solution would be:

for dir in *; do [ -d "$dir" ] && cp test.sh "$dir"/2_bas ; done

But why does the first strategy not work?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

It's how the cp command is designed to work; it accepts multiple sources but only one destination. So your expansion looks like:

cp test prod1/2_bas prod2/2_bas prod3/2_bas .... prodN/2_bas

and what you're telling cp is to essentially copy everything into prodN/2bas (the last directory in the expansion). This is certainly not what you want. The error message you see is because you're telling cp to copy directories, and by default it refuses to do so (unless you specify, for instance, -r for recursive copying. Don't do it, though; it will still not do what you want).

Have a look at cp man page, it states what the command does:

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

Another solution:

find ./ -type -d exec cp test {} \;
share|improve this answer
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.