bash 我可以使用 shell 通配符来选择跨越两位数的文件名(例如,从 foo_1.jpg 到 foo_54.jpg)?

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

Can I use shell wildcards to select filenames ranging across double-digit numbers (e.g., from foo_1.jpg to foo_54.jpg)?

bashshellwildcard

提问by DQdlM

I have a directory with image files foo_0.jpgto foo_99.jpg. I would like to copy files foo_0.jpgthrough foo_54.jpg.

我有一个图像文件的目录foo_0.jpgfoo_99.jpg。我想foo_0.jpg通过foo_54.jpg.

Is this possible just using bash wildcards?

这是否可以仅使用 bash 通配符?

I am thinking something like cp foo_[0-54].jpgbut I know this selects 0-5and 4(right?)

我在想类似的事情,cp foo_[0-54].jpg但我知道这会选择0-54(对吗?)

Also, if it is not possible (or efficient) with just wildcards what would be a better way to do this?

另外,如果仅使用通配符不可能(或有效),那么这样做的更好方法是什么?

Thank you.

谢谢你。

回答by glenn Hymanman

I assume you want to copy these files to another directory:

我假设您想将这些文件复制到另一个目录:

cp -t target_directory foo_{0..54}.jpg

回答by David W.

I like glenn Hymanmananswer, but if you really want to use globbing, following might also work for you:

我喜欢glenn Hymanman 的回答,但如果你真的想使用通配符,以下也可能对你有用:

$ shopt -s extglob
$ cp foo_+([0-9]).jpg $targetDir

In extended globbing +()matches one or more instances of whatever expression is in the parentheses.

在扩展通配符中,+()匹配括号中任何表达式的一个或多个实例。

Now, this will copy ALL files that are named foo_followed by any number, followed by .jpg. This will include foo_55.jpg, foo_139.jpg, and foo_1223218213123981237987.jpg.

现在,这将复制所有命名foo_后跟任意数字,后跟.jpg. 这将包括foo_55.jpgfoo_139.jpg,和foo_1223218213123981237987.jpg

On second thought, glenn Hymanman has the better answer. But, it did give me a chance to talk about extended globbing.

转念一想,格伦Hyman曼有更好的答案。但是,它确实让我有机会谈论扩展 globbing

回答by grok12

ls foo_[0-9].jpg foo_[1-4][0-9].jpg foo_5[0-4].jpg

ls foo_[0-9].jpg foo_[1-4][0-9].jpg foo_5[0-4].jpg

Try it with ls and if that looks good to you then do the copy.

用 ls 试试看,如果你觉得不错,那就复制一下。

回答by pajton

for i in `seq 0 54`; do cp foo_$i.jpg <target>; done