Skip to main content

Raymii.org Logo (IEC resistor symbol) logo

Quis custodiet ipsos custodes?
Home | About | All pages | RSS Feed | Gopher

Bash script for massive rename to numbers

Published: 14-09-2008 | Author: 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/.

If you like this article, consider sponsoring me by trying out a Digital Ocean VPS. With this link you'll get $100 credit for 60 days). (referral link)

(done by: awsetbg -r '/usr/share/wallpapers' in a crontab.)

I have a lot of wallpapers, over 1500. awsetbg/feh has some problems with filenames and so once in a while I got an error message from feh,

This scripts renames all the jpg files in a folder to a number. Instead of having Beach.jpg, Clouds.jpg and Tux.jpg you will have 1.jpg, 2.jpg and 3.jpg.

Script:

#!/bin/bash
RAYMIIWALL=1

#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"
done
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.

Tags: bash , rename , snippets