我想做一个备份,在其中递归地遍历 dir 结构,并且仅获取其名称与特定正则表达式匹配的文件的目录。我想保留目录结构,以便如果有匹配项
〜/dir1/dir2/regexpmatch.txt
它在目标中创建相同的目录结构,并将文件复制到
/media/backup/dir1/dir2/regexpmatch.txt
最好使用 rsync,但如果不可能的话,可以使用另一个程序。
答案1
find
与结合怎么样tar
?查找所有文件的示例.c
:
find . -type f -name '*.c' -print | tar zcf backup.tar.gz -T -
答案2
你应该这样组合find
和命令:rsync
find . -regex .*X | rsync --files-from=- ./ b/
使用示例:
$ find b/
b/
b/a
b/a/dX
b/a/cX
$ find a/
a/
a/dX
a/cX
a/b
$ rm -rf b
$ find . -regex .*X | rsync --files-from=- ./ b/
$ find b/
b/
b/a
b/a/dX
b/a/cX
这里我只复制符合正则表达式的文件.*X
。
答案3
从 rsync 手册页:
Here s an example that copies all .pdf files in a hierarchy, only creating
the necessary destination directories to hold the .pdf files, and ensures
that any superfluous files and directories in the destination are removed
(note the hide filter of non-directories being used instead of an exclude):
rsync -avm --del --include= *.pdf -f hide,! */ src/ dest
If you didn t want to remove superfluous destination files, the more time-
honored options of "--include= */ --exclude= * " would work fine in place
of the hide-filter (if that is more natural to you).