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
Linux
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
There are multiple utilities named rename, it's kind of a mess. OP probably wants prename or file-rename.
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
If you're going to regex why not use rename instead of writing a script for it?
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
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.
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.
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".
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 YYYY-mm-dd-HHMMSS-sfx.ext format
and lowercases the extension
"-filename<CreateDate"
uses the image creation date (from EXIF metadata) for the above naming scheme