2

I have the following short perl script rename.pl. How can I translate this script to one liner perl syntax? And how to combine the perl one liner in find command?

rename.pl

#!/usr/bin/perl

$op = shift;
for (@ARGV) {
  $was = $_;
  eval $op;
  die $@ if $@;
  rename($was,$_) unless $was eq $_;
}

My current find syntax:

find /tmp -name "*$NAME*" -exec \
  ./rename.pl \
  's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +

The find syntax that I want to build:

find /tmp -name "*$NAME*" -exec \
  < perl one liner line ....... > \
  's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +
  • OTOH, you could use File::Find in the script turning the whole thing into a simple command. I know that seems crazy as you will not get to spend as much time typing and making mistakes, but... $0.02 – goldilocks Mar 13 '13 at 13:23
  • @goldilocks, if he's not familiar with perl (he copy-pasted that perl code), then he's more likely to make mistakes with that, and it's probably going to be less efficient. He could use mmv or zmv that are designed for that kind of task (or even rename, an off the shelf perl script for that). – Stéphane Chazelas Mar 13 '13 at 13:46
  • Good point. I had taken "I have the following short perl script" to mean something like "this is my script, it works" (and now I want to make it a one liner). – goldilocks Mar 13 '13 at 13:54
2

Simply give the content of the script as an argument following -e:

find .... -exec perl -e '
  $op = shift;
  for (@ARGV) {
    $was = $_;
    eval $op;
    die $@ if $@;
    rename($was,$_) unless $was eq $_;
  }' s/a/b/g {} +

You can condense it to:

find ... -exec perl -e '$op=shift;for(@ARGV){$was=$_;eval$op;die$@if$@;rename$was,$_ unless$was eq$_}' s/a/b/g {} +

But that doesn't help legibility.

  • not clearly how to set your first example in the find syntax ? , can you give me full example ? – yael Mar 13 '13 at 13:31
  • WOW - this is great , thanx – yael Mar 13 '13 at 14:23
  • this is the last update or you need to test your syntax to be sure that its work ? – yael Mar 13 '13 at 14:25

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.