Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 ")")

Thank you for your help.

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

Thank you all for your help.

share|improve this question
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

1 Answer 1

up vote 0 down vote accepted

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 "'"'"'")
share|improve this answer
    
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 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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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