RALPH BURAIMO
RALPH BURAIMO

Reputation: 15

How to bulk rename files and remove additional characters after extension in linux

I have too many files urls in a txt file that i've downloaded using this bash command

awk '{url=$1; $1="";{sub(/ /,"");out=url" -O \""$0"\""}; print out}' file.txt | xargs -L 1 wget

this bash command above is supposed to get each file url and name and then rename them while downloading with the original name that is listed in front of each file url , the txt file containing those urls and their respective names is organized as follows:

http://example.com/1425233/47188.mp4 Abaixo de Zero (2021).mp4
http://example.com/1425233/47185.mp4 A Sentinela (2021).mp4
http://example.com/1425233/46849.mp4 Eu Me Importo (2021).mp4
http://example.com/1425233/47933.mp4 Loucura de Amor (2021).mp4

Well done, until this point everything is working correctly, the problem now is that when the download finishes, when i put the ls command in the terminal each file is presented with the title looking like this, all of them are looking like this:

'Abaixo de Zero (2021).mp4'$'\r'
'A Sentinela (2021).mp4'$'\r'
'Eu Me Importo (2021).mp4'$'\r'
'Loucura de Amor (2021).mp4'$'\r'

so this problem is causing the video not to play anymore, but when i rename manually to mp4 again then the '$'\r' string disappears and the video is able to play...

So having said that i want a linux command that can bulk rename all of the files and remove this '$'\r' string...

And then when listing in terminal the files would only be:

'Abaixo de Zero (2021).mp4'
'A Sentinela (2021).mp4'
'Eu Me Importo (2021).mp4'
'Loucura de Amor (2021).mp4'

thanks, any help would be appreciated.

Upvotes: 1

Views: 571

Answers (2)

tshiono
tshiono

Reputation: 22012

If you want to rename the existing files, please try:

#!/bin/bash

for f in *mp4$'\r'; do
    mv -- "$f" "${f%$'\r'}"
done

It assumes the *mp4 files are located in the same directory. If you want to fix the filenames in the current directory recursively, please try:

#!/bin/bash

shopt -s globstar
for f in **/*mp4$'\r'; do
    mv -- "$f" "${f%$'\r'}"
done

BTW if you want to fix the filenames downloading from now on, modify your awk command as:

awk '{url=$1; $1="";{sub(/ /,""); sub(/\r$/, "");out=url" -O \""$0"\""}; print out}' file.txt | xargs -L 1 wget

It removes the trailing CR characters out of the names in file.txt then passes the corrected filenames to wget.

Upvotes: 1

Roman Spiak
Roman Spiak

Reputation: 176

you can take an advantage of bash string manipulation and do a move of the original file to a file sub-string like so:

$ touch normal_file_extra
$ fileVar=normal_file_extra
$ echo ${fileVar::-6}
normal_file
$ mv $fileVar ${fileVar::-6}
# previous command substract 6 chars from the original file
$ ls
normal_file

Upvotes: 0

Related Questions