[kwlug-disc] bash - file renaming

Doug Moen doug at moens.org
Thu Sep 26 07:17:47 EDT 2019


I tested your utility. It seems to work as expected, even if there are multiple '*' characters in the glob pattern. I also tested some cross-directory renames, and those worked too. I haven't broken it yet.

So here's my competitive analysis.

There are other Linux commands that do this, but they are more complex to use, and they don't use the DOS rename command line syntax. Matching the semantics of a command that other people are already familiar with seems like a good strategy, and that's what makes your  project distinctive, AFAIK.

Ubuntu has the 'rename' and 'mmv' commands. rename is preinstalled, mmv has to be explicitly installed, 'sudo apt install mmv'. Here's a comparison:

$ rename.sh '*.foo' '*.bar'
$ rename 's/\.foo/.bar/' *.foo
$ mmv '*.foo' '#1.bar'

`rename` is written by Larry Wall, creator of Perl, and you write a short Perl program to modify each file name. It's very powerful, but you need to understand how to write Perl code.

'mmv' is closer in spirit to the DOS rename command, but since there can be multiple wildcards in the first argument, the author requires you to use the syntax #1, #2 and so on to specify which wildcard you are referring to, in the second argument. In particular, "mmv '*.foo' '*.bar' will not work.

Since there is already a `rename` command on Ubuntu, I suggest you choose a more distinctive name for your command. I suggest `ren`, which is a valid DOS command (it's an alias for `rename` on DOS).

Doug Moen.

On Thu, Sep 26, 2019, at 4:00 AM, Ronald Barnes 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
>




More information about the kwlug-disc mailing list