Help please with commands/script to rename my photo files

I’ve messed up a number of folders by using the wrong ID numbers on downloading from the camera. I need to rename the files as follows -

for each file in a given folder
if filename begins “_6D_0” then
rename file changing the zero to a one

E.g. _6D_00347-happy-new-year.CR2 becomes _6D_10347-happy-new-year.CR2

I can see ls foldername | grep '_6D_0' produces the relevant files, and
mv -T oldname newname renames a file,
but I’ve no idea how to send the list of candidates to be renamed.

There are no sub-folders beginning “_6D_0” to complicate things.

If anyone can supply the commands I’d be most grateful. Please keep it simple! I’m thinking I could hardcode the folder as I work on each one in turn.
Thanks for reading.

But careful, please! It is far too easy to make a ooops
in 0.02 seconds flat and leave you with an empty folder — or worse…

From the parent directory run:

rename -d -n 's/^_6D_0/_6D_1/' */_6D_*CR2

's/^_6D_0/_6D_1/' this is a simple regular expression, in plain English: search (the s/) for _6D_0 at the beginning of the filename (the ^) and replace with _6D_1.

To test run with -n, remove -n to actually rename.

-n, --nono
  No action: print names of files to be renamed, but don't rename.

The -d is needed to only match against the filename, not the full path.

-d, --filename, --nopath, --nofullpath
  Do not rename directory: only rename filename component of path.
1 Like

Have you edited with these names…if you rename outside your editing program you might lose your edits

I’d personally use emacs wdired mode which let’s you edit the names of files and folders as if they were text in a text file.

Thanks for the info folks, that’s great, most helpful.
@K-1, I think I’ll start with yours. The files are also jpg and xmp, so would I just leave off the “CR2”, i.e. the last char would be the asterisk?

Yes, the last argument should only match the files you want to rename using regular bash wildcards. You can test the pattern with ls

Thanks, it’s done. The d option doesn’t exist on my system (Ubuntu 18.04, the desktop it came with). And I must have been confused about parent directory, ended up doing
rename 's/^_6D_0/_6D_1/' _6D_*.*
and it was fine.

@priort, thanks for the warning. I don’t use dt’s database so I escaped that complication.

Thanks @Claes but “TLDR” !

@paperdigits - for another day…