Is there a better way on the command line to essentially accomplish the following but with a single command

cp -r css/ ar/
cp -r images/ ar/
cp -r js/ ar/
cp -r backups/ ar/

I've just been stringing them together with a semicolon.

migrated from serverfault.com Mar 19 '13 at 15:18

This question came from our site for system and network administrators.

up vote 49 down vote accepted

Copying folders into another folder (folder in folder):

cp -r css images js backups ar/

Note: this is different from copying just the contents themselves(contents of folders in folder):

cp -r css/ images/ js/ backups/ ar/
  • 3
    the slashes make a difference! always suspected, never knew – Peter Perháč Oct 31 '17 at 9:36
  • 1
    slashes don't make a difference in my testing. cp is not rsync. however cp -r css/. images/. ar/ does copy the contents without the directories. my cp version is "cp (GNU coreutils) 8.29". – sourcejedi Jun 9 at 11:13

The answer is simple, and it works on Debian "this is actually how i did it" (tested) lets say you're in /etc and want to copy some files and folders before you format your hard disk (for some reason) you'd type the following and everything mentioned below will be copied to /mybackups/ folder:

sudo cp -r -a apt/ bash.bashrc environment hosts init.d/ init/ nginx/ proxychains.conf sudoers sysctl.d/ profile.d/ grub.d/ /boot/grub/ /opt/ /root/ /var/www/ /mybackups/

as you can see above, i tried to copy folders from outside etc as well by pointing to its location like /boot/grub/ or /opt/ and it worked!

  • 1
    I believe -r there is not needed, as -a already contains it (via: man). – David Potočnik Sep 11 at 18:08

You can't copy two directories using cp command alone.

You can copy recursively using -r option though.

For example:

cp -r <source> <destination> 

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.