134

I want to copy a directory from one place to another folder.

sudo cp is the command, but after that what should I type? The destination or source first?

1
  • 11
    Does man cp help? Nov 17, 2011 at 4:43

4 Answers 4

207

The -a flag is probably what you are looking for:

cp -a /path/from /path/to

The -a flag turns on recursive behaviour (which can also be done with the -R flag), and will also attempt to preserve metadata such as file ownership, permissions, timestamps, links, etc.

You should only need to use sudo if you are copying to a location not owned by the current user, if the current user doesn't have read permissions for the files being copied, or if you want to preserve ownership on files not owned by the current user.

6
  • 4
    in addition, as a rule of thumb, its always like that [command] [source] [destination]. This applies to mv, cp, ren. Nov 17, 2011 at 5:19
  • 1
    @VineetMenon You probably mean mv, not ren.
    – jcollado
    Nov 17, 2011 at 7:36
  • @jcollado:i have already added mv!! Nov 17, 2011 at 8:49
  • @VineetMenon Yes, you're right, I didn't see that mv was already in the list. Anyway, my point was that, as far as I know, there isn't any ren command (unless you've got an alias or something set).
    – jcollado
    Nov 17, 2011 at 9:28
  • 1
    @jcollado:haha...that was my alias for rename... :D Nov 17, 2011 at 9:29
28

If you want to copy directory please use below command:

sudo cp -R Source_Folder Destination_Folder

This command can also be used to copy files, by just removing the "-R" which is used to copy the recursive structure of internal folders (if there are any in the Source_Folder path that we mentioned.)

One more example:

sudo cp -R /var/www/* /home/test_user/

Please feel free to leave a comment in case of any issue.

1
  • Why does user1@server: sudo cp -r /home/user1/foo/* /home/user2/foo/ return permission denied when I added it to sudoers file? I am so confused.. visudo: user1 ALL=NOPASSWD: /bin/cp -r /home/user1/foo/* /home/user2/foo/?
    – Ismoh
    Jul 19 at 22:50
8

For copy folder via terminal, you can use:

cp -a /source/. /dest/

The -a option is an improved recursive option. It preserves all file attributes and also preserves symlinks.

The . at the end of the source path is a specific cp syntax that allows copying all files and folders, including hidden ones.

An alternative is rsync:

rsync -r source/ destination
2

I think the better way is to use gksu nautilus command and you can copy whatever you want with common GUI as usual.

1
  • 2
    That assumes you are using a GUI and not CLI. Jul 8, 2016 at 14:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .