Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question was also asked here.

When I copy some directory (dir) recursively with sudo in bash it copies just the first level of the dir hierarchy, but when used without sudo it copies the dir with all its subdirs, though commands in command-line have the difference only in the presence of sudo.

Why does this happen?

For example:

k@l:/$ tree
|  |
|  |__b
|     | 
|     |__1.htm   
k@l:/$ sudo cp -r /a/b /c/d
k@l:/$ cd /c
k@l:/c$ ls
k@l:/c$ cd d
bash: cd: /c/d: Permission denied
k@l:/c$ tree
|__d [error opening dir]

1 directory, 0 files
share|improve this question
There are error messages on your listing. You perhaps want to learn their meaning first. – n.m. Jul 26 '11 at 5:11
up vote 2 down vote accepted

When you do sudo cp -r /a/b /c/d the folder are copied to /c/d/ as root with root as the owner, therefore you get a Permission Denied since you need root permission to open the directory, try opening /c/d/ in a Root Terminal; alternatively, you need to alter the file owner or fix the permission by using chown/chmod so your current user will be able to access the folder.

Note: sudo cd won't work since cd is a shell command, not programs.

share|improve this answer

Maybe /c/d had permissions 700 with a different owner. Try sudo chmod -R 755 /c/d.

locked by Community Sep 18 '14 at 13:22
+1: Or you could try 'sudo chown -R me /c/d'. – Jonathan Leffler Jul 26 '11 at 6:50
Yup, that'd work too. – jman Jul 26 '11 at 7:02

I'd rather use cp -a instead of cp -r. The latter might not preserve some file attributes.

share|improve this answer

Instead of changing the cp command, you could run sudo as a specific user doing something like

sudo -u thisUser cp -r /a/b /c/d

Nevertheless, this will work only if thisUser has permissions for /a/b

share|improve this answer

You must log in to answer this question.

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