
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.