0

Using bash or Python how do you :

  • Specify a baseline directory (dir-a)
  • Compare the contents to another directory (dir-b)
  • Where a file/dir exists in both dir-a and dir-b set permissions of the dir-b file to match the permissions of dir-a

So in the following directories (and their child directories) where one file/dir matches another (eg ./SVN_SANDBOX/db and ./SVN_TEST1/db) I'd like to set the permissions for ./SVN_SANDBOX/db to be made equal to those for ./SVN_TEST1/db

glauc@foofoofo:~/devadmin/svn/SVN_SANDBOX$ ls -l
total 28
dr-xr-xr-x 2 glauc glauc 4096 Jul  2 21:16 conf
dr-xr-xr-x 2 glauc glauc 4096 Jul  2 21:16 dav
dr-xr-xr-x 5 glauc glauc 4096 Jul  2 21:16 db
-r--r--r-- 1 glauc glauc    2 Jul  2 21:16 format
dr-xr-xr-x 2 glauc glauc 4096 Jul  2 21:16 hooks
dr-xr-xr-x 2 glauc glauc 4096 Jul  2 21:16 locks
-r--r--r-- 1 glauc glauc  234 Jul  2 21:16 README.txt
glauc@foofoofo:~/devadmin/svn/SVN_SANDBOX$ cd ../SVN_TEST1
glauc@foofoofo:~/devadmin/svn/SVN_TEST1$ ls -l
total 24
drwxrwxr-x 2 glauc glauc 4096 Jul  2 21:23 conf
drwxrwsr-x 6 glauc glauc 4096 Jul  2 21:23 db
-r--r--r-- 1 glauc glauc    2 Jul  2 21:23 format
drwxrwxr-x 2 glauc glauc 4096 Jul  2 21:23 hooks
drwxrwxr-x 2 glauc glauc 4096 Jul  2 21:23 locks
-rw-rw-r-- 1 glauc glauc  229 Jul  2 21:23 README.txt 
4

2 回答 2

3

使用 get/setfacl 可能很容易(检查生成的文件是否具有相对路径):

cd dir-a && getfacl -R . > /permissions.acl
cd dir-b && setfacl --restore=/permissions.acl
于 2013-07-02T21:37:28.880 回答
0

这在 bash 中将比在 Python 中容易得多,特别是如果您可以使用特定于平台的工具,尤其是 Ubuntu 12 保证但一般 linux 不保证的工具......(参见 DRC 的回答。)

但是如果你想在 Python 中做到这一点,这并不难:

import os
import sys

srcdir, dstdir = sys.argv[1:]

paths = (os.path.join(srcdir, name) for name in os.listdir(srcdir))    
modes = {name: os.lstat(path).st_mode for path in paths}

for name in os.listdir(dstdir):
    try:
        mode = modes[name]
    except KeyError:
        pass
    else:
        os.lchmod(os.path.join(dstdir, name), mode)

如果您希望它是递归的,只需使用flat_walk而不是listdir,您可以这样定义:

def flat_walk(path):
    for dirpath, dirnames, filenames in os.walk(path):
        for filename in filenames:
            yield os.path.join(dirpath, filename)
于 2013-07-02T21:46:23.777 回答