Manual Focus Lenses Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups  Rss feed   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Bash recursive file rename [for Linux nerds]

 
Post new topic   Reply to topic    Manual Focus Lenses Forum Index -> Photo Software and Digital Processing
View previous topic :: View next topic  
Author Message
Jieffe



Level 2

Joined: 04 Nov 2007
Posts: 277
Location: Brugelette, Belgium

PostPosted: Wed May 14, 2008 7:39 pm    Post subject: Bash recursive file rename [for Linux nerds] Reply with quote

I just passed the 10.000th picture and my Samsung has reset his counter ... which means that the next file to sg1s9999 is ... sg1s0001.

I decided to rename old files with 6 digits (allowing 999999 pictures, yes, I know, I'm a bit too enthusiastic), renaming sg1s0001 to jf010001 and, while at it, extracting jpeg preview from raw files.

Here is my bash script (largely inspired by another script found on the Net) :

Code:

#!/bin/sh

rename_recursive()
{
   for file in "$1"/*; do
      if [ -d "$file" ]; then
         rename_recursive "$file"
      else
         dirname=`dirname "$file"`
         basename=`basename "$file"`

         singlename=${basename%.*}
        beg_name=${basename:0:4}
         extension=${basename##*.}

    # If file begins in sg1s, meaning a Samsung GX1s file
    if [[ "$beg_name" == "sg1s" ]]; then

         # If file ends in JPG
       if [[ "$extension" == "jpg" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.jpg"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
      fi

           # If file ends in PEF (Raw Pentax file)
           if [[ "$extension" == "pef" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.pef"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
        /usr/local/sbin/dcraw -e -h -v -a $newfile   
           fi

         # If file ends in XCF (if I saved a raw with layers)
           if [[ "$extension" == "xcf" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.xcf"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi

         # If file ends in PNG (you never know :) )
           if [[ "$extension" == "png" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.png"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi


    fi
      fi 
   done
}

for dir in $@; do
   dirpath="$PWD/$dir"
   if [ -d "$dirpath" ]; then
      echo "Dir: $dirpath"
      rename_recursive "$dirpath"
   fi
done


It could be more elegant but it does the trick.
_________________
The gear I use : Samsung GX1s, a few Tamron, Pentax, East German and Russian lenses (Modded Canon G2 for IR).
Gimp & Ufraw on Linux Zenwalk
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
nemesis101



Level 2

Joined: 25 Mar 2008
Posts: 269
Location: Oregon USA

PostPosted: Fri May 30, 2008 8:35 pm    Post subject: Re: Bash recursive file rename [for Linux nerds] Reply with quote

As a fellow LInux nerd (I collect distributions like I collect MF lenses) Thanks!

Doug.


Jieffe wrote:
I just passed the 10.000th picture and my Samsung has reset his counter ... which means that the next file to sg1s9999 is ... sg1s0001.

I decided to rename old files with 6 digits (allowing 999999 pictures, yes, I know, I'm a bit too enthusiastic), renaming sg1s0001 to jf010001 and, while at it, extracting jpeg preview from raw files.

Here is my bash script (largely inspired by another script found on the Net) :

Code:

#!/bin/sh

rename_recursive()
{
   for file in "$1"/*; do
      if [ -d "$file" ]; then
         rename_recursive "$file"
      else
         dirname=`dirname "$file"`
         basename=`basename "$file"`

         singlename=${basename%.*}
        beg_name=${basename:0:4}
         extension=${basename##*.}

    # If file begins in sg1s, meaning a Samsung GX1s file
    if [[ "$beg_name" == "sg1s" ]]; then

         # If file ends in JPG
       if [[ "$extension" == "jpg" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.jpg"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
      fi

           # If file ends in PEF (Raw Pentax file)
           if [[ "$extension" == "pef" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.pef"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
        /usr/local/sbin/dcraw -e -h -v -a $newfile   
           fi

         # If file ends in XCF (if I saved a raw with layers)
           if [[ "$extension" == "xcf" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.xcf"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi

         # If file ends in PNG (you never know :) )
           if [[ "$extension" == "png" ]]; then
             newfile="$dirname/jf01${singlename:4:10}.png"
             if [[ "$file" != "$newfile" && ! -f "$newfile" ]]; then
                echo "Renaming '$file' to '$newfile'..."
                mv "$file" "$newfile"
             fi
           fi


    fi
      fi 
   done
}

for dir in $@; do
   dirpath="$PWD/$dir"
   if [ -d "$dirpath" ]; then
      echo "Dir: $dirpath"
      rename_recursive "$dirpath"
   fi
done


It could be more elegant but it does the trick.
Smile Smile
_________________
Lenses and cameras:
http://forum.mflenses.com/viewtopic,p,51339.html#51339
Photo sites:
www.pbase.com/nemesis101/
Back to top
View user's profile Send private message Visit poster's website
Jieffe



Level 2

Joined: 04 Nov 2007
Posts: 277
Location: Brugelette, Belgium

PostPosted: Thu Jun 05, 2008 1:26 pm    Post subject: Re: Bash recursive file rename [for Linux nerds] Reply with quote

nemesis101 wrote:
As a fellow LInux nerd (I collect distributions like I collect MF lenses)

I'm a slackware fan for years (just switched to Zenwalk, but hey, it's still slackware-ish). I managed to get rid of Windows Smile No more dual-boot since years !
_________________
The gear I use : Samsung GX1s, a few Tamron, Pentax, East German and Russian lenses (Modded Canon G2 for IR).
Gimp & Ufraw on Linux Zenwalk
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Manual Focus Lenses Forum Index -> Photo Software and Digital Processing All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group