Linux 如何使用“cp”命令排除特定目录?

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

How to use 'cp' command to exclude a specific directory?

linuxcp

提问by David Liu

I want to copy all files in a directory except some files in a specific sub-directory. I have noticed that 'cp' command didn't have a --exclude option. So, how can I achieve this?

我想复制目录中的所有文件,但特定子目录中的某些文件除外。我注意到 'cp' 命令没有 --exclude 选项。那么,我怎样才能做到这一点?

采纳答案by hank

rsync is fast and easy:

rsync 既快速又简单:

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude

You can use --excludemultiples times.

您可以--exclude多次使用。

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude

Note that the dir thefoldertoexcludeafter --excludeoption is relative to the sourcefolder, i.e., sourcefolder/thefoldertoexclude.

请注意,选项thefoldertoexclude之后的 dir--exclude是相对于sourcefolder,即sourcefolder/thefoldertoexclude.

Also you can add -nfor dry run to see what will be copied before performing real operation, and if everything is ok, remove -nfrom command line.

您也可以添加-n用于试运行以查看在执行实际操作之前将复制的内容,如果一切正常,请-n从命令行中删除。

回答by sudo work

cp -rv `ls -A | grep -vE "dirToExclude|targetDir"` targetDir

Edit: forgot to exclude the target path as well (otherwise it would recursively copy).

编辑:忘记排除目标路径(否则它会递归复制)。

回答by Linus Kleen

Why use rsyncwhen you can do:

为什么rsync在可以时使用:

find . -type f -not -iname '*/not-from-here/*' -exec cp '{}' '/dest/{}' ';'

This assumes the target directory structure being the same as the source's.

这假设目标目录结构与源目录结构相同。

回答by Vanwaril

I assume you're using bash or dash. Would this work?

我假设您使用的是 bash 或 dash。这行得通吗?

shopt -s extglob  # sets extended pattern matching options in the bash shell
cp $(ls -laR !(subdir/file1|file2|subdir2/file3)) destination

Doing an ls excluding the files you don't want, and using that as the first argument for cp

执行 ls 排除您不想要的文件,并将其用作 cp 的第一个参数

回答by ehudokai

Another simpler option is to install and use rsync which has an --exclude-dir option, and can be used for both local and remote files.

另一个更简单的选择是安装和使用 rsync,它有一个 --exclude-dir 选项,可用于本地和远程文件。

回答by ulidtko

Well, if exclusion of certain filename patterns had to be performed by every unix-ish file utility (like cp, mv, rm, tar, rsync, scp, ...), an immense duplication of effort would occur. Instead, such things can be done as part of globbing, i.e. by your shell.

好吧,如果每个 unix-ish 文件实用程序(如 cp、mv、rm、tar、rsync、scp 等)都必须排除某些文件名模式,则会发生巨大的重复工作。相反,这些事情可以作为globbing 的一部分来完成,即通过你的 shell。

bash

猛击

man 1 bash, /extglob.

man 1 bash/extglob

Example:

例子:

$ shopt -s extglob
$ echo images/*
images/004.bmp images/033.jpg images/1276338351183.jpg images/2252.png
$ echo images/!(*.jpg)
images/004.bmp images/2252.png

So you just put a pattern inside !(), and it negates the match. The pattern can be arbitrarily complex, starting from enumeration of individual paths (as Vanwaril shows in another answer): !(filename1|path2|etc3), to regex-like things with stars and character classes. Refer to the manpage for details.

所以你只要在里面放一个模式!(),它就会否定匹配。该模式可以是任意复杂的,从单个路径的枚举(如 Vanwaril 在另一个答案中所示)开始!(filename1|path2|etc3),到具有星号和字符类的类似正则表达式的事物。有关详细信息,请参阅联机帮助页。

zsh

zsh

man 1 zshexpn, /filename generation.

man 1 zshexpn/文件名生成

You can do setopt KSH_GLOBand use bash-like patterns. Or,

你可以做setopt KSH_GLOB和使用类似 bash 的模式。或者,

% setopt EXTENDED_GLOB
% echo images/*
images/004.bmp images/033.jpg images/1276338351183.jpg images/2252.png
% echo images/*~*.jpg
images/004.bmp images/2252.png

So x~ymatches pattern x, but excludes pattern y. Once again, for full details refer to manpage.

所以x~y匹配模式x,但排除模式y。再次,有关完整详细信息,请参阅联机帮助页。



fishnew!

新!

The fishshell has a much prettier answerto this:

外壳有一个更漂亮的回答这个:

 cp (string match -v '*.excluded.names' -- srcdir/*) destdir


Bonus pro-tip

奖金专业提示

Type cp *, hit CtrlX*and just see what happens. it's not harmful I promise

输入cp *,点击CtrlX*,看看会发生什么。我保证这无害

回答by John Smith

mv tobecopied/tobeexcluded .
cp -r tobecopied dest/
mv tobeexcluded tobecopied/

回答by Steven Penny

Expanding on mvds's comment, this works for me

扩展mvds 的评论,这对我有用

cd dotfiles
tar -c --exclude .git --exclude README . | tar -x -C ~/dotfiles2

回答by vineet kumar

cp -r `ls -A | grep -v "c"` $HOME/

回答by dzon

rsync

同步

rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/

and first try it with -n option to see what is going to be copied

并首先尝试使用 -n 选项以查看将要复制的内容