LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need help writing a renaming script (DDMMYYYY to YYYYMMDD format) (https://www.linuxquestions.org/questions/programming-9/need-help-writing-a-renaming-script-ddmmyyyy-to-yyyymmdd-format-782398/)

Zombie13 01-15-2010 09:33 AM

Need help writing a renaming script (DDMMYYYY to YYYYMMDD format)
 
I have a load of photos from my old Nokia phone that I need to rename. A few examples of the current format is "ddmmyyyy.jpg", "ddmmyyyy(001).jpg", "ddmmyyyy(002).jpg" etc, where ddmmyyyy is the date.

I need to rename hundreds of files in a single directory so that the first 8 digits are rearranged into "yyyymmdd.jpg", "yyyymmdd(001).jpg" format etc.

Even better if the output format could be "yyyy-mm-dd_(001).jpg"

Thanks in advance for any help you can give. :)

forrestt 01-15-2010 10:03 AM

You really want to steer away from having parentheses in your file names. A parenthesis is a special character to the shell. Better use something like "yyyy-mm-dd_001.jpg". That being said, try:

Code:

#!/bin/bash
for FILE in `ls *.jpg` ; do
    NEWFILE=`echo $FILE | sed -e 's/(\(...\))/_\1/' -e 's/\(..\)\(..\)\(....\)/\3-\2-\1/'`
    echo mv $FILE $NEWFILE
done

When you are confident that it will do what you want it to do, remove the 'echo' from the 3rd line and it will move the files.

HTH

Forrest

p.s. You may want to make sure these are backed up first just in case.

bigearsbilly 01-15-2010 06:05 PM

forrest, you don't need the ls

Code:

for file in *.jpg

forrestt 01-15-2010 06:40 PM

Sorry, that was before my second cup of coffee. You're totally right.

Forrest

ta0kira 01-15-2010 06:44 PM

Since the file names aren't exactly safe, maybe ls *.jpg | while read file; do..., plus double quotes around "$file". That should be done, anyway, just in case.
Kevin Barry

ta0kira 01-15-2010 06:56 PM

I use a regex-rename script (similar to rename found on Slackware) that checks to make sure the number of input files is no greater than the number of output files. For example, your regex might result in two files mapping to the same file, causing the first to be lost.
Code:

#!/bin/sh                                                                                                                                 
                                                                                                                                           
                                                                                                                                           
if [ $# -lt 3 ]; then                                                                                                                     
  echo "$0 [old pattern] [new text] [files...]" 1>&2                                                                                       
  exit 1                                                                                                                                   
fi                                                                                                                                         
                                                                                                                                           
                                                                                                                                           
old_pattern="$1"                                                                                                                           
new_text="$2"                                                                                                                             
shift 2                                                                                                                                   
                                                                                                                                           
                                                                                                                                           
revise_name()                                                                                                                             
{                                                                                                                                         
#  sed -E "s/$old_pattern/$new_text/"        #(use for FreeBSD)
  sed --posix -r "s/$old_pattern/$new_text/" #(use for GNU)
}


old_files="$( while [ $# -gt 0 ]; do
  echo "$1"
  shift
done | egrep "$pattern" | sort -u )"

old_count="$( echo "$old_files" | grep -c . )"


if [ "$old_count" -eq 0 ]; then
  echo "$0: no files match the given pattern! please check it and try again." 1>&2
  exit 1
fi


new_files="$( echo "$old_files" | revise_name )"
new_count="$( echo "$new_files" | sort -u | grep -c . )"


if [ "$old_count" -gt "$new_count" ]; then
  echo "$0: the current pattern would result in the loss of $(($old_count-$new_count)) files! please check it and try again." 1>&2
  exit 1
fi


echo "$old_files" | while read file; do
  new_file="$( echo "$file" | revise_name )"
  if [ "$new_file" != "$file" ]; then
    mv -v "$file" "$new_file"
  fi
done

I name this useful-rename.sh. In your case, you might try
Code:

useful-rename.sh '^([0-9]{2})([0-9]{2})([0-9]{4})' '\3\2\1' *jpg
Kevin Barry


All times are GMT -5. The time now is 10:08 AM.