Vatsal
Vatsal

Reputation: 18181

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.

Upvotes: 2

Views: 169

Answers (2)

Sylvain Leroux
Sylvain Leroux

Reputation: 52000

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

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174706

Try the below rename command on the directory where the original file is located.

rename 's/^.*\./foo\*\./' *.foo3

It renames foo*foo2*foo3*.foo3 file to foo*.foo3

Upvotes: 1

Related Questions