0

When I'm using group matched pattern in rename command and use their back references in replacement part and I also want to have command substitution expansion as well within my rename command and using double quotes to expand, but shell also considering back references as parameters though and that cause problem renaming. How can I avoid this?

rename "s:(.*)$(date +"%Y%m%d"):$1$(date -d "+1day" +"%Y%m%d"):" *

Also I tried seperate command substitution expansion part but didn't luck, it just printing extra quotes around $(date -d "+1day" +"%Y%m%d").

rename 's:(.*)'"$(date +"%Y%m%d")"':$1'"'"$(date -d "+1day" +"%Y%m%d")"'"':' *
2

Just escape $ in $1:

rename "s:(.*)$(date +"%Y%m%d"):\$1$(date -d "+1day" +"%Y%m%d"):" *

But you can simplify you pattern like this:

rename "s#$(date +%Y%m%d)#$(date -d +1day +%Y%m%d)#" *

(this assumes # doesn't appear in the expansion of date).

  • thanks, I tried to escaping but never mind that I didn't do on $1 :D And yes, I know that (.*) not requires here, but I should give one example that produce my question : ) – αғsнιη Sep 19 '17 at 19:44
1

Assuming this is the Perl version of rename, one solution is to perform the date math with Perl.

rename 'use Time::Piece;use Time::Seconds;$t=localtime;$n=$t+ONE_DAY;$f=q{(.*)}.$t->strftime("%Y%m%d");s:$f:$1.$n->strftime("%Y%m%d"):e' *

Those modules should be in core for perl >= 5.10 unless your vendor has done strange things to the core install.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.