9

我有 1 个目录,其中有 9 个子目录和 10 个文件。子目录具有下一级子目录和文件。

/home/directory/
/home/directory/subdirectory1
/home/directory/subdirectory2
...
/home/directory/subdirectory9
/home/directory/file1
...
/home/directory/file10

我想递归复制所有子目录和文件,不包括:

/home/directory/subdirectory5
/home/directory/subdirectory7

最好的方法是什么?

4

7 回答 7

30
rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path
于 2012-08-22T12:17:30.443 回答
11

我不知道cprsync.--exclude

于 2012-02-27T12:11:43.133 回答
8

也许该find命令会帮助您:

$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;
于 2012-02-27T12:34:20.377 回答
2

rsync搭配使用--exclude更好

于 2012-02-27T18:40:36.883 回答
2

您可以使用以下--exclude选项tar

{ cd /home/directory; tar -c --exclude=subdirectory5 --exclude=subdirectory7 .; } | { cd _destination_ ; tar -x; }
于 2012-02-27T21:06:08.017 回答
1

Kev 的方式更好,但这也可以:

find "/home/folder" -maxdepth 1 | sed -e "/^\/home\/folder$/d" -e "/^\/home\/folder\/subfolder5$/d" -e "/^\/home\/folder\/subfolder7$/d" -e "s/^/cp \-r /" -e  "s/$/ \/home\/target/" | cat

解释:

find "/home/folder" -maxdepth 1 |
// get all files and dirs under /home/folder, pipe output

sed -e "/^\/home\/folder$/d" 
// have sed strip the path being searched, or the cp -r we prepend later will pickup the excluded dirs again.

-e "/^\/home\/folder\/subfolder5$/d"
// have sed strip subfolder5

-e "/^\/home\/folder\/subfolder7$/d"
// have sed strip subfolder7

-e "s/^/cp \-r /"
// have sed prepend "cp -r " to each line

-e  "s/$/ \/home\/target/" | cat
// have sed append targetdir to each line.

输出:

cp -r /home/folder/subfolder9 /home/target
cp -r /home/folder/subfolder1 /home/target
cp -r /home/folder/file10 /home/target
cp -r /home/folder/subfolder2 /home/target
cp -r /home/folder/file1 /home/target
cp -r /home/folder/subfolder3 /home/target

更改| cat| sh执行命令。

<这里有一个重要的免责声明>

你应该 Kev 的解决方案更好

于 2012-02-27T12:47:28.920 回答
1

为什么不直接使用这样的cp命令:

cp -r /home/directory/!(subdirectory5|subdirectory7) /destination
于 2012-08-22T07:38:21.660 回答