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!

  • AnimusExMachina@programming.dev
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    3 hours ago

    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.

    • Diplomjodler@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      3 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”.