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

2 Answers 2

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.

8
  • 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, 2011 at 7:59
  • call rename.pl like this instead: rename 'print "changing $_\n"; s/node1/node_STAR/' Jan 21, 2011 at 8: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, 2011 at 8:09
  • if you run it the second way with changing $_ you should see the output changing node1 from rename. Do you see that? Jan 21, 2011 at 8:11
  • OK I fix that need to call it rename.pl with full path then its works
    – yael
    Jan 21, 2011 at 8:13
1

try with

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

6
  • stiil not works I run your command but node1 not renamed -(:
    – yael
    Jan 21, 2011 at 7:35
  • of course .... try to replace rename blahblah with mv node1 node_START Jan 21, 2011 at 7:37
  • I get error mv: target `/var/tmp/Change_host_dir/node1' is not a directory
    – yael
    Jan 21, 2011 at 7:42
  • @Fotis my target is to rename dirs or files recursive under /
    – yael
    Jan 21, 2011 at 7:45
  • I don't know ... but certainly it's working for me :> maybe you could try mv node1 node_Star/ {Play with the '/' } Jan 21, 2011 at 7:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .