2008年12月2日星期二

batch-mv

hard to batch rename file in linux? especially for the file named "hello world"?

perl -e '++$d and print qq(mv "$_" hello$d\n) for glob("hello*")' | sh

if you need a standalone program, it can be done with this:

perl -e '++$c and print qq(mv "$_" @{[substr($ARGV[0],0,-1)]}$c\n) for glob($ARGV[0])' 'hello*'

llama book suggest this code snippet:

foreach my $file (glob "*.old") {
my $newfile = $file;
$newfile =~ s/\.old$/.new/;
if (-e $newfile) {
warn "can't rename $file to $newfile: $newfile exists\n";
} elsif (rename $file, $newfile) {
## success, do nothing
} else {
warn "rename $file to $newfile failed: $!\n";
}
}

没有评论: