0

I have a lot of files i've copied over from my iphone file system, to start with they were mp3 files, but app on iphone changed their names to some random staff which looks like:

1c03e04cc1bbfcb0c1237f57f1d0ae2e.mp3?extra=f7NhT68pNkmEbGA_I1WbVShXQ2E2gJAGBKSEyh3hf0hsbLB1cqnXDuepYA5ubcFm_B3KSsrXDuKVtWVAUh_MAPeFiEHXVdg

I only need to remove part of file name after mp3. Please give me a script - there are more than 600 files, and manually it is impossible.

Bato-Bair Tsyrenov
  • 1,174
  • 3
  • 17
  • 40
  • Try [mmv](http://ss64.com/bash/mmv.html) – Jens Jul 27 '14 at 08:20
  • @Jens i am very bad with terminal and don't think mmv would help. All it really needs is some kind of instr function. Like regexp_instr in oracle to get position where that question mark starts, and then get substring and rename. – Bato-Bair Tsyrenov Jul 27 '14 at 08:23
  • possible duplicate of [Rename multiple files in Unix](http://stackoverflow.com/questions/1086502/rename-multiple-files-in-unix) – buff Jul 27 '14 at 08:36
  • @buff i were looking for rename script using regex, but simple rename command solved it in no time – Bato-Bair Tsyrenov Jul 27 '14 at 08:39

2 Answers2

2

you can use rename command:

rename "s/mp3\?.*/mp3/" *.mp3*
Farhadix
  • 1,395
  • 2
  • 12
  • 25
1
#!/bin/bash
shopt -s nullglob
for F in *.mp3\?*; do
    echo mv -v -- "$F" "${F%%.mp3\?*}.mp3"
done

Save it to a script like script.sh then run as bash /path/to/script.sh in the directory where the files exist.

Remove echo when you find it correct already.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Since the OP has over 600 files, he probably should pipe his output to "less" so that he can quicker and easier see what is happening (without waiting for all the lines to scroll by). Example `./script.sh | less` when running is test with the **echo**. – L. D. James Jul 27 '14 at 08:32
  • @L.D.James Yes that can be an option. And better have `less -S`. – konsolebox Jul 27 '14 at 08:33
  • @konsolebox this script is nice and answer is well thought, preventing any damage to file names, thanks but i saw the other answer first and it worked with about 10 files which couldnot be renamed dew to same name error - dublicates – Bato-Bair Tsyrenov Jul 27 '14 at 08:42