chname - script in bash to change name of all files in dir

Discussions Regarding Software

Moderator: Moderators

chname - script in bash to change name of all files in dir

Postby Hakier » Fri Jan 25, 2008 9:21

I wrote it because I am collecting fotos of our parties and I wanted to know where it was taken and when so I decided to write a script renaming them to the format Place|Number_of_photo.extencion (ex. Forest|01.jpg)
So if I had to rename 100 fotos by my self i had to waste a lot of time. I know that there is software like this but i prefer my own :P

How to install? Just copy this script to /bin and change chmod to 755
If you want to read syntax info just write chname (if it isn't in /bin then you have to write /localization/chname )

What do you think about my script?
Any suggestions?

Code: Select all
#!/bin/bash
#
# ****************************************************************************
# *   Name: chname                                                           *
# *   Version: 1.0                                                           *
# *   Author: Hakier                                                          *
# *   E-mail: hakier@is.net.pl                                               *
# *                                                                          *
# *   This script is free software; you can redistribute it and/or modify    *
# *   it under the terms of the GNU General Public License as published by   *
# *   the Free Software Foundation; either version 2 of the License          *
# *                                                                          *
# ****************************************************************************

#<!-- If files location is known --!>
   if [ "${1}" != "" ]; then

      location="${1}"   #Location of files

      #<!-- Take down files list --!>
         cd "${location}"
         chmod 644 *
         files=(`ls "${location}"/ -F1`) #take down files list
         chmod 755 *
      #<!-- //Take down files list --!>
      
      #<!-- Making destination directory --!>
         if [ -d "${location}/copy" ]; then
            nr=0         #file id in table ${files}

         else
            nr=0         #file id in table ${files}
            mkdir "${location}/copy"   #making destination directory

         fi
      #<!-- //Making destination directory --!>
      
      #<!-- Numeration starts from this numer --!>
         if [ "${3}" != "" ]; then
            start="${3}"

         else
            start=1

         fi
      #<!-- //Numeration starts from this numer --!>

      #<!-- Number of cypher --!>
         if [ "${4}" != "" ]; then
            cyfr="${4}"

         else
            cypher=3

         fi

         temp=1

         while [ "${cypher}" -gt "1" ]; do
            cypher=$(( $cypher - 1 ))
            temp=$(( $temp * 10 ))
         done

         temp2=$temp
      #<!-- //Number of cypher --!>
      
      #<!-- Checking extension of file --!>
         if [ "${files[${nr}]##${files[${nr}]%%/}}" = "/" ]; then
            farther=2      #if it is directory

         elif [ "${files[${nr}]##*.}" != "" ]; then
            farther=1      #if it is a file

         else
            farther=0      #if there is nothing

         fi
      #<!-- //Checking extension of file --!>
      
      #<!-- Loop changing names of files --!>
         while [ "${farther}" != "0" ]; do
            if [ "${farther}" = "2" ]; then
               nr=$(( $nr + 1 ))   #go to the next file in table
            else
               #<!-- Changing name of files --!>
                  temp=$temp2
                  zero=''

                  while [ "${start}" -lt "${temp}" ]; do
                     temp=$(( $temp / 10 ))
                     zero=${zero}0
                  done

                     if [ ${files[${nr}]#*.} != ${files[${nr}]} ]; then
                        cp "${location}/${files[${nr}]}" "${location}/copy/${2}${zero}${start}.${files[${nr}]#*.}"
                        echo "Old name: ${files[${nr}]}  New name: ${2}${zero}${start}.${files[${nr}]#*.}"
                     else
                        cp "${location}/${files[${nr}]}" "${location}/copy/${2}${zero}${start}"
                        echo "Old name: ${files[${nr}]}  New name: ${2}${zero}${start}"
                     fi
               #<!-- Changing name of files --!>
         
               nr=$(( $nr + 1 ))   #go to the next file in table
               start=$(( $start +1 ))   
            fi
      
            #<!-- Checking extension of file --!>
               if [ "${files[${nr}]##${files[${nr}]%%/}}" = "/" ]; then
                  farther=2      #if it is directory
      
               elif [ "${files[${nr}]##*.}" != "" ]; then
                  farther=1      #if it is a file
      
               else
                  farther=0      #if there is nothing
      
               fi
            #<!-- //Checking extension of file --!>
         done
      #<!-- //Loop changing names of files --!>
#<!-- //If files location is known --!>

#<!-- If files location is unknown --!>
   else
      echo 'chname '\'location\'' '\'beginning of the name\'' '\'Numeration starts from this number\'' '\'Number of cypher\' #syntax information
      echo ''
      echo "location - location of files ex. '/media/disk/foto'"
      echo ''
      echo "beginning of the name - how the name of file should begin ex. 'Forest|2007|' => Forest|2007|001.jpg"
      echo ''
      echo "Numeration starts from this number - ex. 6 => Forest|2007|006.jpg"
      echo ''
      echo "Number of cypher - ex. 5 => Forest|2007|00006.jpg"
   fi
#<!-- If files location is unknown --!>
User avatar
Hakier
Baby Hen
 
Posts: 3
Joined: Sat Oct 20, 2007 20:06

Return to Software in General

Who is online

Users browsing this forum: No registered users and 1 guest