我有一个重命名文件/目录名称的 perl 行代码
代码:
find /tmp -name "*$NAME_THAT_WE_WANT_TO_CHANGE*" -exec /tmp/rename.pl 's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +
我想在我的代码中添加打印消息(警告),以便打印将更改的文件/目录
所以我添加了这一行:
&& warn "Rename file - [$ENV{$NAME_THAT_WE_WANT_TO_CHANGE}"'
所以最后更新代码将是:
find /tmp -name "*$NAME_THAT_WE_WANT_TO_CHANGE*" -exec /tmp/rename.pl 's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/ {} + && warn "Rename file - [$ENV{$NAME_THAT_WE_WANT_TO_CHANGE}"'
当我运行 perl one liner 时,我收到以下错误消息:
find: missing argument to `-exec'
请建议我需要在我的代码中修复/更新什么?
. . . . .
完整示例(在我添加警告“.........”之前)
cd /tmp
touch orig-name
touch new-name
export NAME_THAT_WE_WANT_TO_CHANGE=orig-name
export NEW_NAME=new-name
find /tmp -name "*$NAME_THAT_WE_WANT_TO_CHANGE*" -exec /tmp/rename.pl 's/\Q$ENV{NAME_THAT_WE_WANT_TO_CHANGE}\E/$1$ENV{NEW_NAME}$2/' {} +
ls | grep new-name
new-name
more /tmp/rename.pl
#!/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 $_;