Sunday, August 12, 2018

macos - Batch rename file to remove everything after the &

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


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...