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?

  • Look at man rename; you are using the wrong syntax for the version of rename on your OS. Also see 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 May 23 '16 at 23:47
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*
  • 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 0:33
  • I've changed the pattern to match everything after the ]. – Barmar May 24 '16 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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