In linux bash shell, I have multiple tif files (around 1000) as follows:

Sep04_17.00.37.tif  Sep04_17.05.54.tif  Sep04_17.08.41.tif
Sep04_17.02.02.tif  Sep04_17.07.17.tif

How Can I remove the _ and . in the names? the final result should be:

Sep04170037.tif  Sep04170554.tif  Sep04170841.tif
Sep04170202.tif  Sep04170717.tif

I have tried rename: rename 's/\_/\//' *.tif ; rename 's/\_/\//g' *.tif ; none of them works.

  • 1
    Hello, and welcome on Stack Exchange :) If one of the answers was helpful or actually solved your problem, don't hesitate to vote it up or mark it as "accepted" so that it'll be easier for other people to benefit from it. You can vote up every answer you like, but you can only accept one. – iago-lito Sep 9 '18 at 17:06
  • Removing every dot would remove the dot before the filename suffix as well... – Kusalananda Oct 24 '18 at 12:17

You are having the right approach. Perhaps you missed out the trailing g in your regex expression that enables matching every occurence of . and _ in each filename.

For use with rename, I would suggest this two-steps approach:

rename 's/[._]//g' *.tif    # remove any . and _ occurence


Sep04170037tif Sep04170202tif Sep04170554tif Sep04170717tif Sep04170841tif


rename 's/tif$/.tif/' *tif    # replace ending tif by .tif

resulting in

Sep04170037.tif Sep04170202.tif Sep04170554.tif Sep04170717.tif Sep04170841.tif

Don't hesitate to try out your regexes online first, for instance on regex101, which also teaches you many things about pcre :)

[EDIT]: if you prefer one-liners:

rename 's/[._]//g;s/tif$/.tif/' *.tif

Will also avoid matching unfortunate neighbouring unrelated files that would unluckily also be ending by tif during the second step.


One variant using sed:

for file in *tif; do newname=$(echo "$file" | sed 's/_//;s/\.\([0-9]\)/\1/g'); mv -v "$file" "$newname"; done
'Sep04_17.00.37.tif' -> 'Sep04170037.tif'
'Sep04_17.02.02.tif' -> 'Sep04170202.tif'
'Sep04_17.05.54.tif' -> 'Sep04170554.tif'
'Sep04_17.07.17.tif' -> 'Sep04170717.tif'
'Sep04_17.08.41.tif' -> 'Sep04170841.tif'

Using mv -v in the example only to show before/after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.