I am trying to copy the contents of a folder to another folder in a different directory using terminal.

Would somebody be able to provide me an example of the command line syntax required to achieve this?

You can copy the content of a folder /source to another existing folder /dest with the command

cp -a /source/. /dest/

The -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

The . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.

  • 1
    Great answer. I will write a real example in case someone is having trouble with this command: sudo cp -a /var/www/opencart/. /var/www/opencart_local/ – pablofiumara Nov 11 '13 at 15:29
  • @enzotib I am trying to use this command to copy the contents of "Downloads/textext" to "~/.config/inkscape/extensions/." Using your command I type "cp -a /Downloads/textext/. /~/.config/inkscape/extensions/." but this does not work – Funzies Feb 5 '14 at 11:09
  • @Funzies: probably your command should be: cp -a ~/Downloads/textext/. ~/.config/inkscape/extensions/ – enzotib Feb 6 '14 at 7:58
  • Add -p flag as to preserve the file permissions and timestamps. cp -ap /var/www/original.com/images/. /var/www/new.com/images/ – Dylan Valade Dec 17 '14 at 2:31
  • 2
    @DylanValade: -a already implies --preserve=all, that is wider than -p = --preserve=mode,ownership,timestamps. – enzotib Dec 22 '14 at 16:22

An alternate is rsync

rsync -r source/ destination

The advantages of rsync are:

  1. After the initial sync, it will then copy only the files that have changed.

  2. You can use it over a network, convenient for files in $HOME, especially config files.

  • 5
    I think you don't need the asterisk. rsync -r source/ destination should be enough, no? – Joschua Dec 17 '15 at 15:10
  • 1
    This one is more appropriate: 'rsync -rtvp source/* destination' – shridutt kothari Sep 16 '16 at 14:14
  • -r not works for hidden files/folders – Nam G VU Sep 21 '16 at 4:42
  • 1
    This will not copy hidden files, since bash expands * only to non-hidden file. The solution by @Joschua is safer. – sauerburger Mar 9 '17 at 23:00
  • rsync will copy the entire directory, including hidden files, if you use the trailing / without a * so rync source/ dest Obviously people will need to customize the rsync command to function as they wish. – Panther Mar 10 '17 at 16:40

Lets say you have a folder called folder1 in your ~, inside folder1 is 1 file called file1 and 2 folders called sub1 and sub2 each with other files and folders inside them.

To copy all the contents of ~/folder1 to ~/new_folder1 you would use

cp -r ~/folder1/. ~/new_folder1

new_folder1 would then contain all the files and folders from folder1.

cp is the command to copy using a terminal, -r makes it recursively (so, current directory + further directories inside current) ~/folder1 is the origin folder, ~/new_folder1 is the destination folder for the files/folders inside the origin.

  • 4
    it does not catch hidden files – Portablejim Dec 11 '11 at 13:04
  • 1
    Thank you Bruno! It helped me to understand the syntax, though I had to change it a bit(removing ~ sign). Maybe because the destination folder was in /opt, which resides in another file system. And thank you Portablejim to remember the hidden file thing! – pandisvezia Dec 11 '11 at 15:51
  • 3
    The trailing period is important. Without it, sometimes it may create a new subdirectory ~/new_folder1/folder1 instead of copying the contents over. – wisbucky Jan 19 '15 at 23:00
  • Why not cp -r ~/folder1/* ~/new_folder1 – Alex78191 Jul 24 at 10:59
  • @Alex78191 [root@ home]# mkdir food [root@ home]# cd food/ [root@ food]# mkdir .fruit [root@ food]# mkdir veggies [root@ food]# touch veggies/carrots [root@ food]# touch .fruit/apple [root@ food]# ls * carrots [root@ food]# – Bruno Pereira Jul 24 at 11:19

Check this http://www.cyberciti.biz/faq/copy-folder-linux-command-line/ for more information on copying folder. Hope this helps.

cp Command

cp is a Linux command for copying files and directories. The syntax is as follows:

cp source destination
cp dir1 dir2
cp -option  source destination
cp -option1 -option2  source destination

In this example copy /home/vivek/letters folder and all its files to /usb/backup directory:

cp -avr /home/vivek/letters /usb/backup

Where,

-a : Preserve the specified attributes such as directory an file mode, ownership, timestamps, if possible additional attributes: context, links, xattr, all.

-v : Explain what is being done.

-r : Copy directories recursively. Example

Copy a folder called /tmp/conf to /tmp/backup:

$ cp -avr /tmp/conf/ /tmp/backup
  • 3
    This isn't what the question asks. – cybermonkey Mar 8 '15 at 13:18
  • (Using -r with -a is redundant; on gnu cp -a is the same as "-dr --preserve=all") – cristoper Feb 14 at 16:35

If there are two folders: (with write permission)

drwxr-xr-x 4 vimal vimal  4096 Sep  9 12:17 .
drwxr-xr-x 3 root  root   4096 Aug 18 14:35 ..
drwxrwxrwx 6 vimal vimal  4096 Sep  9 12:15 DATA
drwxrwxrwx 7 vimal vimal  4096 Sep  9 12:15 PORTAL

If you are inside the folder called PORTAL where you want to copy all content of another folder say DATA at the same level then you will do

vimal@vimal-D3H:/var/www/html/PORTAL$ cp -a ../DATA/. .

You have to notice 2 dots. Last dot says copy here in present folder

and

one following /DATA/. says that all the CONTENTS inside DATA folder to be copied, and not the DATA folder itself.

If you remove this trailing "." from /DATA/

then whole DATA folder will be copied inside PORTAL(from where you are coping).

protected by kos Apr 22 '16 at 11:11

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.