Wednesday, May 8, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 24606  / 3 Years ago, tue, july 6, 2021, 4:17:45

I am trying to rename all files in a folder replacing underscores with spaces.



i.e. this_is_a_test --> this is a test


but somehow I'm messing up the quoting



> for file in * ; do echo mv -v $file $(echo $file | sed 's/_/ /g') ; done
mv -v this_is_a_test this is a test


that looks OK, but if I remove the 'echo' mv complains as if the backslashes were removed



> for file in * ; do mv -v $file $(echo $file | sed 's/_/ /g') ; done
mv: target ‘test’ is not a directory


Can someone point out the error of my ways?


More From » command-line

 Answers
1

There is a minor mistake. Use "$newfile" instead of only $newfile. You need to use "



Here is the correct one.



for file in * ; do mv -v "$file" "$(echo $file | sed 's/_/ /g')" ; done


If you have filename this_is_a_test it will rename file to this is a test.



In case if you want to rename the file to this is a test. Use the code below,



for file in * ; do mv -v "$file" "$(echo $file | sed 's/_/ /g')" ; done


It is a good practise to use variables inside "" while you writing good shell script.


[#27980] Wednesday, July 7, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
donurp

Total Points: 328
Total Questions: 128
Total Answers: 123

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
donurp questions
;