Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 1 down vote accepted

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.

share|improve this answer
    
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 7:59
    
call rename.pl like this instead: rename 'print "changing $_\n"; s/node1/node_STAR/' –  Phil Hollenback Jan 21 '11 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 '11 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? –  Phil Hollenback Jan 21 '11 at 8:11
    
OK I fix that need to call it rename.pl with full path then its works –  yael Jan 21 '11 at 8:13

try with

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

share|improve this answer
    
stiil not works I run your command but node1 not renamed -(: –  yael Jan 21 '11 at 7:35
    
of course .... try to replace rename blahblah with mv node1 node_START –  Nikolaidis Fotis Jan 21 '11 at 7:37
    
I get error mv: target `/var/tmp/Change_host_dir/node1' is not a directory –  yael Jan 21 '11 at 7:42
    
@Fotis my target is to rename dirs or files recursive under / –  yael Jan 21 '11 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 '/' } –  Nikolaidis Fotis Jan 21 '11 at 7:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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