Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

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
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

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

# path to the directory of files that you want to restore
# permissions on.

find "$backup_dir" |
while read backup_file
    chmod --reference="$backup_file" "$new_file"

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

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

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

from the man page

              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

Your Answer


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.