简体   繁体   English

复制除一个以外的所有目录

[英]Copy all directories except one

I'm copying all subdirectories with its contents to my current directory as follows:我将所有子目录及其内容复制到我的当前目录,如下所示:

cp -r dirToCopy/* .

But in the folder dirToCopy , there is one subfolder called dirNotToCopy which should not be copied.但在文件夹dirToCopy中,有一个名为dirNotToCopy的子文件夹,不应被复制。

How can I filter that particular folder out of my expression?如何从我的表达式中过滤掉那个特定的文件夹?

Use extended globbing:使用扩展通配符:

shopt -s extglob
cp -r dirToCopy/!(dirNotToCopy) .

Well if you want to do it in single line:好吧,如果您想在单行中执行此操作:

find /path_to/dirToCopy -mindepth 1 -type d  ! -name dirNotToCopy -exec cp -r {} . \; 

One more way of doing the same.做同样的另一种方法。

“-R”标志可以复制包括“符号链接”在内的所有文件,因此您最好使用“-R”

cp -R dirToCopy/!(dirNotToCopy) .
find /path_to/dirToCopy -maxdepth 1 -type d  ! -name dirNotToCopy -exec cp -r {} . \; 

Instead of using mindepth suggested in the other answer, we should use maxdepth.我们应该使用 maxdepth,而不是使用其他答案中建议的 mindepth。 (I can't comment or edit another answer since I do not have enough reputation yet) (我无法评论或编辑另一个答案,因为我还没有足够的声誉)

Also note that this command only copies the subdirectories, not the files in the directory.另请注意,此命令仅复制子目录,而不是目录中的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM