Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a directory ~/dir that contains a bunch of random folders like: ~/dir/av 801 and ~/dir/lm 320. I want to copy the contents of every inner folder (ie: av 801) into a different directory. The contents of that folder can consist of folders or files.

This is what I guessed the bash command would be:

cp ~/dir/*/* ~/target/

But it gives this error:

cp: when copying multiple files, last argument must be a directory

Is there a bash command that can do such a thing?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

To copy directories, you need to tell cp to copy recursively by passing it the -r flag.

cp -R ~/dir/*/* ~/target/

If ~/target does not exist, you need to create it first.

mkdir ~/target
share|improve this answer
    
that worked, except I was using eshell and I had to use a capital R for some reason: cp -R ~/dir/*/* ~target/ –  Korey Hinton Jul 1 '13 at 21:52
    
Apparently -r is not portable while -R is.. I have modified my answer to reflect that. –  paraxor Jul 1 '13 at 21:58
    
Evan, did you mean to link to -r is not portable? –  Korey Hinton Jul 1 '13 at 22:05
    
Yes. I pasted the wrong link. –  paraxor Jul 1 '13 at 22:09
    
Okay, thanks for your help! –  Korey Hinton Jul 1 '13 at 22:14

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.