this post was submitted on 18 Jan 2026
6 points (100.0% liked)

Linux

11137 readers
962 users here now

A community for everything relating to the GNU/Linux operating system (except the memes!)

Also, check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS
 

Situation: My partner and I backup our photos from our phones to a shared location. We have the same phone, so the files are named the same way however we do not like the naming scheme our phones use for the files. We have been renaming them manually so far but really getting tired of it and I know it could be achieved with a script.

Goal: Have a script that runs either on demand or on a cron schedule. The script detects files in our designated directories that are not yet renamed, and then renames them to our preferred format. It needs to:

  • remove some letters (e.g. "IMG" or "VID") which I am thinking can also be what helps the script know if it's already been renamed or not.
  • It then needs to insert hyphens (e.g. "20260118" into "2026-01-18").
  • It then needs to remove excess digits at the end of the file name.
  • It then needs to enumerate the files if there are duplicates (e.g. "2026-01-18-1" and "2026-01-18-2")

I have some very basic script writing knowledge and linux experience, and am eager to learn more. I'm not looking for anyone to completely write this script for me but very interested in anyone's suggestions for commands, methods, tips etc that I should look into.

For example, I believe the mv command can accomplish most of what I want to do here but I don't know if that's the most efficient or best way to do so. I'm also not 100% sure where to begin with the inserting hyphens at specific points in the existing name, or how to find files that still haven't been renamed.

Cheers!

top 9 comments
sorted by: hot top controversial new old
[–] cerement@slrpnk.net 3 points 40 minutes ago* (last edited 35 minutes ago)

a more obscure option is exiftool – and documention

  • setup an input folder and a destination folder beforehand
  • copy photos from phone to input folder
  • bonus: decide on separate suffix or initials for each of you
exiftool -progress -d <destination-folder>/%Y%m%d-%H%M%S-<suffix>.%%le "-filename<CreateDate" <input-folder>

    -progress
        verbose and shows an [x/y] progress
    -d <destination-folder>/%Y%m%d-%H%M%S-<suffix>.%%le
        renames to YYYYmmdd-HHMMSS-sfx.ext format
        and lowercases the extension
    "-filename<CreateDate"
        uses the image creation date (from EXIF metadata) for the above naming scheme
[–] traceur402@lemmy.blahaj.zone 4 points 10 hours ago

there's a utility named rename that will rename based on regular expressions. I think all the things you listed can be done by making a suitable regexp for each case

[–] AnimusExMachina@programming.dev 3 points 11 hours ago* (last edited 11 hours ago) (1 children)

I'm sure there is a great bash option but my bash is rubbish.

A quick python script would work.

From the datetime module you could use fromisoformat() method to get the date then use the strftime() to get your Year-month-day format. All this after reading the directories contents with the os.listdir() or os.walk() loops. You could parse the filenames for IMG/VID using if "VID" in file_name logic. You could strip the date from the file_name before using datetime with string slicing. Lastly you can use the os.rename() method to rename the file.

These steps are a bit out of order. But the ingredients are there. With a bit of search engine magic you should be able to work up a script. I would set up a testing folder and test it well before using your script on things you don't have a back up of.

[–] Diplomjodler3@lemmy.world 1 points 10 hours ago* (last edited 10 hours ago)

pathlib is your friend here.

from pathlib import Path

my_dir = Path('/home/myuser/photos')
new_pics = my_dir.glob('IMG*.jpeg')

This should give you a list of all JPEG files that start with "IMG".

[–] kiri@ani.social 3 points 11 hours ago* (last edited 11 hours ago)

When I started learning scripting, some internet guy recomended me this resource: https://mywiki.wooledge.org/ I really like it, espescially FAQ. I guess you would like Parameter Expansion chapter.

[–] INeedMana@piefed.zip 3 points 11 hours ago (1 children)

Take a look into regular expressions - sed or awk. What you wrote would go something like this

#!/usr/bin/env bash  
for F in *; do  
  NEWNAME="`echo $F|sed 's/\(....\)\(..\)\(\).*\.\(...\)$/\1-\2-\3.\4/'`"  
  if [ -f "$DESTINATION"/$"NEWNAME" ]; then  
    NEWNAME="${NEWNAME}_1"  
  fi  
  #mv "$F" "$DESTINATION/$NEWNAME"  
  #let's debug instead  
  echo "$NEWNAME"  
done  
[–] Scoopta@programming.dev 2 points 9 hours ago (1 children)

If you're going to regex why not use rename instead of writing a script for it?

[–] INeedMana@piefed.zip 2 points 8 hours ago

OP's question was for a script. One advantage of script instead of one-liner is that you have it saved somewhere instead of searching through history. Also, the requirement

enumerate the files if there are duplicates

means regex alone will not be enough

[–] vk6flab@lemmy.radio 3 points 11 hours ago