LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bulk rename all 'filename.mp3?NonsenseString' to 'filename.mp3' (https://www.linuxquestions.org/questions/linux-newbie-8/bulk-rename-all-%27filename-mp3-nonsensestring%27-to-%27filename-mp3%27-4175611512/)

iwtbf 08-07-2017 05:22 PM

Bulk rename all 'filename.mp3?NonsenseString' to 'filename.mp3'
 
Sometimes when I download podcasts with newsbeuter+podbeuter I get files of the form:

Code:

filename.mp3?NonsenseString
Not sure why this occurs but I would like correct this for all files in my podcast directory. I.e for all files in ~/podcasts with filenames not ending in mp3 or ogg do the truncation:

Code:

mv filename.mp3?NonsenseString filename.mp3
And similarly for ogg files. For simplicity I think it's OK to completely disregard weird upper-lower-case permutations of '.mp3' and '.ogg'.

What is the most straightforward way of doing this?

EDIT: Just wanted to add an example:

2017-07-12-symhc-bill-berry.mp3?awCollectionId=1002&awEpisodeId=927933

hydrurga 08-07-2017 05:36 PM

If you fancy using a GUI approach, then pyRenamer is a cool utility.

In this case, you would select the pattern of {X}? replaced by {1}.

That deletes all characters in selected filenames after the question mark, including the question mark, so would cover both oggs and mp3s. Note that as it is, it only deletes everything after the ultimate question mark in the filename, but unless you're expecting to find multiple question marks in filenames you should be fine.

TheEzekielProject 08-07-2017 05:41 PM

I'm not super experienced with sed but I think you could try something like
Code:

cd /path/to/your/folder
sed -i 's/.mp3*/.mp3/' *

.

I'm sure others will have other suggestions

Sefyir 08-07-2017 06:01 PM

You can try using the rename program. -n is "dry-run" mode.
This matches the "?" and everything after and renames it with nothing as demoed below

Code:

$ rename -v -n 's/[?].*//g' 201*
rename(2017-07-12-symhc-bill-berry.mp3?awCollectionId=1002&awEpisodeId=927933, 2017-07-12-symhc-bill-berry.mp3)
rename(2018-07-12-symhc-bill-berry.mp3?awCollectionId=1002&awEpisodeId=927933, 2018-07-12-symhc-bill-berry.mp3)


iwtbf 08-07-2017 06:12 PM

Quote:

Originally Posted by Sefyir (Post 5745721)
You can try using the rename program. -n is "dry-run" mode.
This matches the "?" and everything after and renames it with nothing as demoed below

Code:

$ rename -v -n 's/[?].*//g' 201*
rename(2017-07-12-symhc-bill-berry.mp3?awCollectionId=1002&awEpisodeId=927933, 2017-07-12-symhc-bill-berry.mp3)
rename(2018-07-12-symhc-bill-berry.mp3?awCollectionId=1002&awEpisodeId=927933, 2018-07-12-symhc-bill-berry.mp3)



Thank you so much, it worked like a charm. Highly appreciated!


All times are GMT -5. The time now is 02:52 AM.