1

I have two files:

~/Pulpit/kot$ find . -name "*jpg" -printf "%f\n"
1.jpg
`[~!($%^_&*){.}\___"`]`1.jpg

I want to change their name to:

_home_pic_Pulpit_kot_1.jpg

_home_pic_Pulpit_kot_`[~!($%^_&*){.}\___"`]`1.jpg

I use this command:

~/Pulpit/kot$ find $PWD -name "*.jpg" | mawk '{c=$0; gsub("/", "_", c)}{system("echo mv -v " $0 " " c)}'
mv -v /home/pic/Pulpit/kot/1.jpg _home_pic_Pulpit_kot_1.jpg
/bin/sh: Syntax error: word unexpected (expecting ")")

EDIT:

This command works.

find $PWD -name "*.jpg" -exec bash -c 'mv "$0" "${0//\//_}"' {} \;

How to do the same with mawk?

EDIT- 1:

This solution works:

find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '"'"'" a "'"'"'  '"'"'" $0 "'"'"'")}'

find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '\''" a "'\'' '\''" $0 "'\'' ")}'

find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v \047" a "\047 \047" $0 "\047 ")}'
  • 2
    Do you really have a file named ./`[~!($%^_&*){.}\___"`]`1.jpg? I am utterly horrified. I generally stick to the POSIX Portable Filename Character Set. What are you trying to do? – jw013 May 3 '12 at 11:01
  • Yes. I have a test file: See my edit. – nowy May 3 '12 at 11:06
  • I want to change "/" to "_" (as in gawk). – nowy May 3 '12 at 11:14
  • 1
    Are you sure / is in file name? Like in your output seems ./ is just added by find. Try find . -name "*jpg" -printf "%f\n" instead to output it without ./ in the begin. – rush May 3 '12 at 11:40
  • 1
    @nowy I'm a bit confused by what you are doing. / is never a valid character in a file name because it is the path separator. Replacing / in a path means moving the files to a completely directory, not just a simple file name transformation. Whereas ./file is rather ordinary, ._file is a dotfile / hidden file, whereas dir/file becomes dir_file. Is that really what you intend? – jw013 May 3 '12 at 12:05
0

You probably just need to put single quotes around the filename arguments to mv. It's ugly but...

change

system("echo mv -v " $0 " " c)

to

system("echo mv -v '"'"'" $0 "'"'"' '"'"'" c "'"'"'")
  • Your solution works. Thank you very much.find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")}' – nowy May 3 '12 at 14:47
  • 1
    @nowy If I understand what you're trying to do correctly, this isn't correct. Try with a ' in the file name. Also, that '"'"'" is horribly unreadable. I recommend '\''" or \047". – Gilles 'SO- stop being evil' May 3 '12 at 23:41
  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v '\''" a "'\'' '\''" $0 "'\'' ")}' or: find $PWD -name "*.jpg" | mawk 'a=$0{gsub("/", "_")}{system("mv -v \047" a "\047 \047" $0 "\047 ")}' – nowy May 4 '12 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.