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!
13 Comments
traceur402@lemmy.blahaj.zone · 9 pts · 242d
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
frongt@lemmy.zip · 2 pts · 242d
There are multiple utilities named rename, it's kind of a mess. OP probably wants prename or file-rename.
INeedMana@piefed.zip · 7 pts · 242d
Take a look into regular expressions -
sedorawk. What you wrote would go something like thisScoopta@programming.dev · 3 pts · 242d
If you're going to regex why not use rename instead of writing a script for it?
INeedMana@piefed.zip · 4 pts · 242d
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
means regex alone will not be enough
thingsiplay@lemmy.ml · 2 pts · 241d
Just a little note, that you can use the Bash "<<< heredoc"
sed 's/l/x/g' <<< 'hello'to read stdin, instead usingechofor this job. The reason is, this is more pure and direct without any alteration compared to processing withecho.INeedMana@piefed.zip · 2 pts · 240d
Yes, true. But I rarely use redirection thingies and I was writing that out of my head
Personally I find them cumbersome to remember how many < or > mean what
thingsiplay@lemmy.ml · 2 pts · 240d
Totally understandable. It took me years and years to get used to and to remember how it even functions (was it 2 or 3 angled brackets?). Just to be clear, this was not a critique or attack (as you defended your way of doing :D), I just wanted to share another possibility.
kiri@ani.social · 7 pts · 242d
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.
vk6flab@lemmy.radio · 6 pts · 242d
https://stackoverflow.com/questions/638975/how-do-i-tell-if-a-file-does-not-exist-in-bash#638980
man mvhttps://tldp.org/LDP/abs/html/string-manipulation.html
https://www.digitalocean.com/community/tutorials/linux-sed-command
cerement@slrpnk.net · 4 pts · 241d
a more obscure option is exiftool – and documention
AnimusExMachina@programming.dev · 4 pts · 242d
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 thestrftime()to get your Year-month-day format. All this after reading the directories contents with theos.listdir()oros.walk()loops. You could parse the filenames for IMG/VID usingif "VID" in file_namelogic. You could strip the date from the file_name before using datetime withstring slicing. Lastly you can use theos.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 · 2 pts · 242d
pathlib is your friend here.
This should give you a list of all JPEG files that start with "IMG".