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!

  • INeedMana@piefed.zip
    link
    fedilink
    English
    arrow-up
    4
    ·
    3 hours ago

    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  
    
      • INeedMana@piefed.zip
        link
        fedilink
        English
        arrow-up
        1
        ·
        43 minutes 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