Can I use shell wildcards to select filenames rang

2019-03-11 19:15发布

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

Is this possible just using bash wildcards?

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

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

Thank you.

4条回答
小情绪 Triste *
2楼-- · 2019-03-11 19:56

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

$ 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.

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

查看更多
Anthone
3楼-- · 2019-03-11 20:00

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.

查看更多
混吃等死
4楼-- · 2019-03-11 20:02
for i in `seq 0 54`; do cp foo_$i.jpg <target>; done
查看更多
我想做一个坏孩纸
5楼-- · 2019-03-11 20:04

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

cp -t target_directory foo_{0..54}.jpg
查看更多
登录 后发表回答