2

I have approx 200 files named in the format:

paired_dat_11_R1.fq
paired_dat_12_R2.fq 
paired_dat_121_R1.fq
paired_dat_122_R2.fq
paired_dat_851_R1.fq
paired_dat_852_R2.fq

I just would like to rename the 2 to 1, while leaving the R1 and R2 values intact:

paired_dat_11_R1.fq 
paired_dat_11_R2.fq 
paired_dat_121_R1.fq
paired_dat_121_R2.fq
paired_dat_851_R1.fq
paired_dat_851_R2.fq
  • Yes, the perl rename command. Even the default file manager could do that. – vanadium Sep 01 '22 at 07:48
  • What exactly do you want to do? Change all 12 to 11? Change 2 to 1? Change 2 to its previous number? [Edit] your question and be more specific. Also add a few more items to your example input and output formats so we can understand better. – BeastOfCaerbannog Sep 01 '22 at 08:53
  • Yes, I want to change 2 to 1 so the ID number is the same while leaving the R1 and R2 alone since those correspond to forward and reverse ends of the genome. – veridian21 Sep 01 '22 at 10:03

3 Answers3

5

You can use mmv (install it with sudo apt install mmv) as follows:

mmv -n 'paired_dat_*2_R2*' 'paired_dat_#1\1_R2#2'

-n is used so that you can preview the changes without applying them to your files. If you are satisfied with the output, run the command without -n.

How this works

mmv comprises of a filename matching part ('paired_dat_*2_R2*' here) and a replacement part ('paired_dat_#1\1_R2#2' here).

In the first part we use wildcards to match parts of the filename which allows for using these matches to rename the files.

In the above command the * wildcard is used, which means "match any character". The first * matches the characters between paired_dat_ and 2_R2, while the second * matches the characters from 2_R2 to the end of the filename.

Each match can be then used in the replacement part using # followed by ascending numbers that correspond to the match. So #1 corresponds to the match of the first * and #2 to the second match of *. Since the first * matches up to 2 without including it, using #1 in the replacement part essentially removes it. We then add 1_R2 (1 should be escaped, thus we use \1) and then we add the rest of the filename as #2.

You can find more details by running in a terminal man mmv.


There is also a GUI option using Nautilus, which is similar to what Artur Meinild suggests in his answer:

  • Open Nautilus.

  • Navigate to the directory with your files.

  • Select your files.

  • right-click and select Rename or press F2.

  • In the window that pops up select Find and replace text.

  • In the Existing Text field enter: 2_R2

  • In the Replace With field enter: 1_R2

  • Click Replace and your files will be renamed!

nautilus renaming

As always, test this first on a copied portion of the files to make sure that it works as intended.

2

Install the Perl rename command, if it's not already present (it wasn't on my system by default):

sudo apt install rename

Run this command to do the change you describe:

rename 's/2_R2/1_R2/' paired_dat*.fq

The syntax is equivalent to that of the sed command.

Also see this thread on Unix & Linux.

Artur Meinild
  • 26,018
-1

You can use Krusader's MultiRename. It takes ???? for single characters and like in the above answer you can see the results before applying them. It is essentially just a GUI for krename.

pierrely
  • 653
  • It would be nice if you could [edit] your answer and be a bit more detailed about how Krusader could be used for the particular OP's case. Thanks! – BeastOfCaerbannog Dec 08 '22 at 10:29
  • yes it would be nice, and time consuming. I accept the downvote though. http://multicommander.com/docs/MultiRename – pierrely Dec 08 '22 at 21:02