0

I have some symbolic linked files and some normal files like below:

P_2_UC34_31_1.gz
P_2_UC34_31_2.gz
P_2_UC34_32_1.gz
P_2_UC34_32_2.gz
P_2_UC34_33_1.gz
P_2_UC34_33_2.gz

P_2_UC34_31_1.gz and P_2_UC34_31_2.gz are symbolic links. The other 4 files are normal files (not symbolic links).

I'm trying to add _Ms before the _1 and _2 in all the files.

For the symbolic links I tried like below:

sed -i --follow-symlinks 's/_1/_Ms&/g' *.gz

But this didn't work. I didn't get any error and the file names are not changed.

I want the output to be like below:

P_2_UC34_31_Ms_1.gz
P_2_UC34_31_Ms_2.gz
P_2_UC34_32_Ms_1.gz
P_2_UC34_32_Ms_2.gz
P_2_UC34_33_Ms_1.gz
P_2_UC34_33_Ms_2.gz
beginner
  • 277
  • 7
    sed operates on the contents of files, not their names. You probably just ruined those gzip files irrecoverably (unless you have backups, which I hope you do). What you want is a tool for renaming, like perl-rename (or whatever it is called in your OS) – muru Sep 09 '23 at 14:20
  • okay, Is there a way I can add _Ms to all those files before _1 and _2? – beginner Sep 09 '23 at 14:25
  • Yes, as I said, get the Perl rename command. What it's called varies with distributions (see also: https://unix.stackexchange.com/q/229230/70524) Then you can do prename 's/_[12]\./_Ms&/g' *.gz (or perl-rename or rename or whatever it maybe). So you restored the files from backup? – muru Sep 09 '23 at 14:41
  • I tried this way but didn't work rename 's/_[12]\./_Ms&/g' *.gz – beginner Sep 09 '23 at 14:48
  • 2
    Ah, it should be $&, not &. perl-rename 's/_[12]\./_Ms$&/g' *.gz works for me. You might want to double check whether your rename is actually the Perl one or the util-linux one or something else altogether. – muru Sep 09 '23 at 14:51
  • 1
    Okay I used this way and it worked. for file in *_1.gz; do mv "$file" "${file%_1.fq.gz}_Ms_1.fq.gz"; done no need of any rename of perl-rename – beginner Sep 09 '23 at 15:24
  • 2
  • 1
    Now that you've answered your own question, please either post the answer and mark it as such, for the benefit of others who have similar issues, or remove the question to keep the question space clean. – ghoti Sep 10 '23 at 03:09

1 Answers1

1

From the comment by the author of the question:

Okay I used this way and it worked.

for file in *_1.gz; do
     mv "$file" "${file%_1.fq.gz}_Ms_1.fq.gz";
 done

No need of any rename or perl-rename.


If the script has to change something, it needs a small correction, e.g.

for file in *_1.fq.gz; do
     mv "$file" "${file%_1.fq.gz}_Ms_1.fq.gz";
 done

For the solution of the original question this would be something like

for x in 1 2; do
  for file in *_${x}.gz; do
     mv "$file" "${file%_${x}.gz}_Ms_${x}.gz";
  done
done

(The complicated inner expression deserves a test: file="aaaa_1.gz"; x=1; echo mv "$file" "${file%_${x}.gz}_Ms_${x}.gz"; echo output is mv aaaa_1.gz aaaa_Ms_1.gz.)


I add:

The problem is not related to sed (which does not rename files, only changes a text) nor to symbolic links (which get well renamed by the script).

The finest thing in the script is % in "${file%_1.fq.gz}_Ms...... There, ${file} would expand to the content of variable file but %_1.fq.gz indicates that the string _1.fq.gz should be removed at the end, if posible.

The part _Ms..... is simply added to the string.

  • The thing is that even though the OP provided this answer in the comments, the substitution they used will not work on the set of example files – Chris Davies Sep 12 '23 at 09:18
  • @roaima I added some corrections. Does that work now ? – minorChaos Sep 12 '23 at 09:40
  • Indeed :-) But you could also test, with something like mkdir /tmp/t; cd /tmp/t; touch touch P_2_UC34_3{1_1,1_2,2_1,2_2,3_1.gz,3_2}.gz and then apply the code – Chris Davies Sep 12 '23 at 09:50