Linux cp 目录递归地排除 2 个子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9464752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:51:37  来源:igfitidea点击:

cp dir recursivly excluding 2 subdirs

linuxbashunixcp

提问by dr0zd

I have 1 directory with 9 subdirectories and 10 files. Subdirectory have next level subdirectories and files.

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

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

I want to copy all subdirectories and files recursivly excluding:

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

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

What is the best way for it?

最好的方法是什么?

采纳答案by kev

Maybe the findcommand will help you:

也许find命令会帮助你:

$ find /home/directory -mindepth 1 -maxdepth 1 -name 'subdirectory[57]' -or -exec cp -r {} /path/to/dir \;

回答by aioobe

I don't know a good way of doing it with cp, but it's fairly easy using rsyncand the --excludeswitch.

我不知道用 做这件事的好方法cp,但它的使用rsync--exclude开关相当容易。

回答by jon

Kev's way is better, but this would also work:

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

Explained :

解释:

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.

Outputs:

输出:

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

Change | catto | shto execute the command.

更改| cat| sh执行命令。

<A big disclaimer goes here>

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

You should Kev's solution is better

你应该 Kev 的解决方案更好

回答by Qian

use rsyncwith --excludeis better

使用rsync具有--exclude较好

回答by jfg956

You can use the --excludeoption of tar:

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

{ cd /home/directory; tar -c --exclude=subdirectory5 --exclude=subdirectory7 .; } | { cd _destination_ ; tar -x; }

回答by Morten Pedersen

Why not just use the cpcommand like this:

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

cp -r /home/directory/!(subdirectory5|subdirectory7) /destination

回答by kjohri

rsync -avz --exclude subdirectory5 --exclude subdirectory7 /home/directory/ target-path