![rsync + chmod 多个文件](https://linux22.com/image/998839/rsync%20%2B%20chmod%20%E5%A4%9A%E4%B8%AA%E6%96%87%E4%BB%B6.png)
我有一个源文件夹
来源/
-rw-r--r-- 1 27773 27773 12 Mar 23 21:41 aws-1
-rw-r--r-- 1 27773 27773 12 Mar 23 21:41 aws-2
目标文件夹尚不存在。我尝试将 2 个 aws 文件 rsync 到目标文件夹中。这是我的命令:
rsync \
--include="aws*" \
--exclude="*" \
-avh --chmod=a=rw "source/." "destination"
我试图做的是 rsync 2 个 aws 文件并在目标文件夹中更改它们的权限。运行命令后,我收到一个错误,文件不在目标文件夹中。仅创建了文件夹。以下是错误:
sending incremental file list
created directory destination
rsync: failed to modify permissions on "/home/cg/root/destination/.": Permission denied (13)
rsync: recv_generator: failed to stat "/home/cg/root/destination/aws-1": Permission denied (13)
rsync: recv_generator: failed to stat "/home/cg/root/destination/aws-2": Permission denied (13)
./
sent 88 bytes received 346 bytes 868.00 bytes/sec
total size is 24 speedup is 0.06
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1165) [sender=3.1.1]
它说权限被拒绝,没有复制文件。我还不明白哪个权限导致了问题。我的源文件和目录似乎没问题。创建的目标文件夹似乎也没问题。
PS:我精确地说明我的 rsync 命令发生在 bash 脚本中
答案1
目录需要执行权限以便进程可以访问(打开)它们。
当您使用时--chmod=a=rw
,目标目录会被创建,但随后其权限会立即设置为drw-rw-rw
,以致其内容变得不可访问(“ failed to stat
”),例如给定
$ tree
.
└── source
├── file1
├── file2
└── file3
1 directory, 3 files
然后
$ rsync --include="file*" --exclude="*" -avh --chmod=a=rw source/ destination/
sending incremental file list
created directory destination
rsync: failed to modify permissions on "/home/steeldriver/destination/.": Permission denied (13)
rsync: recv_generator: failed to stat "/home/steeldriver/destination/file1": Permission denied (13)
rsync: recv_generator: failed to stat "/home/steeldriver/destination/file2": Permission denied (13)
rsync: recv_generator: failed to stat "/home/steeldriver/destination/file3": Permission denied (13)
./
sent 107 bytes received 502 bytes 1.22K bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1183) [sender=3.1.1]
失败,因为
$ ls -dl destination/
drw-rw-rw- 2 steeldriver steeldriver 4096 Mar 23 18:24 destination/
以便
$ ls -al destination/
ls: cannot access 'destination/.': Permission denied
ls: cannot access 'destination/..': Permission denied
total 0
d????????? ? ? ? ? ? .
d????????? ? ? ? ? ? ..
解决方案是使用--chmod=a=rwX
(或者,等效地,--chmod=ugo=rwX
)
$ rsync --include="file*" --exclude="*" -avh --chmod=ugo=rwX source/ destination/
sending incremental file list
created directory destination
./
file1
file2
file3
sent 224 bytes received 110 bytes 668.00 bytes/sec
total size is 0 speedup is 0.00
其中X
符号模式意味着
execute/search only if the file is a directory or
already has execute permission for some use
看man chmod