I have a bunch of files where there is additional information in square brackets. I would like to rename them by removing that section.
For example:
filename [additional info 1].ext1 anotherfile [different info 2].ext2
Want to rename to:
filename.ext1 anotherfile.ext2
I tried playing with regex (and using the rename command) but without success. Does anybody know a way to have the rename command strip of only the part of ” [….]” (including the leading space).
Advertisement
Answer
Looping for each file and using sed for some simple regex matching:
for i in *.ext*; do mv "$i" "$(sed 's/ [[^]]*]././' <<<"$i")" done
Tested with:
$ touch 'filename [additional info 1].ext1' 'filename [additional info 2].ext2' $ for i in *.ext*; do mv -v "$i" "$(sed 's/ [[^]]*]././' <<<"$i")"; done renamed 'filename [additional info 1].ext1' -> 'filename.ext1' renamed 'filename [additional info 2].ext2' -> 'filename.ext2'