2

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.

2
  • Related. Jul 27, 2012 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, 2012 at 7:19

3 Answers 3

2

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.

2
  • GNU chmod has a --reference option which would simplify that bit. chmod --reference="$backup_file" "$new_file".
    – geirha
    Jul 27, 2012 at 7:18
  • @geirha: nice! updated. Jul 27, 2012 at 7:34
0

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.

0

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.

1
  • 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. Aug 7, 2012 at 20:04

You must log in to answer this question.

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