1

I have a folder where I have pictures in .jpg named name_surname.jpg

I made a loop :

for f in *.jpg;do
instructions...
done

I wish I rempacer the _ with spaces. I know there is the tr command or with sed regexp. But in this case I do not know how to thank you.

Note that I do not wish to display the names of pictures but recover them(in a var ?) for use again later

Digger
  • 17
  • 6

2 Answers2

3

here the simple version

and here the extended one:

#!/bin/bash
for foo in *.jpg; do
bar="${foo//_/ }"
mv "$foo" "$bar"
done
Community
  • 1
  • 1
kindaleek
  • 81
  • 1
  • 6
  • Quite right. I wish more accurate timestamps were available, so we could know who got to this first. – Charles Duffy Mar 22 '15 at 17:20
  • hehe, i think i was 4min quicker but did an edit there with the bar-variable ;D thanks for the upvote built-in rules ! – kindaleek Mar 22 '15 at 17:24
  • Looks like you did indeed get there first (though by seconds, not minutes); deleting my answer. – Charles Duffy Mar 22 '15 at 17:25
  • @CharlesDuffy i've tried your answer, you've delated.its work but when i try to open image i've a error Image file misinterpretation JPEG (Improper call to JPEG library in state 200) any idea ? – Digger Mar 22 '15 at 17:27
  • @Digger, I'm guessing that you tested the answer given by SMA first? That one corrupted your files. They're still corrupted. – Charles Duffy Mar 22 '15 at 17:27
  • @Digger, ...that said, SMA's answer would have made backup files named ending in `.bak`, so you have uncorrupted versions to restore from. – Charles Duffy Mar 22 '15 at 17:28
-1
new_f=`echo $f | sed -e 's/\ /_/'`
mv $f $new_f

s in sed essentially substitutes text in between the first /'s with the text in the second /'s. In your case, replace spaces with underscores.

Anthony
  • 3,492
  • 1
  • 14
  • 9
  • The question is asking for the opposite, to replace the underscores with spaces. You may want to flip that around. – Sam Hanley Mar 22 '15 at 17:17
  • Thats tell me :mv: "name_surname.jpg" and "name_surname.jpg" identify the same file – Digger Mar 22 '15 at 17:20
  • This is also buggy -- it doesn't correctly handle filenames with tab characters or runs of spaces. – Charles Duffy Mar 22 '15 at 17:21
  • @Digger, it tells you that because it's doing the replacement in the wrong direction, but that's easy to fix. The bigger issue is the quoting (and its impact on correctness with unusual filenames), and the inefficiency of using `sed`. – Charles Duffy Mar 22 '15 at 17:21