0

I have a directory full of files like this:

Joe Schmidt Kapitel 10 - Teil 7.mp4&content_uid=359&hash=8353ee953b9bb79cebf58cc46d7dc085&random=190644109

A bunch of web stuff got kept on the end when downloading them, now I'd like to remove it like so:

Joe Schmidt Kapitel 10 - Teil 7.mp4

I'm on macOS, have the rename command, etc.

I tried the following but it didn't work so well:

$ rename --verbose 's/\&\w*//' *

'Joe Schmidt_ Kapitel 10 - Teil 7.mp4&content_uid=359&hash=8353ee953b9bb79cebf58cc46d7dc085&random=190644109' renamed to 'Joe Schmidt_ Kapitel 10 - Teil 7.mp4=359&hash=8353ee953b9bb79cebf58cc46d7dc085&random=190644109'

rename --verbose 's/\&\.*//' *

didn't work either.

And since there are multiple ampersands -- I want to kill everything after the first ampersand -- I can't even use cut:

for i in $(find . -type f)
do
mv "$i" `echo $i | cut -d "&" -f1`
done

produces:

mv: rename ./Joe to ./Joe: No such file or directory mv: rename Schmidt_ to Schmidt_: No such file or directory mv: rename Kapitel to Kapitel: No such file or directory mv: rename 10 to 10: No such file or directory mv: rename - to -: No such file or directory mv: rename Teil to Teil: No such file or directory mv: rename 7.mp4&content_uid=359&hash=8353ee953b9bb79cebf58cc46d7dc085&random=190644109 to 7.mp4: No such file or directory

0

Solved it with:

rename 's/&.*//' *
0

Not elegant, but should work. The magic is in the quotes :)

ls *\&* | (
  while read F
  do
    TGT="$(echo $F | cut -d'&' -f1)"
    echo mv "$F" "$TGT"
  done
)

Please remove the echo to arm this script.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.