This command renames a file:
$ 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'
I have a test file:
$ ls
`[~!($%^_&*){.}\___"`]`1.jpg
………
$ find -not -name .
./`[~!($%^_&*){.}\___"`]`1.jpg
I want to change the file name:
$ find -not -name . | awk '{c=$0; gsub("/", "_", c)}{system("mv -v " $0 " " c)}'
/bin/sh: Syntax error: word unexpected (expecting ")")
I want:
_home_pic_Pulpit_test_stary_`[~!($%^_&*){.}\___"`]`1.jpg
Thank you for your help.
EDIT:
This works to ‘gawk’, but it does not work on ‘mawk’.
gawk (works):
$ 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 (does not work):
$ 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 (works):
$ find -not -name . | gawk '{
> gsub( /[[:punct:]]/, "\\\\&" );
> c = $0;
> gsub( /\//, "_", c );
> system( "echo mv -v " $0 " " c ) }
> '
mv -v ./`[~!($%^_&*){.}\___"`]`1.jpg ._`[~!($%^_&*){.}\___"`]`1.jpg
mawk (does not work):
$ find -not -name . | mawk '{
> gsub( /[[:punct:]]/, "\\\\&" );
> c = $0;
> gsub( /\//, "_", c );
> system( "echo mv -v " $0 " " c ) }
> '
/bin/sh: Syntax error: word unexpected (expecting ")")
I’m using Ubuntu 10.10 and I have installed ‘mawk’.
How to do it on ‘mawk’?
EDIT – 1:
On another forum I got a solution for mawk.
find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")}'
One way using
GNU awk. I escape all punctuation characters before executing themvcommand.UPDATE to add an
awkcompatible command: