大网数据广告活动
服务器运维问答

In an rsync, how do I exclude all directories that match a pattern?

· 52CE 我爱测

In an rsync I am trying to exclude sub-directories that match a pattern. But, I cannot get it to work. I have followed several examples found here and on Google. But, I do not get the correct results. Here is the option bit of my…

问题原文

原帖链接 · Roger Creasy · CC BY-SA 3.0

In an rsync I am trying to exclude sub-directories that match a pattern. But, I cannot get it to work. I have followed several examples found here and on Google. But, I do not get the correct results. Here is the option bit of my command:

-avh --exclude 'branch*' --stats --delete --link-dest=$LNK

My source directory structure is

/root
    /branch1
    /branch2
    /branch3
    /other
    /stillAnother
    /etc

This is part of a backup script. $LNK is a link to the previous day's rsync destination.

I do not want /root/branch1, /root/branch2, /root/branch3. or their contents to be synced. But, they are.

Here are the exclude bits I have already tried:

--exclude=branch*
--exclude='branch*'
--exclude '/branch*'
--exclude /branch*

Thanks for any help/advice.

EDIT - to address the "possible duplicate" flag

This question is regarding a known list of directories. I need to exclude any directories that follow a pattern, even if those directories do not yet exist. i.e. from my example, other directories named /branch* may be added. I need to make my script future-proof, and avoid editing the script when a directory that matches the pattern is added, as those directories may be temporary.

社区高分解答

原帖链接 · AaronDanielson · CC BY-SA 4.0

rsync version 3.1.3 (possibly earlier, haven't checked) correctly excludes subdirectories using this syntax (obviously replacing exclude_dirname with the pattern you want to exclude):

rsync [other opts...] --exclude='*/exclude_dirname/' /src/ /dst/

This also works with wildcards. Original question uses 'branch*', so this works:

rsync [other opts...] --exclude='*/branch*/' /src/ /dst/

Hope this helps.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-09-08)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。