Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 820
DQdlM Avatar asked Jun 22 '11 16:06

DQdlM


People also ask

How do you use wildcards in file names?

An asterisk is replaced by any number of characters in a filename. For example, ae* would match aegis, aerie, aeon, etc. if those files were in the same directory. You can use this to save typing for a single filename (for example, al* for alphabet.

What is wildcard in shell script?

A wildcard is a symbol that takes the place of an unknown character or set of characters. Commonly used wildcards are the asterisk ( * ) and the question mark ( ? ). Depending on the software or the search engine you are using, other wildcard characters may be defined.

What is files wildcard?

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively.


2 Answers

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

cp -t target_directory foo_{0..54}.jpg 
like image 147
glenn jackman Avatar answered Oct 11 '22 19:10

glenn jackman


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.

like image 36
David W. Avatar answered Oct 11 '22 18:10

David W.