
hello All, I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension. This will let me order by date when viewing in Image Viewer, and a relatively sane sorting of the filenames. There are something over 450 images in the directory that I wish to edit the names of, so better to get the PC to do the repetitive work. My shell programming, and the like, are rusty, what there is of such skills. I would appreciate some assistance and pointers. Regards, Mark Trickett

On Mon, 1 May 2017 08:18:03 PM Mark Trickett via luv-main wrote:
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension.
for n in ????????*jpg ; do DATE=$(echo $n|cut -c 5-8)$(echo $n|cut -c 3-4)$(echo $n|cut -c 1-2 echo touch -t${DATE}0000 $n echo mv -i $n new/)${DATE}$(echo $n|cut -c 9-80) done The above MIGHT do what you want. Note that I have it "echo" the commands not run them. So paste the above in the shell and see if the mv commands are to your liking. Also note that it's moving files to a directory named "new" to avoid double-processing. The touch command sets the correct date so you can sort it by date as well. Also you could expand that touch command to convert the 3 digit number of photos per day to hours and minutes if you want to have a strict date order work. Naturally this will start a contest of writing the best shell code to solve your problem. ;) -- My Main Blog http://etbe.coker.com.au/ My Documents Blog http://doc.coker.com.au/

On Mon, 1 May 2017 10:03:25 PM Russell Coker via luv-main wrote:
On Mon, 1 May 2017 08:18:03 PM Mark Trickett via luv-main wrote:
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension.
for n in ????????*jpg ; do DATE=$(echo $n|cut -c 5-8)$(echo $n|cut -c 3-4)$(echo $n|cut -c 1-2) echo touch -t${DATE}0000 $n echo mv -i $n new/)${DATE}$(echo $n|cut -c 9-80) done I missed a close bracket on my first try.
The above MIGHT do what you want. Note that I have it "echo" the commands not run them. So paste the above in the shell and see if the mv commands are to your liking. Also note that it's moving files to a directory named "new" to avoid double-processing.
The touch command sets the correct date so you can sort it by date as well.
Also you could expand that touch command to convert the 3 digit number of photos per day to hours and minutes if you want to have a strict date order work.
Naturally this will start a contest of writing the best shell code to solve your problem. ;)
-- My Main Blog http://etbe.coker.com.au/ My Documents Blog http://doc.coker.com.au/

On 1 May 2017 at 20:18, Mark Trickett via luv-main <luv-main@luv.asn.au> wrote:
hello All,
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension.
This will let me order by date when viewing in Image Viewer, and a relatively sane sorting of the filenames. There are something over 450 images in the directory that I wish to edit the names of, so better to get the PC to do the repetitive work.
My shell programming, and the like, are rusty, what there is of such skills. I would appreciate some assistance and pointers.
It is possible but somewhat fiddly to do this as a shell script. Personally I would not solve it that way even though I am comfortable with shell scripting. I recommend the perl rename tool. On debian you would install this package: https://packages.debian.org/jessie/rename This package contains a 'rename' command that accepts perl syntax and applies it to matching filenames. Your specification is mostly clear but does contains some ambiguity, for example: - "brackets", are they () or [] or {} ? - what "sequence number" does the second photo have? - is the "sequence number" inside a pair of brackets? To avoid this game of questions, it would help if you provide actual examples of the various before and after filenames, so that anyone tempted to offer any solution can see and test exactly the names you have, and want.

On 01.05.2017 20:18, Mark Trickett via luv-main wrote:
hello All,
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension.
This will let me order by date when viewing in Image Viewer, and a relatively sane sorting of the filenames. There are something over 450 images in the directory that I wish to edit the names of, so better to get the PC to do the repetitive work.
My shell programming, and the like, are rusty, what there is of such skills. I would appreciate some assistance and pointers.
Regards,
Mark Trickett _______________________________________________ luv-main mailing list luv-main@luv.asn.au https://lists.luv.asn.au/cgi-bin/mailman/listinfo/luv-main
Below is my answer to your problem although it probably will not suit without modifacation. I have a couple Of Nikons (a D700 and a D810) they produce filenames like DSC_0829.NEF. I wanted them to be in a Date and time format for the reasons you mentioned. I ended up with a filename like 20081106_132142-0829.png. I kept the cameras sequence number is the D700 is capable of around 6 frames per second, and this of course would produce identical file names for shots within the same second, The date and time comes from data within the image and is extracked by exiv2, using this garuntees the time is correct according to the camera. Cut here................................................. #! /bin/bash # this is a script to take the file names from the camera and rename them to Date and time of # creation # ls *.nef --color=never -l | cut -c 33-45,50-57 | tr '\040' '\137' #NAME=$(ls $1 --color=never -l | cut -c 33-45,50-57 | tr '\040' '\137') LIST=`ls --color=never *.nef` echo $LIST COUNTER=1 for i in $LIST do # echo $i # NAME=$(ls $i --color=never -l | cut -c 35-47,52-59 | tr '\040' '\137' | tr '\072' '\055') NAME1=$(ls $i --color=never -l | cut -c 52-59 | tr '\040' '\137' | tr -d '\072') NAME=$(exiv2 $i | grep timestamp | cut -c 19-37 | tr -d '\072' | tr '\040' '\137') echo $NAME-$NAME1 cp $i $NAME-$NAME1 done ------------------------------end cut------------------------------ Lindsay

Hi, On 01/05/17 20:18, Mark Trickett via luv-main wrote:
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. The problem is the naming, the first eight characters are the date, two digits for the day, two for the month then four for the year. I wish to reorder them to be year, month and then day. That portion of the filename is then followed by brackets with a three digit sequence number for the second and subsequent photos on the day. it would be good to add the brackets with three zeros when not present. That is followed by a period and the jpg extension.
This will let me order by date when viewing in Image Viewer, and a relatively sane sorting of the filenames. There are something over 450 images in the directory that I wish to edit the names of, so better to get the PC to do the repetitive work.
My shell programming, and the like, are rusty, what there is of such skills. I would appreciate some assistance and pointers.
Okay, here is my take: NB: the first two commented lines and the function it calls are to provide some test files .... $ cat photo-wrk.sh #!/bin/bash create_some_files() { declare -a old_filenames old_filenames=( '10062005.jpg' '10062005(121).jpg' '10062005(122).jpg' '10062005(123).jpg' ) for old_fn in "${old_filenames[@]}" do touch "${old_fn}" done } #create_some_files # for testing #exit # The ./ with the ls is there on purpose to make it safer # from potential globbing issues for old_fn in $(ls ./*.jpg) do # strip out extra ./ from filename old_fn=${old_fn:2} # get filename without suffix (file extension) sans_ext=$(basename -s ".jpg" "${old_fn}") # if filename without suffix is only 8 characters, # then it doesn't have a counter value, so give it 0 if [[ ${#sans_ext} -eq 8 ]] then counter=0 else counter=${sans_ext:9:3} fi # format the counter as 3 digits counterx=$(printf "%03d" "${counter}") # Get date in seconds from epoc # - using old file name format of DDMMYYYY ab=$(date -d "${old_fn:4:4}-${old_fn:2:2}-${old_fn:0:2}" +%s) # Add day counter for photos in seconds ab=$((ab + counter)) new_fn=$(date +%Y%m%d -d "@${ab}")"(${counterx}).jpg" mv "${old_fn}" ./new_names/"${new_fn}" touch -d "@${ab}" "./new_names/${new_fn}" done ls -l --full-time "./new_names/" Cheers A.

On Tue, 2 May 2017, Andrew McGlashan wrote:
# The ./ with the ls is there on purpose to make it safer # from potential globbing issues for old_fn in $(ls ./*.jpg)
Just stop it right now! Stop doing that! Just don't! `for old_fn in *.jpg` is all you need, all you want. Then make sure "$old_fn" is properly quoted elsewhere. http://porkmail.org/era/unix/award.html -- Tim Connors

On 4 May 2017 at 10:38, Tim Connors via luv-main <luv-main@luv.asn.au> wrote:
On Tue, 2 May 2017, Andrew McGlashan wrote:
# The ./ with the ls is there on purpose to make it safer # from potential globbing issues for old_fn in $(ls ./*.jpg)
Just stop it right now! Stop doing that!
Just don't!
`for old_fn in *.jpg` is all you need, all you want. Then make sure "$old_fn" is properly quoted elsewhere.
Yep, good advice. For more information, see this wonderful and comprehensive documentation: http://mywiki.wooledge.org/BashPitfalls#for_i_in_.24.28ls_.2A.mp3.29 ( in case that link does not work, it is to Pitfall #1 at http://mywiki.wooledge.org/BashPitfalls )

On 04/05/17 10:38, Tim Connors via luv-main wrote:
On Tue, 2 May 2017, Andrew McGlashan wrote:
# The ./ with the ls is there on purpose to make it safer # from potential globbing issues for old_fn in $(ls ./*.jpg)
Just stop it right now! Stop doing that!
Just don't!
`for old_fn in *.jpg` is all you need, all you want. Then make sure "$old_fn" is properly quoted elsewhere.
That's okay, so long as there is at least one target file, otherwise it fails. I've added a test before now and dropped the ls in the for. There was another minor change, formatting the number to be 3 digits with printf isn't necessary in the use case. $ cat photo-wrk.sh #!/bin/bash set -u create_some_files() { declare -a old_filenames old_filenames=( '10062005.jpg' '10062005(121).jpg' '10062005(122).jpg' '10062005(123).jpg' ) for old_fn in "${old_filenames[@]}" do touch "${old_fn}" done } #create_some_files # for testing #exit ls ./*.jpg &>/dev/null || { echo no jpg files to process exit } # create new_names dir if it doesn't exist [[ -d ./new_names ]] || { mkdir new_names } for old_fn in *.jpg do # if filename has only 12 characters (including .jpg) # then it doesn't have a counter value, so give it 0 if [[ ${#old_fn} -eq 12 ]] then counterx=000 else # if it has a counter, it is supposed to already have 3 digits counterx=${old_fn:9:3} fi # Get date in seconds from epoc # - using old file name format of DDMMYYYY ab=$(date -d "${old_fn:4:4}-${old_fn:2:2}-${old_fn:0:2}" +%s) # Add day counter for photos in seconds ab=$((ab + counterx)) new_fn=$(date +%Y%m%d -d "@${ab}")"(${counterx}).jpg" mv "${old_fn}" ./new_names/"${new_fn}" touch -d "@${ab}" "./new_names/${new_fn}" done ls -l --full-time "./new_names/" Cheers A.

On Thu, May 04, 2017 at 02:42:00PM +1000, Andrew McGlashan wrote:
That's okay, so long as there is at least one target file, otherwise it fails.
I've added a test before now and dropped the ls in the for.
or you could just set nullglob in the script.
From the bash man page:
nullglob If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves. e.g. $ for i in *.doesnotexist ; do echo $i ; done *.doesnotexist $ shopt -s nullglob $ for i in *.doesnotexist ; do echo $i ; done $ Note that this only affects interpretation of glob patterns, not fixed strings. so 'for i in 1 2 3 4 5; ...' still works as expected because there aren't any glob/wildcard characters in it. you can put the nullglob setting and for the loop in a different scope (e.g. a sub-shell or a function) if you need to avoid nullglob having unwanted side-effects on other parts of the script. but, really, why write a 58 line shell script when a oneliner rename command (or 2 or 3 lines when reformatted for readability) is all that's needed? or one of the many similar utilites to do this extremely common task (most of which are both harder to use and less functional than the perl rename tool). craig ps: the previously mentioned Bash Pitfalls and the Bash FAQ at the same site are, IMO, **ESSENTIAL** reading for anyone wanting or needing to write bash scripts. also useful for other sh dialects. http://mywiki.wooledge.org/BashPitfalls http://mywiki.wooledge.org/BashFAQ I also highly recommend the Unix & Linux Stack Exchange site for anything to do with shell, awk, sed, etc scripting. Searching there will almost certainly find good solutions for whatever you're trying to do and if not, you can always ask your own question and get a good answer in short order. https://unix.stackexchange.com/ -- craig sanders <cas@taz.net.au>

Hi, On 05/05/17 14:34, Craig Sanders via luv-main wrote:
On Thu, May 04, 2017 at 02:42:00PM +1000, Andrew McGlashan wrote: or you could just set nullglob in the script.
Okay.
but, really, why write a 58 line shell script when a oneliner rename command (or 2 or 3 lines when reformatted for readability) is all that's needed?
Yes, but most of my script was to setup the test and show comments; the actual lines that did the work were not significant. The script was easy to understand with just a little study and I'm sure it gives Mark exactly what he actually needs without complicating anything with regex foo. And what's more, I did interpret what Mark had wanted quite well from his initial description, his later ls output showed files that would work perfectly well with my script. Cheers A.

On 1 May 2017, Mark Trickett via luv-main typed:
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. I wish to reorder them to be year, month and then day.
An alternative to filename based shell scripts and the rename command is to use jhead to rename them based on the date and time embedded in the exif jpeg header. For example: jhead -n'%Y%m%d-%H%M%S(%03i)' *.jpg Will rename files matched by *.jpg to the format YYYYMMDD-HHMMSS(00#).jpg This will only work if the files have date and time information in the exif header (every digital camera I've owned does this), and this date and time on are correct. It has the advantage that you can easily incorporate the time in the new filename. Regards, Kim

Hello Kim, and others, On 5/7/17, Kim Oldfield <luv@oldfield.wattle.id.au> wrote:
On 1 May 2017, Mark Trickett via luv-main typed:
I have photos from my mobile phone, an old Nokia 6021, and readily get them onto the PC. I wish to reorder them to be year, month and then day.
An alternative to filename based shell scripts and the rename command is to use jhead to rename them based on the date and time embedded in the exif jpeg header.
There were a number of suggestions, Russell and others came up with shell scripts, which have generic merit. i think that the one of Russell's that I tried had a small hiccup with unmatched brackets, and not quite sure where the opening one should have been. Craig came up with the perl based rename command, and his example worked a treat, and again is flexible for more than just photos. Your contribution is also very instructive, it has value in that it does not depend on the filenames having any commonality. I rate all of these as very useful and informative. Now I have another small question, i want to make copies of selected photos, but with a maximum size limit. I do have, and use, convert. I scan documents, and use convert to put the right way up, and convert the native scan format to jpeg. For the photos, I want to make them less than 250Mb. I will reread the man page, but there are so many options, and quite the implications.... It reminds me of trying to set up Xwindows in the earlier days, having to know the implications of the dot clock and the like, especially if you did not have the monitor details.
For example:
jhead -n'%Y%m%d-%H%M%S(%03i)' *.jpg
Will rename files matched by *.jpg to the format YYYYMMDD-HHMMSS(00#).jpg
Again, thanks to everybody. Regards, Mark Trickett
participants (8)
-
Andrew McGlashan
-
Craig Sanders
-
David
-
Kim Oldfield
-
Mark Trickett
-
Russell Coker
-
Tim Connors
-
zlinew9@virginbroadband.com.au