I would like to copy the contents of a directory into another. I don't want to copy the directory and all files and directories under it, but just the contents of the directory just as if it were a regular file.

Doing cp -r target dest copies the directory and the entire hierarchy rooted in it.

Can anyone please suggest a command to do that? I get error if I do not include the -r option.

(I am calling cp from within a C program.)

link|improve this question
feedback

migrated from stackoverflow.com Jul 7 '11 at 18:48

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

3 Answers

I am not sure to have understood what you want exactly, anyway something like

find  MyDir  -maxdepth 1 -type f -exec cp "{}" destdir \;

will copy all files from MyDir to destdir.

link|improve this answer
cp ./* destdir will work too, with extra output like "omitting directory" – ShinTakezou Jul 7 '11 at 18:03
1  
+1 for a solution which will catch dotfiles – Michael Mior Jul 7 '11 at 18:04
missed that! :D though now I remember why I do always find instead of cp!! – ShinTakezou Jul 7 '11 at 18:09
@Shin Thanks...I just want the contents of the directory to be copied and not any of the directory or file under it. Hope I am clear – Juggler Jul 8 '11 at 2:32
then it is what "find ..." would do – ShinTakezou Jul 8 '11 at 6:46
feedback

Use cp * target from inside the directory.

Check out SuperUser for other questions like this!

link|improve this answer
Note that this will miss any dotfiles in the directory. – Michael Mior Jul 7 '11 at 18:03
feedback
cp source/* target

This way it won't copy subdirectories that would need -r

link|improve this answer
not working. All directories and there contents are copied – Juggler Jul 7 '11 at 18:03
feedback

Your Answer

 
or
required, but never shown

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