此命令重命名文件:
$ find $PWD -name "*.jpg" | awk '{c=$0; gsub("/", "_", c)}{system("mv -v " $0 " " c)}'
`/home/pic/Pulpit/test/stary/1.jpg' -> `_home_pic_Pulpit_test_stary_1.jpg'
我有一个测试文件:
$ ls
`[~!($%^_&*){.}\___"`]`1.jpg
.........
$ find -not -name .
./`[~!($%^_&*){.}\___"`]`1.jpg
我想更改文件名:
$ find -not -name . | awk '{c=$0; gsub("/", "_", c)}{system("mv -v " $0 " " c)}'
/bin/sh: Syntax error: word unexpected (expecting ")")
我想:
_home_pic_Pulpit_test_stary_`[~!($%^_&*){.}\___"`]`1.jpg
感谢您的帮助。
修改
这适用于'gawk',但它不适用于'mawk'。
gawk(作品):
$ find -not -name . | gawk '{
> c=$0;
> gsub( "/", "_", c );
> c = gensub( /([[:punct:]])/, "\\\\&", "g", c );
> $0 = gensub( /([[:punct:]])/, "\\\\&", "g", $0 );
> system("echo mv -v " $0 " " c)}
> '
mv -v ./`[~!($%^_&*){.}\___"`]`1.jpg ._`[~!($%^_&*){.}\___"`]`1.jpg
mawk(不起作用):
$ find -not -name . | mawk '{
> c=$0;
> gsub( "/", "_", c );
> c = gensub( /([[:punct:]])/, "\\\\&", "g", c );
> $0 = gensub( /([[:punct:]])/, "\\\\&", "g", $0 );
> system("echo mv -v " $0 " " c)}
> '
mawk: line 8: function gensub never defined
mawk: line 8: function gensub never defined
gawk(作品):
$ find -not -name . | gawk '{
> gsub( /[[:punct:]]/, "\\\\&" );
> c = $0;
> gsub( /\//, "_", c );
> system( "echo mv -v " $0 " " c ) }
> '
mv -v ./`[~!($%^_&*){.}\___"`]`1.jpg ._`[~!($%^_&*){.}\___"`]`1.jpg
mawk(不起作用):
$ find -not -name . | mawk '{
> gsub( /[[:punct:]]/, "\\\\&" );
> c = $0;
> gsub( /\//, "_", c );
> system( "echo mv -v " $0 " " c ) }
> '
/bin/sh: Syntax error: word unexpected (expecting ")")
我正在使用Ubuntu 10.10而且我安装了'mawk'。
如何在'mawk'上做到这一点?
编辑 - 1:
在另一个论坛上,我得到了mawk的解决方案。
find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")}'
答案 0 :(得分:1)
使用GNU awk
的一种方法。在执行mv
命令之前,我会转义所有标点字符。
find -not -name . | awk '{
c=$0;
gsub( "/", "_", c );
c = gensub( /([[:punct:]])/, "\\\\&", "g", c );
$0 = gensub( /([[:punct:]])/, "\\\\&", "g", $0 );
system("mv -v " $0 " " c)}
'
更新以添加awk
兼容命令:
find -not -name . | awk '{
gsub( /[[:punct:]]/, "\\\\&" );
c = $0;
gsub( /\//, "_", c );
system( "mv -v " $0 " " c ) }
'