Skip to main content

Change the end of multiple similar filenames [Resolved]

I have many files that look similar to this:

56.mp3?referredby=rss

What I want to do is remove the ?referredby=rss so they'll be like this:

56.mp3

How would I do this?


Asked April 21, 2017
Posted Under: Unix Linux
40 views
2 Answers

If you have Perl rename, it’s as easy as

rename 's/\?referredby=rss//' ./*referredby=rss

With util-linux rename:

rename '?referredby=rss' '' ./*referredby=rss

Answered April 21, 2017
 
That avoids issues with files whose names start with - (they would end being interpreted as options rather than files). – Stephen Kitt 16 mins ago
 CanDoerz  5 months ago
 
Why do you have to put the ./ in the second part of the first command? – Proletariat 24 mins ago
 CanDoerz  5 months ago

Aside from escaping the ? (which has special meaning in glob expressions) this is really no different from renaming any other files: so for example you could use a simple shell loop

for f in *.mp3\?referredby=rss; do mv -- "$f" "${f%\?*}"; done

where ${f%\?*} is a shell parameter expansion that removes the shortest trailing element matching \?*


Answered April 21, 2017
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA