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 did cp -r on a folder (copy), then removed the original. I have a backup with most of the files. How do I restore the permissions of the files & folders on the copy using the backup?

It would be very tedious to go in and do chmod XXX filename every single item in there.

share|improve this question
    
Related. –  Eliah Kagan Jul 27 '12 at 2:55
2  
So next time, use rsync -a or cp -a, whichever's available, to have the permissions preserved in the first place ;) –  geirha Jul 27 '12 at 7:19
add comment

3 Answers 3

You can use stat to query the mode on your backup file, and chmod to update your working files. Do this in a loop, with a little string manipulation, and you have a solution. Something like this should work to copy the permissions:

# path to the top level of the backup
backup_dir=/path/to/backup/dir

# path to the directory of files that you want to restore
# permissions on.
dir=/path/to/working/dir

find "$backup_dir" |
while read backup_file
do
    new_file="$dir${backup_file#$backup_dir}"
    chmod --reference="$backup_file" "$new_file"
done

Note that this won't work well if your filenames contain the newline character (but thats's pretty rare). Also, it's untested, so you may want to do a "dry-run" by changing 'chmod' to 'echo chmod', so it just prints what it would do.

share|improve this answer
    
GNU chmod has a --reference option which would simplify that bit. chmod --reference="$backup_file" "$new_file". –  geirha Jul 27 '12 at 7:18
    
@geirha: nice! updated. –  Jeremy Kerr Jul 27 '12 at 7:34
add comment

What backup system are you using? If it is Ubuntu backup, you can restore the original using nautilus. Right-click in the parent folder and choose "Restore missing files" from the menu.

share|improve this answer
add comment

use cp -a or more generally cp --preserve=all

from the man page

       --preserve[=ATTR_LIST]
              preserve  the  specified  attributes   (default:   mode,owner‐
              ship,timestamps),  if possible additional attributes: context,
              links, xattr, all

chmod also has a recursive (-R) option.

share|improve this answer
    
This will copy files while retaining their ownership, permissions, and other attributes. But this question is about how to copy just the attributes (or at least the permissions); that is, how to apply them to files that have already been copied with some method that didn't preserve them in the destination. –  Eliah Kagan Aug 7 '12 at 20:04
add comment

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.