Recommended Posts

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

Link to post
Share on other sites

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 enter
a SPACE followed by the name you would like
all the files in the current directory to be
renamed to.
--------------------------------------------------

_EOF_
exit
fi

NEWNAME="$(echo "$1" | tr -Cs '[:alnum:]' '_')"

cat << _EOF_

-------------------------------------------------------
Rename files to--> $NEWNAME
Current 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 CONTINUE
case $CONTINUE
in
[!i]* ) exit;;
i ) cat << _EOF_

----------------------------------------------------------------
INFORMATION

This shell script (Bourne or BASH) will RENAME all visible files
(files that don't begin with a dot) in the current directory, to
a name you specify, appending a numerical index to each filename
so that each is unique, and retaining the original filename
extension, if one exists.

This script will NOT rename subdirectories or symbolic links
contained within the current directory, nor will it descend into
subdirectories to rename files within them.

If the script does not see what looks like an existing valid
FILENAME EXTENSION (3-4 characters following a dot at the end
of a filename), it will ask for one. If you WANT to add a
filename extension, just type 3 or 4 characters (i.e. jpg, txt,
html), with or without a preceding dot--the script will provide
the dot if you do not. If you do NOT want the filename to have
an extension, just press RETURN or ENTER at that prompt without
typing any characters, and no filename extension will be added.

To QUIT this program at any time, press CONTROL-C
To CONTINUE, press RETURN or ENTER
----------------------------------------------------------------

_EOF_
read CONTINUE;;
esac

INDEX=0

make_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
done
cat << _EOF_

---------------------------------------------------
The files have been renamed. Script exiting...
---------------------------------------------------

_EOF_

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...