[kwlug-disc] bash - file renaming

Cedric Puddy cedric at ccj.host
Thu Sep 26 08:02:42 EDT 2019


I've always done as Jason suggests, but the one thing that is usually helpful is the addition of "basename":

> for $FILE in *
> do
> mv $FILE $(basename $FILE .htm).whatever
> done

Basename will remove an arbitrary string (eg: .htm) from the end of another string, thus getting rid of the old extension, so you can tack on your new ".html" or ".whatever" extension.

The fact that this covers most use cases is why there is no utility, I suspect.  When I was first getting started, a grey beard showed me loops - I think for this exact reason, actually.

Also, there would be no need for multiple "*" wildcards, I expect, since every "*.htm" is going to get the new target extension, so why not have the syntax for the rename utility be "rename *.oldext .newext"?

   Warm Regards,
  -Cedric

[CCj] [Cedric Puddy, President, CCj/Clearline Inc..  T: 519-342-4004x102]

> On Sep 26, 2019, at 13:04, Jason Eckert <jason.eckert at gmail.com> wrote:
> 
> Why not write a simple for loop in bash?
> for $FILE in *
> do
> mv $FILE $FILE.whatever
> done
> 
>> On Thu, Sep 26, 2019 at 4:23 AM Ronald Barnes <ron at ronaldbarnes.ca> wrote:
>> 
>> Hi everyone,
>> 
>> As you probably already know, Gnu/Linux distributions do not come with a 
>> rename utility similar to what some of us "grew up on" in DOS and/or OS/2.
>> 
>> The "mv" (move) command is to be used instead. But it doesn't handle 
>> things like "mv *.htm *.html".
>> 
>> So, I decided to tackle writing one.
>> 
>> 
>> Since bash has "Pathname Expansion", which expands a parameter like 
>> "*.htm" into a list of files that match that expression, it was a bit 
>> more challenging than I expected.
>> 
>> 
>> However, I think I have something that handles wildcards sufficiently well.
>> 
>> > https://github.com/RonaldBarnes/bash-rename
>> 
>> 
>> If anyone is interested in having a look, and providing feedback, it 
>> would be welcome.
>> 
>> 
>> There is a "rename.sh" which does the parameter parsing (although once 
>> it parses the parameters, "mv" does the actual work), and a 
>> "rename.unit-tests.sh" which is - so far - just notes to myself on edge 
>> cases.
>> 
>> 
>> 
>> I'm pretty sure this utility can be of some use to a significant number 
>> of people and hopefully someone on this list will agree.
>> 
>> 
>> Note, there's a "no-op" aka "dry-run" option of -n that will prevent any 
>> files from being touched / moved / renamed, and a -vvv option to spew 
>> out debugging info to help determine how the script derives the target 
>> file name from the wildcards provided.
>> 
>> 
>> 
>> Thanks!
>> 
>> rb
>> 
>> _______________________________________________
>> kwlug-disc mailing list
>> kwlug-disc at kwlug.org
>> http://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org
> _______________________________________________
> kwlug-disc mailing list
> kwlug-disc at kwlug.org
> http://kwlug.org/mailman/listinfo/kwlug-disc_kwlug.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://kwlug.org/pipermail/kwlug-disc_kwlug.org/attachments/20190926/a09fc9bc/attachment.htm>


More information about the kwlug-disc mailing list