From: Tuxedo on
I'd like to remove from within a shell script a series of files with the
following number format:
004023.jpg
004024.jpg
004025.jpg
etc.

They all start with '004' and end with '.jpg'. In-between there can be
exactly 3 numericals, each with any value from 0 to 9, such as 004000.jpg,
004920.jpg, 004999.jpg, 004111.jpg etc.

Of course, a simple way is to just do:
rm 004*.jpg

So as not to accidentally remove any jpg image that happen start with 004
and end with .jpg, apart from the ones that follow the above 6-number
format, what is the bash expression to narrow the match for a safe delete?

Tuxedo
From: Janis Papanagnou on
Tuxedo wrote:
> I'd like to remove from within a shell script a series of files with the
> following number format:
> 004023.jpg
> 004024.jpg
> 004025.jpg
> etc.
>
> They all start with '004' and end with '.jpg'. In-between there can be
> exactly 3 numericals, each with any value from 0 to 9, such as 004000.jpg,
> 004920.jpg, 004999.jpg, 004111.jpg etc.
>
> Of course, a simple way is to just do:
> rm 004*.jpg
>
> So as not to accidentally remove any jpg image that happen start with 004
> and end with .jpg, apart from the ones that follow the above 6-number
> format, what is the bash expression to narrow the match for a safe delete?

Use character ranges. For codes with sequentially coded numbers (like
ASCII, ISO Latin, etc.) that's

004[0-9][0-9][0-9].jpg

Another modern way (which has not the above mentioned restriction but
may not work on older systems) is

004[[:digit:]][[:digit:]][[:digit:]].jpg


Janis

>
> Tuxedo
From: mallin.shetland on
Addì sabato 23 gennaio 2010 10:41 Tuxedo scrisse:

> I'd like to remove from within a shell script a series of files with the
> following number format:
> 004023.jpg
> 004024.jpg
> 004025.jpg
> etc.
> ...

This issue does not concerne regular expression at all!
This task can be done using brace epansion; I.E. in bash and ksh
you type:

rm -f 004{0..9}{0..9}{0..9}.jpg

In ksh you can type also:

rm -f 004{0..999%03d}.jpg



From: Janis Papanagnou on
mallin.shetland wrote:
> Addì sabato 23 gennaio 2010 10:41 Tuxedo scrisse:
>
>> I'd like to remove from within a shell script a series of files with the
>> following number format:
>> 004023.jpg
>> 004024.jpg
>> 004025.jpg
>> etc.
>> ...
>
> This issue does not concerne regular expression at all!

Of course it does. File globbing uses regular expressions.

> This task can be done using brace epansion; I.E. in bash and ksh
> you type:
>
> rm -f 004{0..9}{0..9}{0..9}.jpg

This is bad advice. Brace expansion will create a string with *all*
lexically possible combinations of the brace expression evaluation,
and not just the subset of interesting files from the existing set
of files.

You've optically hidden that bad effect by using option -f, but you
will see what happens if you omit it. And generally you have to omit
-r because you may not want to automatically delete a file where you
have set write protection; but you would always remove it if you use
option -r.

Moreover, you will not only get those error messages, but you cannot
complete that command if the command string will get too long, e.g.

$ rm -f 004{0..9}{0..9}{0..9}{0..9}{0..9}.jpg
ksh: rm: /bin/rm: cannot execute [Argument list too long]

So instead, use standard file globbing as already proposed uptherad.
It's standard.

Janis

>
> In ksh you can type also:
>
> rm -f 004{0..999%03d}.jpg
>
>
>
From: mallin.shetland on
On Saturday 23 January 2010 11:59 Janis Papanagnou wrote:

> Of course it does. File globbing uses regular expressions.

You are quite confused. Regular expression is a thing, globbing
is a totally different (and simpler) thing.
Regular expressions belong to the theory of formal languages
while globbing is the use of whildcard and subset.




On Saturday 23 January 2010 11:59 Janis Papanagnou wrote:

> This is bad advice. Brace expansion will create a string with *all*
> lexically possible combinations of the brace expression evaluation,
> and not just the subset of interesting files from the existing set
> of files.
> ...

Normally you are right but in this particular case it is safe doing that.
In spite of this you are right, my method was orrible. There is another
cleaver way doing that in many shells:

rm -f 0004[0-9][0-9][0-9].jpg

What' This is exactly what you have suggested?
Well I'm quite confused. :-(