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 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.)

share|improve this question

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

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

add comment

4 Answers

I think the cleanest solution would be to use cp:

cp -r source/. dest/

The extra . is the key here!

share|improve this answer
add comment
cp source/* target

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

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

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.

share|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
add comment

Use cp * target from inside the directory.

Check out SuperUser for other questions like this!

share|improve this answer
    
Note that this will miss any dotfiles in the directory. –  Michael Mior Jul 7 '11 at 18:03
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.