This question already has an answer here:

Ubuntu 16.04

I'm pretty new to linux and have a large number of files an the directory dir. These files have postfix _uploaded.

Is there a way to rename all this files and set them postfix _handled instead of _uploaded?

marked as duplicate by jasonwryan, Jeff Schaller, HalosGhost, jordanm, techraf Nov 3 '16 at 22:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Ubuntu has rename (prename), from directory dir:

rename -n 's/_uploaded$/_handled/g' -- *_uploaded
  • -n is for --dry-run

After getting the potential changes to be made, remove n for actual action:

rename 's/_uploaded$/_handled/g' -- *_uploaded

You can also leverage bash parameter expansion, within a for loop over the filenames containing the string _uploaded at the end, from directory dir:

for f in *_uploaded; do new=${f%_uploaded}; echo mv -- "$f" "${new}_handled"; done

This will show you the changes to be made, remove echo for actual action.

  • The rename regex allows using $ as "end of line" to make sure to replace only the suffix if a file was named like A_foo_foo - i.e. rename 's/_uploaded$/_handled/' * is to be preferred. – Fiximan Nov 3 '16 at 21:56
  • @Fiximan rename supports PCRE, but matching the end of the line using $ is not the desired case here i am afraid. – heemayl Nov 3 '16 at 21:58
  • I seem not to understand the question then: Isn't the aim to replace suffix A with suffix B? – Fiximan Nov 3 '16 at 22:05
  • @Fiximan The question does not mention anything about suffix, the pattern can be anywhere in the name AFAIU. – heemayl Nov 3 '16 at 22:06
  • 1
    Might be a language barrier then: AFAIK suffix and and postfix are synonymous - but I'm not a native speaker though. English guys seem to agree – Fiximan Nov 3 '16 at 22:12

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