Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
.
|
|__a
|  |
|  |__b
|     | 
|     |__1.htm   
|
|__c
k@l:/$ sudo cp -r /a/b /c/d
k@l:/$ cd /c
k@l:/c$ ls
d
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

2 Answers 2

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

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

share|improve this answer

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.