Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

Here's the problem I'm attempting to solve:

  • Let's say I have a directory "A", containing some files as well as some other directories.
  • I want to copy all the files directly under directory A to directory B.
  • I want to recursively copy all the folders inside folder A to folder C.

What is the shortest and less platform-specific way to accomplish this in UNIX/Linux?

share|improve this question
up vote 5 down vote accepted

Probably something like this

find A -type f -maxdepth 1 -exec cp {} B/ \;


find A -type d -maxdepth 1 -mindepth 1 -exec cp -r {} C/ \;

Where -type is a flag, determining the type you're looking for (file or directory), - maxdepth how deep into directory, and -exec for executing a command on the result.

share|improve this answer
What is the necessity for -name '*'? – enzotib Mar 4 '12 at 8:38
@enzotib Good point, I adjusted the answer. – Bernhard Mar 4 '12 at 8:52
Would use find ... \; for less platform specific. – forcefsck Mar 4 '12 at 9:09
Nice, but the second part also copies the entire A directory into C (in addition to the subdirectories). How would I remove that? – user2398029 Mar 4 '12 at 18:52
@louism The mindepth flag should prevent this. – Bernhard Mar 4 '12 at 19:17

You could use cp to copy the directories, giving a glob pattern ending in /,

cp -a A/*/ C/

and you can copy files without the -a (similar to -r) option

cp A/* B/

though this last command would give harmless errors on directories not been copied.

share|improve this answer
Would use -R instead of -a for less platform specific. – forcefsck Mar 4 '12 at 9:08

Your Answer


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.