使用bash或Python你如何:
所以在以下目录(及其子目录)中,一个文件/目录与另一个文件/目录匹配(例如./SVN_SANDBOX/db和./SVN_TEST1/db)我想将./SVN_SANDBOX/db的权限设置为等于./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
答案 0 :(得分:2)
使用get / setfacl(检查生成的文件是否具有相对路径)可能很简单:
cd dir-a && getfacl -R . > /permissions.acl
cd dir-b && setfacl --restore=/permissions.acl
答案 1 :(得分: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)