1

此命令重命名文件:

$ 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 "'"'"'")}'
4

1 回答 1

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 ) }
'
于 2012-05-02T08:58:34.917 回答