Search code examples
linuxbashcentosbatch-rename

Linux: How to add a suffix to files maintaining extension


Linux bash, Centos.

How to add a suffix to files maintaining extension? I could find answer about how to change or add extension to files but not for how to add suffix maintaining the extension...

For example: If I want to add the '_thumb' suffix to all image files (.jpg, .png, .gif, .JPEG) in some directory.

somefile.jpg -> somefile_thumb.jpg

EDIT: If possible the script can be re-launched and not affect those files which have already been renamed. In order to add more extensions to the list.

Thanks in advance!


Solution

  • You can use:

    shopt -s nullglob
    for f in *.jpg *.png *.gif *.JPEG; do
        [[ $f != *_thumb.* ]] && mv "$f" "${f%.*}_thumb.${f##*.}"
    done