I am using a linux system and need to experiment with some permissions on a set of nested files and directories. I wonder if there is not some way to save the permissions for the files and directories, without saving the files themselves.
In other words, I'd like to save the perms, edit some files, tweak some permissions, and then restore the permissions back onto the directory structure, keeping the changed files in place.
Does that make sense?
There is also a special tool for that called metastore:
It's also available as Debian package.
I have a Python script for doing this at https://github.com/robertknight/mandrawer/blob/master/save-file-attrs.py
save-file-attrs.py save
Will save the permissions, mode and modification times of files in the dir tree rooted at the current working directory to a local file (.saved-file-attrs) and:
save-file-attrs.py restore
Will restore those attributes from the file and display the changes.
save:
find . -type f |xargs ls -la| awk '{print "chmod "$1" "$NF}'>./filesPermissions.sh
restore:
sh ./filesPermissions.sh
Here's an example for easily doing this with a single file. No additional tools, scripts, temp file, etc. are required. You could expand upon this method for working with more files if needed.
In this specific example, the permissions are saved in a varibale via the
stat
command. Then, the file is temporarily stripped of any restrictive permissions. Next, something is done with it (that may have failed due to those prior restrictions). Finally, the original permissions are restored.The easiest way is to use ACL tools, even if you don't actually use ACLs. Simply call
getfacl -R . >saved-permissions
to back up the permissions of a directory tree andsetfacl --restore=saved-permissions
to restore them.Otherwise, a way to back up permissions is with
find -printf
. (GNU find required, but that's what you have on Linux.)You get a file containing records separated by a null character; each record contains the numeric permissions, user name, group name and file name for one file. To restore, loop over the records and call
chmod
andchown
. The-depth
option tofind
is in case you want to make some directories unwritable (you have to handle their contents first).You can restore the permissions with this bash snippet derived from a snippet contributed by Daniel Alder:
You can use the following awk script to turn the
find
output into some shell code to restore the permissions.you can get the file's permissions with
which will return a list of file names and their permissions. save it somewhere, and once you're done - restore (chmod) each file's permissions.