BASH 复制除一个之外的所有文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1313590/
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-09-09 18:26:53  来源:igfitidea点击:

BASH copy all files except one

bashscriptingcopy

提问by Joe Cannatti

I would like to copy all files out of a dir except for one named Default.png. It seems that there are a number of ways to do this. What seems the most effective to you?

我想从一个目录中复制所有文件,除了一个名为 Default.png 的文件。似乎有很多方法可以做到这一点。你觉得什么最有效?

采纳答案by John Kugelman

Simple, if src/only contains files:

简单,如果src/只包含文件:

find src/ ! -name Default.png -exec cp -t dest/ {} +

If src/has sub-directories, this omits them, but does copy files inside of them:

如果src/有子目录,则省略它们,但会复制其中的文件:

find src/ -type f ! -name Default.png -exec cp -t dest/ {} +

If src/has sub-directories, this does not recurse into them:

如果src/有子目录,这不会递归到它们:

find src/ -type f -maxdepth 1 ! -name Default.png -exec cp -t dest/ {} +

回答by Jon

Should be as follows:

应该如下:

cp -r !(Default.png) /dest

If copying to a folder nested in the current folder (called example in the case below) you need to omit that directory also:

如果复制到嵌套在当前文件夹中的文件夹(在下面的情况下称为示例),您还需要省略该目录:

cp -r !(Default.png|example) /example

回答by matja

rsync has been my cp/scp replacement for a long time:

rsync 长期以来一直是我的 cp/scp 替代品:

rsync -av from/ to/ --exclude=Default.png

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
-v, --verbose               increase verbosity

回答by nos

I'd just do:

我只会做:

cp srcdir/* destdir/ ; rm destdir/Default.png

unless the files are big. Otherwise use e.g.

除非文件很大。否则使用例如

find srcdir -type f/ |grep -v Default.png$ |xargs -ILIST cp LIST destdir/

回答by Will Hartung

cp `ls | grep -v Default.png` destdir

回答by Josh

This works great for copying everything except node modules :) enjoy and thanks to the answers above I have just added on to it. Rsync is better in my opinion than CP as you can see progress bar without asking for it.

这非常适合复制除节点模块之外的所有内容:) 享受并感谢上面的答案,我刚刚添加了它。在我看来,Rsync 比 CP 更好,因为您无需询问即可看到进度条。

   rsync -av fromDirectory/ ToDirectory/ --exclude=node_modules

回答by Ivan Grynenko

# chattr +i /files_to_exclude
# cp source destination
# chattr -i /files_to_exclude

回答by alex

use the shell's expansion parameter with regex

将 shell 的扩展参数与正则表达式一起使用

cp /<path>/[^not_to_copy_file]* .

Everything will be copied except for the not_to_copy_file

除了 not_to_copy_file 之外的所有内容都将被复制

-- if something is wrong with this. please Specify !

——如果这有什么问题的话。请明确说明 !