Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
    
love your humor :) –  user8290 Dec 11 '11 at 3:01
1  
tried to be specific and clear :) –  pandisvezia Dec 11 '11 at 16:01
    
Oh, I am so sorry: I misunderstood and thought it was asked by Bruno! Apologies. –  user8290 Dec 11 '11 at 16:08
    
ahahahaha you're welcome –  pandisvezia Dec 11 '11 at 16:28
1  
@Christopher the shame! (no, honestly, no question is so silly that does not need to be asked at least one time;)) –  Bruno Pereira Dec 11 '11 at 18:15

3 Answers 3

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.

share|improve this answer
1  
it does not catch hidden files –  Portablejim Dec 11 '11 at 13:04
    
Fixed, should work now. –  Bruno Pereira Dec 11 '11 at 14:10
    
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

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.

share|improve this answer
1  
Thank you enzotib! It seems another useful syntax for copy operations. –  pandisvezia Dec 11 '11 at 15:52
    
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 at 11:09
    
@Funzies: probably your command should be: cp -a ~/Downloads/textext/. ~/.config/inkscape/extensions/ –  enzotib Feb 6 at 7:58

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.

share|improve this answer
    
Thank you for the alternative! –  pandisvezia Dec 11 '11 at 15:58
    
thank you very much!! –  thecodeparadox Nov 1 '12 at 15:39

Your Answer

 
discard

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.