1

In one directory I have many files which look like this:

Galaxy100-[0025-CL3.single.bed].bed

and I would like to change it to:

0025-CL3.single.bed

I tried this rename 's/Galaxy[0-9] - \[//' *, but it did not change anything.

How is possible to rename these files?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
user977828
  • 921
  • 4
  • 16
  • 30
  • Look at `man rename`; you are using the wrong syntax for the version of `rename` on your OS. Also see http://unix.stackexchange.com/a/238862/135943 – Wildcard May 23 '16 at 23:31
  • You're only matching a single digit after `Galaxy`. You need to use `[0-9]+` to match any number of digits. And you shouldn't have spaces around `-`. – Barmar May 23 '16 at 23:43
  • 2
    You need `prename`, not `rename` (`rename` is only `prename` on Debian and derivatives). – Gilles 'SO- stop being evil' May 23 '16 at 23:47

1 Answers1

2

Your regular expression doesn't match the pattern in your filename. To match at least one digit, you need to use [0-9]+ (you can also use \d to match digits); your pattern will only match 1 digit. Your example filename doesn't have spaces around -, but you have them in the pattern. And you're not doing anything to remove the ] at the end. Try:

rename 's/Galaxy\d+-\[(.*)\].*/$1/' Galaxy*
Barmar
  • 9,648
  • 1
  • 19
  • 28
  • In CentOS 6 it did not work, but it work almost in Ubuntu 14.04 and I got `0025-CL3.single.bed.bed`. How is it possible to remove the file extension? – user977828 May 24 '16 at 00:33
  • I've changed the pattern to match everything after the `]`. – Barmar May 24 '16 at 00:36