yoga
yoga

Reputation: 860

Rename command not evaluating expressions

i have the following files in my unix box:

CB_CB13_B01_20190502.txt 

CB_CB13_B01_20190503.txt.1

CB_CB13_B01_20190504.txt.2 

CB_CB13_B01_20190505.txt.3

CB_CB13_B01_20190506.txt.a 

CB_CB13_B01_20190507.txt.b

and so on

I am trying to remove all of the characters after .txt with the help of rename command:

rename "txt.*" ".txt" *

The rename is not working when i use * in the expression. any idea what i am doing wrong here.

Upvotes: 0

Views: 55

Answers (2)

jdw
jdw

Reputation: 76

@Yasen was right:

Test:

#!/bin/bash
rm -f something.txt
touch something.txt.2
echo "original contents:"
ls something*
rename --version
rename -v 's/txt.*/txt/' something*
echo "updated contents:"
ls something*

Result

original contents:
something.txt.2
/usr/bin/rename using File::Rename version 0.20

something.txt.2 renamed as something.txt
updated contents:
something.txt

Upvotes: 1

Yasen
Yasen

Reputation: 4474

rename uses Perl Compatible Regular Expression for substitution, e.g. s/txt.*/txt/

So, try this:

rename 's/txt.*/txt/' *

Note: you may use -n switch to just try without real renaming.

$ rename -n 's/txt.*/txt/' *
> rename(B_CB13_B01_20190503.txt.1, B_CB13_B01_20190503.txt)
> rename(B_CB13_B01_20190504.txt.2, B_CB13_B01_20190504.txt)
> rename(B_CB13_B01_20190505.txt.3, B_CB13_B01_20190505.txt)
> rename(B_CB13_B01_20190506.txt.a, B_CB13_B01_20190506.txt)
> rename(B_CB13_B01_20190507.txt.b, B_CB13_B01_20190507.txt)

Upvotes: 2

Related Questions