Matt Posted January 8, 2008 Report Share Posted January 8, 2008 Hey, I'm looking for a bash script that can scan a directory for all .jpg files, and rename them with a number. I've got 220 images in a fold that I would like to rename 1.jpg through 220.jpg. I've tried using variables and arrays, but I don't know enough about the shell to get this going.Thanks,Matt Quote Link to post Share on other sites
Matt Posted January 8, 2008 Author Report Share Posted January 8, 2008 Nevermind, I found this script:http://lists.freebsd.org/pipermail/freebsd...ber/021729.html#!/bin/bash#----------------------------------------------------# Change the path above to point to the location on# your computer of either the Bourne shell (sh) or# the BASH (Bourne Again) shell (bash).## This shell script was written to "batch rename" files# (change the name of many files at once) in the# current working directory.# # For his personal use the author named this script# mvb (MV-Batch) in reference to the mv command# of *nix/Linux (which this script uses).## Written by: Steve Doonan, Portales, NM US# Email: xscd at xscd.com# Date: October, 2003#----------------------------------------------------if [ $# -eq 0 ] then cat << _EOF_--------------------------------------------------You did not specify a NEWNAME for the files.After the name of the command please entera SPACE followed by the name you would likeall the files in the current directory to berenamed to.--------------------------------------------------_EOF_ exitfiNEWNAME="$(echo "$1" | tr -Cs '[:alnum:]' '_')"cat << _EOF_-------------------------------------------------------Rename files to--> $NEWNAMECurrent directory--> $(pwd) Continue? (Press RETURN or ENTER) TO QUIT, type q (then press RETURN or ENTER) FOR INFORMATION, type i (then press RETURN or ENTER)-------------------------------------------------------_EOF_read CONTINUEcase $CONTINUEin [!i]* ) exit;; i ) cat << _EOF_----------------------------------------------------------------INFORMATIONThis shell script (Bourne or BASH) will RENAME all visible files(files that don't begin with a dot) in the current directory, toa name you specify, appending a numerical index to each filenameso that each is unique, and retaining the original filenameextension, if one exists.This script will NOT rename subdirectories or symbolic linkscontained within the current directory, nor will it descend intosubdirectories to rename files within them.If the script does not see what looks like an existing validFILENAME EXTENSION (3-4 characters following a dot at the endof a filename), it will ask for one. If you WANT to add afilename extension, just type 3 or 4 characters (i.e. jpg, txt,html), with or without a preceding dot--the script will providethe dot if you do not. If you do NOT want the filename to havean extension, just press RETURN or ENTER at that prompt withouttyping any characters, and no filename extension will be added.To QUIT this program at any time, press CONTROL-CTo CONTINUE, press RETURN or ENTER----------------------------------------------------------------_EOF_ read CONTINUE;;esacINDEX=0make_zero-padded_index_number (){INDEX=$(($INDEX + 1))INDEX_COUNT="$(echo "$INDEX" | wc -c)"PADDING_ZEROS="$(ls "$(pwd)" | wc -l | tr '[:digit:]' '0' | tr -d '[:space:]')"INDEX_ALPHANUMERIC="$(echo "${PADDING_ZEROS}${INDEX}" | cut -c$INDEX_COUNT-)"}for I in * do #----------------------------------------- # if file is NOT a directory or a link... #----------------------------------------- if [ -f "$I" -a ! -L "$I" ] then #----------------------------------------------- # if filename has a 3 or 4 character extension... #----------------------------------------------- if echo "$I" | grep "\.[^.0-9]\{3,4\}$" > /dev/null then #------------------------------------------------ # assign filename extension to variable EXTENSION #------------------------------------------------ EXTENSION="$(echo "$I" | sed 's/^.*\(\.[^.]*\)$/\1/')" #----------------------------------------------------- # otherwise, ask for a filename extension (or none)... #----------------------------------------------------- else echo "" echo '------------------------------------------------------------' echo "FILENAME: $I" echo "No (or improbable) filename extension found" echo "Enter new filename extension, or press RETURN or ENTER" echo -n "for no filename extension: " read NEW_EXTENSION #----------------------------------------------------------------- # cut the new extension (if any) down to no more than 4 characters #----------------------------------------------------------------- NEW_EXTENSION="$(echo "$NEW_EXTENSION" | sed 's/^\.*\(.\{0,4\}\).*$/\1/'| tr -C '[:alnum:]\12' '_' )" echo '------------------------------------------------------------' echo "" if [ -n "$NEW_EXTENSION" ] then EXTENSION=".${NEW_EXTENSION}" else EXTENSION='' fi fi #---------------------------------------------------------------------- # at this point, EXTENSION should be set correctly--an alphanumeric # index number is created (by the function make_zero-padded_index_number) # and the file is renamed (with a slightly different name if the computed # filename already exists in the current directory). #---------------------------------------------------------------------- make_zero-padded_index_number RENAME_TO="${NEWNAME}${INDEX_ALPHANUMERIC}${EXTENSION}" if [ -e "$RENAME_TO" ] then RENAME_TO="${NEWNAME}${INDEX_ALPHANUMERIC}a${EXTENSION}" fi chmod 664 "$I" mv -iv -- "$I" "$RENAME_TO" fi donecat << _EOF_--------------------------------------------------- The files have been renamed. Script exiting...---------------------------------------------------_EOF_ Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.