Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Jul 7 '11 at 18:48

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

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

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

I think the cleanest solution would be to use cp:

cp -r source/. dest/

The extra . is the key here!

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .