Results 1 to 3 of 3
Hi!
I have tried searching for any solution without any luck.
I have about 11 000 jpg images that is named like this:
image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)
...
- 03-07-2011 #1Just Joined!
- Join Date
- Mar 2011
- Posts
- 1
Batch rename
Hi!
I have tried searching for any solution without any luck.
I have about 11 000 jpg images that is named like this:
image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)
image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)
All of these files have correct file system created/modified date (meaning its the same as my timestamp in the file names).
How do I rename all the files like this:
image000001.jpg
image000002.jpg
image000003.jpg
...sorted by date?
I would very much appreciate any help.
- 03-09-2011 #2Just Joined!
- Join Date
- May 2008
- Posts
- 6
hmmm found no easy way to do this.
Maybe this will help you:
#####################
#/bin/bash
value=0001
for i in `ls /directory/*.jpg -tr`; do ##you can add R to make it recursive, of course
if [ $value -lt 10 ]; then
new_value=00000$value
elif [ $value -lt 100 ]; then
new_value=0000$value
elif [ $value -lt 1000 ]; then
new_value=000$value
elif [ $value -lt 10000 ]; then
new_value=00$value
elif [ $value -lt 100000 ]; then
new_value=0$value
else
new_value=$value;
fi
mv $i image$new_value.jpg
value=$(($value+1))
done
#####################
- 03-09-2011 #3Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
This script generates commands to rename the files:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate rename of inconveniently-named files. # Utility functions: print-as-echo, print-line-with-visual-space. pe() { for i;do printf "%s" "$i";done; printf "\n"; } pl() { pe;pe "-----" ;pe "$*"; } # Context with local commands. [[ -f ~/bin/context ]] && . context sed paste sort seq date FILE=${1-data1} pl " Sample data file $FILE:" cat $FILE pl " Strings denoting seconds since 1970:" # Remove some bad characters from date string, # remove string "at" from date, # remove all data *except* the date string, # feed into GNU date to get time since epoch (1970) pe sed -e 's/[.,]//g' -e 's/at//' $FILE | tee t1 | sed -e 's/^.* (//' -e 's/)//' | tee t2 | date +'%s' --file - | tee t3 pl " Epoch time pasted in front of file names:" # put the time since epoch to front of file name, # sort numerically. pe paste t3 $FILE | sort -k1,1n | tee t4 pl " Move commands, to be saved and executed later:" # extract the original file name, now in order by date, # adding a "move" command string at the front, # create a file of new names as in requirements, # write a series of move commands, to be executed later after # correctness for situation has been verified. pe # to files named like " image000001.jpg" cut -f2 t4| sed -e 's/^/"/' -e 's/$/"/' -e 's/^/mv /' > t5 n=$( wc -l < t5 ) printf "image%06d.jpg\n" $( seq 1 $n ) > t6 paste -d" " t5 t6
See comments and man pages for details.Code:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0.7 (lenny) GNU bash 3.2.39 GNU sed version 4.1.5 paste (GNU coreutils) 6.10 sort (GNU coreutils) 6.10 seq (GNU coreutils) 6.10 date (GNU coreutils) 6.10 ----- Sample data file data1: image-250211-142301.jpg (25. february 2011, at 02:23:01 PM) image-240211-132301.jpg (24. february 2011, at 01:23:01 PM) ----- Strings denoting seconds since 1970: 1298665381 1298575381 ----- Epoch time pasted in front of file names: 1298575381 image-240211-132301.jpg (24. february 2011, at 01:23:01 PM) 1298665381 image-250211-142301.jpg (25. february 2011, at 02:23:01 PM) ----- Move commands, to be saved and executed later: mv "image-240211-132301.jpg (24. february 2011, at 01:23:01 PM)" image000001.jpg mv "image-250211-142301.jpg (25. february 2011, at 02:23:01 PM)" image000002.jpg
It's possible that the mtime of the files could be used to make things easier -- say with command stat, but I didn't / don't have those files ... cheers, drlWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote