0

why the following command " find / -name 'node1' -print0 | xargs -0 rename 's/node1/node_STAR/' " not replace the node1 under /var/tmp directory?

my target is to scan linux sys and rename directories and files

example from my linux machine

pwd
/var/tmp/Change_host_dir
find  /  -name '*node1*'  -print0 | xargs -0
/var/tmp/Change_host_dir/node1
find  /  -name '*node1*'  -print0 | xargs -0 rename 's/node1/node_STAR/'
ls
node1
MadHatter
  • 80,590
yael
  • 1,439

2 Answers2

1

try with

find / -name node1 -exec rename blahblah {} \;

Nikolaidis Fotis
  • 2,032
  • 11
  • 13
1

The standard linux rename command man page says:

rename will rename the specified files by replacing the first occurrence of from in their name by to.

so it's just for simple file renames. This is the standard rename from util-linux, available on all linux systems I am aware of.

I think you are probably trying to use Larry Wall's example perl rename script. If that's what you really want, create this file in your path:

#!/usr/bin/perl
#
# rename script examples from lwall:
#       rename 's/\.orig$//' *.orig
#       rename 'y/A-Z/a-z/ unless /^Make/' *
#       rename '$_ .= ".bad"' *.f
#       rename 'print "$_: "; s/foo/bar/ if <stdin> =~ /^y/i' *

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

make it executable with chmod 755 rename, and call it instead of the system rename. I tested it with your example and it works.

  • hi I save your perl script as rename.pl and I run find / -name 'node1' -print0 | xargs -0 rename.pl 's/node1/node_STAR/' but I still have the node1 in place to get node_STAR ? – yael Jan 21 '11 at 07:59
  • call rename.pl like this instead: rename 'print "changing $_\n"; s/node1/node_STAR/' – Phil Hollenback Jan 21 '11 at 08:03
  • I perfrom mv rename.pl rename and run find / -name 'node1' -print0 | xargs -0 rename 'print "changing $_\n"; s/node1/node_STAR/' but still not work why? – yael Jan 21 '11 at 08:09
  • if you run it the second way with changing $_ you should see the output changing node1 from rename. Do you see that? – Phil Hollenback Jan 21 '11 at 08:11
  • OK I fix that need to call it rename.pl with full path then its works – yael Jan 21 '11 at 08:13
  • but there is problem if I have for example node11 then its also replace to node1_START how to replace only if I have full match only – yael Jan 21 '11 at 08:17
  • rename 's/^node1$/node_STAR/' will change only node1 filenames. – Phil Hollenback Jan 21 '11 at 08:20
  • good Job its works – yael Jan 21 '11 at 08:24