如何排除特定目录被复制?

问题描述 投票:0回答:3

我有以下行,它是 bash 脚本的一部分,用于复制网站,但我想添加到其中以排除一个目录

website.com/_archive
。我该怎么做?

rsync -a --info=progress2 --no-i-r ~/website.com ~/tmp/"$projectName"/www

之前的线路一直在运行。我只是想排除一个目录。

bash rsync
3个回答
1
投票

给定目录树:

├── website.com
│   ├── _archive
│   │   └── d1
│   └── d2
│       └── _archive
│           └── d3
└── www

您的命令将导致后者变成:

└── www
    └── website.com
        ├── _archive
        │   └── d1
        └── d2
            └── _archive
                └── d3

要仅排除顶级

_archive
目录,您可以使用
--exclude
选项,如其他答案所述,但您还需要提供锚定路径:

--exclude=/website.com/_archive/

给予:

└── www
    └── website.com
        └── d2
            └── _archive
                └── d3

来自联机帮助页:

The matching rules for the pattern argument take several forms:

o  If  a  pattern contains a / (not counting a trailing slash) or a
   "**" (which can match a slash),  then  the  pattern  is  matched
   against  the  full  pathname,  including any leading directories
   within the transfer.  If the pattern  doesn't  contain  a  (non-
   trailing) / or a "**", then it is matched only against the final
   component of the filename or pathname. For  example,  foo  means
   that  the final path component must be "foo" while foo/bar would
   match the last 2 elements of the path (as long as both  elements
   are within the transfer).

o  A  pattern  that  ends  with a / only matches a directory, not a
   regular file, symlink, or device.

o  A pattern that starts with a / is anchored to the start  of  the
   transfer  path  instead  of  the  end.   For example, /foo/** or
   /foo/bar/** match only leading elements in  the  path.   If  the
   rule is read from a per-directory filter file, the transfer path
   being matched will begin at the level of the filter file instead
   of  the  top  of the transfer.  See the section on ANCHORING IN‐
   CLUDE/EXCLUDE PATTERNS for a full discussion of how to specify a
   pattern that matches at the root of the transfer.

-1
投票
rsync -a --info=progress2 --exclude='_archive/' --no-i-r ~/website.com ~/tmp/"$projectName"/www

--排除=模式

此选项是 --filter 选项的简化形式,默认为排除规则,并且不允许正常过滤规则的完整规则解析语法。

请检查:https://linux.die.net/man/1/rsync

这里有几个例子: https://linuxize.com/post/how-to-exclude-files-and-directories-with-rsync/#exclude-a-specific-directory


-1
投票

您想使用选项

--exclude
,因此您的命令如下所示:

rsync -a --info=progress2 --no-i-r ~/website.com ~/tmp/"$projectName"/www --exclude=/website.com/_archive/

这也会将目录

website.com
复制到目录
~/tmp/"$projectName"/www
中。 如果您只想复制内容而不复制包含目录,请使用

rsync -a --info=progress2 --no-i-r ~/website.com/ ~/tmp/"$projectName"/www --exclude=/_archive/

请注意

~/website.com
后面的附加斜杠以及更改后的排除选项。排除选项应包含相对于入口点
/
~/website.com
的锚定路径(以
~/website.com/
开头)。 不以
/
开头的排除选项,排除具有给定名称的所有目录,无论它们位于目录结构中的位置。

感谢@jhnc,您通过评论指出了这一点。

© www.soinside.com 2019 - 2024. All rights reserved.