Rename images/ files recursively?

How can I rename images/ files recursively? For example:

folder
- subfolder1
   - a-copy.jpg
- subfolder2
   - b-copy.jpg
- subfolder3
   - c-copy.jpg

I would like to get:

folder
- subfolder1
   - a-thumb.jpg
- subfolder2
   - b-thumb.jpg
- subfolder3
   - c-thumb.jpg

I tried:

for i in $(find . -iname '*.jpg') ; do rename 's/-copy/-thumb/' *.jpg; done

Doesn’t work off course! Any idea?

Asked By: Run

||

With rename, check with -n for a dry-run first like so:

rename -n 's/-copy./-thumb./ if -f' folder/*/*.jpg

… and if satisfied with the naming output, remove -n and run it again to do the actual renaming.

or in a loop, first, check with echo included as follows (run from within the parent directory containing the folder directory or alternatively provide /full/path/to/folder/*/*.jpg below):

for f in folder/*/*.jpg
  do
    [ -f "$f" ] && echo mv -nv -- "$f" "${f/-copy./-thumb.}"
    done

… and if satisfied with the naming output, remove echo and run it again as follows:

for f in folder/*/*.jpg
  do
    [ -f "$f" ] && mv -nv -- "$f" "${f/-copy./-thumb.}"
    done

… to do the actual renaming.

To include other file extensions (Case Sensitive), add them in a brace expansion like folder/*/*.{jpg,JPG,png,PNG} etc.

Answered By: Raffa
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.