[ale] Mass changing of file extension

JD jdp at algoloma.com
Fri Sep 14 12:00:59 EDT 2012


I don't recall seeing "*\.tsv" in any examples in the file globbing part with
the rename command.  Normally if I want that, I'd use "*[.]tsv" instead, but the
"\." should work find too.  I'm odd in that way. ;)

Perhaps I'm missing something?



There are two different regex engines involved.
* bash/shell file globber
* perl regex inside the "rename" command
For simple regex, those work almost the same, for many values of "simple regex."

$ rename 's/tsv$/csv/g' *tsv
vs
$ rename 's/\.tsv$/.csv/g' *tsv

In the real world, the '$' at the end of the first pattern-search (forcing an
end of filename/string match) and 3 characters, "tsv", are probably enough to
perform the intended replacements in the file names.  Anything longer is extra
characters and if those need to be escaped, it is just more, extra characters.

In my experience, filenames like "file1.tsv" and "file2tsv" don't really happen
in the same directory.  If that happens in the filenames, then the leading \. is
required.

The file globbing with "*.tsv" does not match on any "period" character. A
period means "any character" in regex patterns, even simple ones for file
globbing.  To a human, the intension is clear, but the regex parser will happily
assume you know the regex rules.

Of course, different shells could handle the file globbing a little differently,
but for mostly Borne shell compatible shells, like bash, this is how it works.

>From the bash manual:
"When a pattern is used for filename expansion, the character ‘.’ at the start
of a filename or immediately following a slash must be matched explicitly,
unless the shell option dotglob is set. When matching a file name, the slash
character must always be matched explicitly. In other cases, the ‘.’ character
is not treated specially."

https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html


On 09/14/2012 11:28 AM, Jim Kinney wrote:
> so "*\.tsv" wouldn't require the "." before the tsv?
> 
> On Fri, Sep 14, 2012 at 11:26 AM, JD <jdp at algoloma.com
> <mailto:jdp at algoloma.com>> wrote:
> 
>     On 09/14/2012 11:21 AM, Sparr wrote:
>     > be careful with this one, it will rename things that end with "tsv"
>     > and not just ".tsv", such as perhaps your hypothetical "renametsv"
>     > script :)
>     >
>     > On Fri, Sep 14, 2012 at 11:18 AM, JD <jdp at algoloma.com
>     <mailto:jdp at algoloma.com>> wrote:
>     >> $ rename 's/tsv$/csv/g' *tsv
> 
> 
>     There is no different in the shell globbing between "*tsv" and "*.tsv" since "."
>     matches any character in a regex.
> 
>     The ".ext" match is special in MS-Dos/Windows, not UNIX.


More information about the Ale mailing list