Rename files which have $’r at end of file name (including recursive in folders) without using rename

Posted on

Problem :

I have a ton of folders with jpg files in on a linux system. However I accidently moved them using a sh file that i made in windows.

Therefore instead of the files and structure being like this if running a ls command:

media/mugs/mug1.jpg
media/mugs/mug2.jpg
media/mugs/mug3.jpg
media/plates/plate1.jpg
media/plates/plate2.jpg
media/plates/plate3.jpg

Instead if I run a ls command, they show the following with a ‘ at the beginning and ‘$’r’ at the end:

media/mugs/'mug1.jpg'$'r'
media/mugs/'mug2.jpg'$'r'
media/mugs/'mug3.jpg'$'r'
media/plates/'plate1.jpg'$'r'
media/plates/'plate2.jpg'$'r'
media/plates/'plate3.jpg'$'r'

I do not have access to install programs, and therefore rename is not installed, I therefore only have access to built in ones such as find and mv

Much appreciated

Solution :

Test before using.

find media -name '*'$'r' -print0 |
while IFS= read -r -d '' file
do    mv "$file" "${file%?}"
done

See man bash section Parameter Expansion for the syntax ${parameter%word} to remove the last character, and help read for the -r -d '' options to read the filenames ending