如何仅复制某些文件和子目录

如何仅复制某些文件和子目录

你好,假设我有一个这样的目录:

/
    my_work
               /dir1
                    keepdir1.ddd
                    keepdir2.ddd
                    file.cfg
                    (lots of files and directories I don't want)
               /dir2
                    (same layout as dir 1)

因此,我想复制目录“my_work”和所有“dir1”、“dir2”等子目录。但在每个子目录中,我只想保留某些文件和某些目录。在我保留的目录中,我想递归地包含所有内容。

这是我到目前为止尝试过的但似乎没有什么可以复制:

rsync -rl --include-from=~/rsync_include /my_work ~/backup

rsync_include 的内容为:

+ *file.cfg
+ *keepdir*/*
- *

答案1

除非您忘记提供重要信息,否则以下脚本将会起作用。

#!/usr/bin/env bash

for DIR in my_work/* ; do
    mkdir -pv dest/"$DIR"
    cp -av "$DIR"/*.cfg "$DIR"/keepdir* dest/"$DIR"
done

示例输出,其中不适用的通配符产生消息:

dest
dest/my_work
dest/my_work/dir1
my_work/dir1/file.cfg -> dest/my_work/dir1/file.cfg
my_work/dir1/keepdir1.ddd -> dest/my_work/dir1/keepdir1.ddd
my_work/dir1/keepdir2.ddd -> dest/my_work/dir1/keepdir2.ddd
dest/my_work/dir2
my_work/dir2/file.cfg -> dest/my_work/dir2/file.cfg
cp: my_work/dir2/keepdir*: No such file or directory
dest/my_work/dir3
cp: my_work/dir3/*.cfg: No such file or directory
cp: my_work/dir3/keepdir*: No such file or directory

相关内容