Back up and restore file permissions
Solution 1
You can do this with the commands from the acl package (which should be available on all mainstream distributions, but might not be part of the base installation). They back up and restore ACL when ACL are present, but they also work for basic permissions even on systems that don't support ACL.
To back up permissions in the current directory and its subdirectories recursively:
getfacl -R . >permissions.facl
To restore permissions:
setfacl --restore=permissions.facl
Solution 2
I'm not aware of anything "off the shelf" that would do this. Here's a starter script for you, though, that will handle basic permissions. It does not handle ACLs of any description - but your Question explicitly excludes those. (It will also fail on pathological filenames - those starting with whitespace, or containing non-printable characters.)
Save the permissions
find * -depth -exec stat --format '%a %u %g %n' {} + >/tmp/save-the-list
Restore the permissions
while read PERMS OWNER GROUP FILE
do
chmod "$PERMS" "$FILE"
chown "${OWNER}:${GROUP}" "$FILE"
done </tmp/save-the-list
Related videos on Youtube
leeand00
Projects jobdb - Creator of Open Source Job Search Document Creator/Tracker http://i9.photobucket.com/albums/a58/Maskkkk/c64nMe.jpg Received my first computer (see above) at the age of 3, wrote my first program at the age of 7. Been hooked on programming ever since.
Updated on September 18, 2022Comments
-
leeand00 over 1 year
Is there a way to back up and restore file ownership and permissions (the things that can be changed with
chown
andchmod
)?You can do this in Windows using icacls.
What about access control lists?
-
Admin about 9 yearsIt would help if you stated which distro you are using, as different distros use different package managers.
-
Admin about 9 years@garethTheRed, does it also depend on the fs being used, or just the distro?
-
Admin about 9 yearsI doubt it would depend on the filesystem.
-
-
roaima about 9 yearsHmm. I really need to read up on ACLs.
-
leeand00 about 9 yearsIn the generated file, are they relative to a directory too?
-
Gilles 'SO- stop being evil' about 9 years@leeand00 Yes, the generated file always uses relative file names.
-
kittygirl over 5 years@Gilles,based on unix.stackexchange.com/questions/364517/…
setfacl
files then cannotchmod
again,maybe will cause conflict? -
kittygirl over 5 yearsdo you mean ACL will also fail on pathological filenames?
-
Gilles 'SO- stop being evil' over 5 years@kittygirl I have no idea what you're asking. What does “setfacl files then cannot chmod again” mean? What does this have to do with unix.stackexchange.com/questions/364517/… ? What conflict?
-
roaima over 5 years@kittygirl I didn't include any processing of ACLs in the scriptlet because the OP had explicitly excluded them from the requirements. You can add what you like, bearing in mind that the code isn't particularly robust (see the comment describing pathological filenames).
-
kittygirl over 5 years@Gilles,consider ACL is not respected by other linux function like
cp
, I am afraid of usingsetfacl
. -
kittygirl over 5 years@Gilles,for the files only defined by
chmod
andchown
,cangetfacl -R . >permissions.facl
andsetfacl --restore=permissions.facl
get the origin permission of these files? -
Gilles 'SO- stop being evil' over 5 years@kittygirl Yes.
-
kittygirl over 5 years@Gilles,does it work for filenames starting with whitespace, or containing non-printable characters?
-
Gilles 'SO- stop being evil' over 5 years@kittygirl Yes, including newlines.
-
kittygirl over 5 yearsI found a problem:cannot find
.htaccess
,gitignore
... -
kittygirl over 5 years@Gilles,there's big problem(maybe bug):run
setfacl
by root,cannot change a file like-rw-r--r-- 1 root root
, -
Gilles 'SO- stop being evil' over 5 years@kittygirl I don't understand your comment. When run by root,
setfacl
can change the permissions for any file. -
kittygirl over 5 years@Gilles,actually I found a bug.When you run by root, you cannot change
-rw-r--r-- 1 root root
to-rw-r--r-- 1 usernotexistinyourmachine apache
.Normally,if username not exist,will be replaced by userid like1001
.But if owner isroot
, cannot change to1001
-
Gilles 'SO- stop being evil' over 5 years@kittygirl Uh? Why not? Of course
notexistuser
has to exist. If you request to make a file owned by a user but you give an invalid user name, then the request is rejected, this is the correct behavior. Restoring won't invent a user ID for you, you have to restore the user account before restoring the files.