53

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.

6 Answers 6

96

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/
5
  • 7
    the slashes make a difference! always suspected, never knew Oct 31, 2017 at 9:36
  • 2
    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, 2018 at 11:13
  • My god... thank you for that nuance. I was wasting a lot of time trying to figure that out.
    – 2upmedia
    Apr 15, 2020 at 19:58
  • 1
    also, just to add cp -rv * ar/ Will copy every folder, if the assumption is that you just simply want everything
    – the_5imian
    Sep 25, 2021 at 3:27
  • @the_5imian How would you copy the contents of every folder using * Jul 22 at 19:34
8

I am on Ubuntu 18.04.3 and use the following command to copy multiple folders in one directory:

cp -r /source_directory/{folder1,folder2,folder3} /destination

Equals

cp -r /source_directory/folder1 /destination
cp -r /source_directory/folder2 /destination
cp -r /source_directory/folder3 /destination

I think it's a very clean method.

0

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
  • 1
    I believe -r there is not needed, as -a already contains it (via: man). Sep 11, 2018 at 18:08
-1

if you want to copy all the folder's in a directory into another folder.

cp -r * /anotherfolder/

if you want to copy some files or folders.

cp -r folder1 folder2 folder3 /anotherfolder
-2

cp -r css images js backups ar/ should work, Note that in order to copy a directory on Linux, you have to execute the cp command with the r recursive option.

1
-3

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

You can copy recursively using -r option though.

For example:

cp -r <source> <destination> 

You must log in to answer this question.