Search code examples
bashwildcardmv

Referring to wildcard characters in mv operation


This is my bash code:

mv ./foo*foo2*foo3*.foo3 ./foo*.foo3

How can I refer to the second wildcard character from ./foo*foo2*foo3*.foo3 when renaming my file using the mv command?

I'm sorry if this sounds stupid, but I couldn't find an answer anywhere. Maybe I'm using incorrect terminology? If so, please let me know.


Solution

  • The rename tool is just for that:

    sh$ touch fooAfoo2Bfoo3C.foo3
    sh$ rename -v 's|foo.*foo2(.*)foo3.*\.foo3|foo$1.foo3|' *.foo3
    #                         ^^^^                ^^
    #                     capture the           insert
    #                   second "wildcard"  --->  here
    fooAfoo2Bfoo3C.foo3 renamed as fooB.foo3