Skip to main content

Bash script for massive rename to numbers

14-09-2008 | Remy van Elst | Text only version of this article

Table of Contents

And here is another nice little script for a problem I have.
I use awesome as window manager, and in my rc.lua file i've setup some nice tweaks so that my wallpaper changes every 5 minutes to a new one randomly chosen from /usr/share/wallpapers/.
(done by: awsetbg -r '/usr/share/wallpapers' on a crontab.)
But I have a lot of wallpapers, now my collection has over 1500. But awsetbg/feh has some problems with filenames and so once in a while I got this annoying message from feh, and I hate annoying messages.
So I fixed it. With a nice little bash script. It renames all the jpg files to a number. Instead of having Beach.jpg, Clouds.jpg and Tux.jpeg you will then have 1.jpg, 2.jpg and 3.jpg.



#convert everything to lowercase - you need the rename command.
rename 'y/A-Z/a-z/' *

for i in {*.jpg,*.jpeg}; 
    do mv -f -v "$i" "$RAYMIIWALL.jpg";
    let "RAYMIIWALL += 1"
exit 0

If you get an error like:

mv: cannot stat `.jpeg': No such file or directory* then it means that there are no jpeg files to rename. And remember, all on your own risk ;).

Tags: bash  rename  snippets