AWK - change the file name (special characters)

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have two files:



~/Pulpit/kot$ find . -name "*jpg" -printf "%fn"
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=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'



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



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









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 "%fn" 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














up vote
1
down vote

favorite












I have two files:



~/Pulpit/kot$ find . -name "*jpg" -printf "%fn"
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=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'



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



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









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 "%fn" 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












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have two files:



~/Pulpit/kot$ find . -name "*jpg" -printf "%fn"
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=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'



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



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









share|improve this question















I have two files:



~/Pulpit/kot$ find . -name "*jpg" -printf "%fn"
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=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'



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



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






awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 35 mins ago









MUmla

112




112










asked May 3 '12 at 10:42









nowy

84




84







  • 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 "%fn" 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












  • 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 "%fn" 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







2




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





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





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




I want to change "/" to "_" (as in gawk).
– nowy
May 3 '12 at 11:14




1




1




Are you sure / is in file name? Like in your output seems ./ is just added by find. Try find . -name "*jpg" -printf "%fn" instead to output it without ./ in the begin.
– rush
May 3 '12 at 11:40





Are you sure / is in file name? Like in your output seems ./ is just added by find. Try find . -name "*jpg" -printf "%fn" instead to output it without ./ in the begin.
– rush
May 3 '12 at 11:40





1




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




@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






active

oldest

votes

















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=$0gsub("/", "_")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 47".
    – Gilles
    May 3 '12 at 23:41











  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
    – nowy
    May 4 '12 at 7:39










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f37816%2fawk-change-the-file-name-special-characters%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








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=$0gsub("/", "_")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 47".
    – Gilles
    May 3 '12 at 23:41











  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
    – nowy
    May 4 '12 at 7:39














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=$0gsub("/", "_")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 47".
    – Gilles
    May 3 '12 at 23:41











  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
    – nowy
    May 4 '12 at 7:39












up vote
0
down vote



accepted







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












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












share|improve this answer



share|improve this answer










answered May 3 '12 at 14:16









glenn jackman

49.1k469106




49.1k469106











  • Your solution works. Thank you very much.find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")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 47".
    – Gilles
    May 3 '12 at 23:41











  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
    – nowy
    May 4 '12 at 7:39
















  • Your solution works. Thank you very much.find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")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 47".
    – Gilles
    May 3 '12 at 23:41











  • @Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
    – nowy
    May 4 '12 at 7:39















Your solution works. Thank you very much.find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'
– nowy
May 3 '12 at 14:47




Your solution works. Thank you very much.find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '"'"'" a "'"'"' '"'"'" $0 "'"'"'")'
– nowy
May 3 '12 at 14:47




1




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 47".
– Gilles
May 3 '12 at 23:41





@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 47".
– Gilles
May 3 '12 at 23:41













@Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
– nowy
May 4 '12 at 7:39




@Gilles Thank you for your help. This is a good way: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v '''" a "''' '''" $0 "''' ")' or: find $PWD -name "*.jpg" | mawk 'a=$0gsub("/", "_")system("mv -v 47" a "47 47" $0 "47 ")'
– nowy
May 4 '12 at 7:39

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f37816%2fawk-change-the-file-name-special-characters%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay