views:

50

answers:

3

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?

+1  A: 

you can get the file's permissions with

ls -l | awk '{print $1" "$NF}'

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.

ozk
@ozk: [Don't parse the output of ls.](http://mywiki.wooledge.org/ParsingLs)
Gilles
And I think I can combine this with @second's idea...
NinjaCat
+2  A: 

hm. so you need to 1) read file permissions 2) store them somehow, associated to each file 3) read your stored permissions and set them back

not a complete solution but some ideas:

stat -c%a filename
>644

probably in combination with

find -exec

to store this information, this so question has some interesting ideas. basically you create a temporary file structure matching your actual files, with each temp file containing the file permissions

to reset you iterate over your temp files, read permissions and chmod the actual files back.

second
You summarized it perfectly. Going to test this out now...
NinjaCat
+1  A: 

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 and setfacl --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.)

find -depth -printf '%m:%u:%g:%p\0' >saved-permissions

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 and chown. The -depth option to find is in case you want to make some directories unwritable (you have to handle their contents first).

Gilles
Never used ACL tools before, but I like this too...
NinjaCat