[Leaplist] help with renaming script

Bryan J. Smith b.j.smith at ieee.org
Thu Dec 24 19:25:31 EST 2009


You could do this on the command line, for all files in the current directory
with a passed extension (e.g., *.jpg) ...

Prefix number (with trailing underscore, _):  
  let i=1 ; for f in *.jpg ; do mv $f ${i}_{f} ; let i=i+1 ; done

Suffix number (with leading underscore, _), just flip the order of "f" and "i" in
the mv (move) command:  
  let i=1 ; for f in *.jpg ; do mv $f ${f}_{i} ; let i=i+1 ; done

If you need to rename more than just "*.jpg" files, just add it to the list
(after the "in" portion), such as in the prefix version:  
  let i=1 ; for f in *.jpg *.JPG *.jpeg *.JPEG ; do mv $f ${i}_{f} ; let i=i+1 ; done

If you want to prefix and pad with zeros (for proper, alphanumeric ordering),
say six (6) digits total:    
  let i=1 ; for f in *.jpg ; do i0=`printf %06d $i` ; mv $f ${i0}_{f} ; let i=i+1 ; done

Let's break statements these down ...

First off, the semicolon (;) is like a line break in a script.  It lets you send multiple
commands on the command line.

Secondly, we have the numeric evaluations ... which should be obvious.
- let i=1
- let i=i+1

Third, there is the for loop, which is a bit more complicated ...

A)  "f" -- this is the designated variable used inside of each loop.  It is set to
one value each time it loops -- a value that is from the list of items.

B)  "*.jpg" (and others) -- this is the list, which is expanded by bash to be all
files that match the glob *.jpg -- like picofbryan.jpg.  Add more items after the
"in" but before the ";" and they are appended to the list -- like "*.JPG".

C)  The " ; do" and " ; done" portions -- the "loop" is between these two, "do"
is the start and "done" is the end.  The loop will take place on these two.
A "do" must follow the "for (variable) in (list)" portion, and if it occurs on the
same line, it must be preceeded by the semicolon (;), although it does _not_
need to be followed by a semicolon (;) for the first command, although one
must come before the "done."

Fourth, we have the actual "mv" (move/rename) statement:  
- Prefix:  mv $f ${i}_${f} 
- Suffix:  mv $f ${f}_${i}

These are substitutions.  The "$f" is set on each loop, and it is the current
filename taken from the list (which is expanded by bash from *.jpg).  The
"$i" is the numeric we've used earlier.  In bash, assignments (and let
statements) do _not_ require the leading dollar ($).  But in substitution,
they are required.  Assignments are those where you assign a value
(i.e., letter on the left of the = sign).  Substitutions are when you put them
in echo, printf, mv and other statements.

Furthermore, bash can get confused if the variable is not separated by
whitespace (e.g., space), so the curly braces ({}) are required to guarantee the
variable name.  I.e., the first "$f" does not require curly braces because there is
a space after the "mv" and before the renamed name.  But in the latter, we're
using "${i}_${f}" with curly braces because the "${i}" (prefixed number example
here) has a trailing underscore (_).  In all honestly, the latter "${f}" is probably not
required, but it never hurts to use curly braces (it always works in that case).

Lastly, one more command in the latter example with padded zeros (0):  
- i0=`printf %06d $i`

We use this $i0 and, correspondingly, ${i0} in the "mv" command because
we are padding the number with zeros (0).  We do this with a little trick.  The
print format (printf) command is a special type of echo/print statement.  It says
print with a special format.  We say "%06d" which means format item (%),
"decimal" (d), with six (6) characters, pad with leading zeros (0), and then
follow with the variable.  This command is then surrounded with backticks (`),
which then takes the "output" and puts it in the variable "i0".  Variables have
to start with a letter, but the 2nd and latter digits can be numbers.  I used
"i0" (i-sub-0) like your old math teacher did.  ;)

Study these commands and you'll see how they work.  Such commands can
be entered on the command line and are easy to pick up.  If you need to
renumber files that are not in the same directory, then we'll have to write
something a bit more complex.  ;)




----- Original Message ----
From: Ray Brunkow <ray at brunkow.ws>
To: This is the Leap Main List <leaplist at leap-cf.org>
Sent: Thu, December 24, 2009 5:54:59 PM
Subject: [Leaplist] help with renaming script

I do not know how to script.  I have a simple script that I got some help about a year back or so to replace spaces with _ but now i am in need of replacing entire file names with sequential numbers in order to get them to function properly with Dreamweaver to make a photo album of roughly 200M worth of pictures.  I really do not want to go and manually rename every single file.  that would just take way to long.

====================

#!/bin/bash
#
#       remove spaces in names of files and replace them with '_'
#
        function basic {
        for i in *
        do
                if [ -d "$i" ]
                then
                        cd "$i"
                        remove_space2.sh #name of this script from LQ member dive.  Must be in PATH or full path to script must be given.
                        cd ..
                fi
                tempa=$(echo "$i" | sed 's/ /_/g' | sed 's/_-_/-/g' | sed -e 's/\ -\ /-/g' | sed 's/__/_/g' \
                | sed "s#'##g" | sed "s#;##g" | sed "s#,##g")
                [[ "$i" != "$tempa" ]] && mv -v "$i" "$tempa"
        done
        }

        function cmd {
        for i in "$@"
        do
                tempa=$(echo "$i" | sed 's/ /_/g' | sed 's/_-_/-/g' | sed -e 's/\ -\ /-/g' | sed 's/__/_/g' \
                | sed "s#'##g" | sed "s#;##g" | sed "s#,##g")
                [[ "$i" != "$tempa" ]] && mv -v "$i" "$tempa"
                [[ ! -d "$tempa" ]] && break
                cd "$tempa"
                remove_space2.sh #name of this script from LQ member dive.  Must be in PATH or full path to script must be given.
                cd ..
        done
        }

        [[ "$@" != "" ]] && cmd "$@"
        [[ "$@" == "" ]] && basic

==============

is there a simple way to modify that script to do what i need?  no i did not code that.   the good folks over at LinuxQuestions.org helped with that one.

-- Raymond L. Brunkow
5th Degree Black Belt
Certified Instructor
Choong Sil Kwan TaekwonDo Federation


-- This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

_______________________________________________
Leaplist mailing list
Leaplist at leap-cf.org
http://lists.leap-cf.org/mailman/listinfo/leaplist


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the Leaplist mailing list