0

I have multiple files as below

File1[Pattern].txt
File2[Pattern].txt
File3[Pattern].txt

I want to remove [Pattern] from all the files in a directory.

And finally, the file names should be as below.

File1.txt
File2.txt
File3.txt
kayess
  • 3,384
  • 9
  • 28
  • 45
vivekvroy
  • 27
  • 1
  • 6

1 Answers1

0

With bash parameter expansion (${f%%\[*\]*}) to strip off the portion starting from (first) [ (from left), and using mv to rename:

for f in *.txt; do echo mv -- "$f" "${f%%\[*\]*}".txt; done

Drop echo for actual action.


With rename (prename), the matched group 1 matches the portion before first [ and second group matches literal .txt. In the replacement we have just used the captured groups:

rename -n 's/^([^[]+)\[[^]]+\](\.txt)$/$1$2/' *.txt

Drop -n for actual action.

heemayl
  • 39,294
  • 7
  • 70
  • 76