LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-15-2010, 09:33 AM   #1
Zombie13
Member
 
Registered: Nov 2003
Posts: 70

Rep: Reputation: 15
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.
 
Old 01-15-2010, 10:03 AM   #2
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
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.

Last edited by forrestt; 01-15-2010 at 10:05 AM. Reason: added p.s.
 
Old 01-15-2010, 06:05 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
forrest, you don't need the ls

Code:
for file in *.jpg
 
Old 01-15-2010, 06:40 PM   #4
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

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

Forrest
 
Old 01-15-2010, 06:44 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 01-15-2010, 06:56 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 01-15-2010 at 06:59 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Simple Linux script to convert datetime format to UTC format shayno90 Linux - Newbie 10 10-09-2009 08:19 AM
find YYYYMMDD and YYYYMMDD difference kpachopoulos Programming 1 09-22-2007 10:37 AM
Need help in writing simple shell script to format names naren_0101bits Programming 3 08-14-2007 10:59 AM
Simple Renaming Script Boffy Programming 5 08-12-2005 09:07 AM
Renaming multiple files with the same format Shr00mBoXx Slackware 7 06-20-2004 07:22 PM


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

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration