2

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?

steve
  • 21,582
  • 5
  • 48
  • 75
stella
  • 121
  • 2

1 Answers1

5

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.

heemayl
  • 54,820
  • 8
  • 124
  • 141
  • 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. – FelixJN Nov 03 '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 03 '16 at 21:58
  • I seem not to understand the question then: Isn't the aim to replace suffix A with suffix B? – FelixJN Nov 03 '16 at 22:05
  • @Fiximan The question does not mention anything about _suffix_, the pattern can be anywhere in the name AFAIU. – heemayl Nov 03 '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](http://english.stackexchange.com/questions/81263/postfix-or-suffix) – FelixJN Nov 03 '16 at 22:12
  • @Fiximan duh..I'm really poor at this. Edited. Thanks. – heemayl Nov 03 '16 at 23:21