How to rename file titles that contain underscores?
2
1
Entering edit mode
3.7 years ago
A_heath ▴ 160

Hi all,

I have multiple files that have the same (long) title format: Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta_proteins.fasta_results

I want to delete everything after ".fasta" so it would be something like: Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta

I tried to make a small bash script in order to do that quickly but when I run it, nothing is changing. Here is my script:

#!/bin/bash

for file in /home/Documents/Folder;
do
cut -d_ -f1,2,3,4,5,6,7,8 <<< "$f";
done

There is obviously something wrong with it but I can't find where.

If you have any ideas/suggestions for improvements, please, let me know.

Thank you for your help!

shell bash • 882 views
ADD COMMENT
3
Entering edit mode
3.7 years ago
Ram 43k

cut operates on file contents, not file names - not unless used much differently. Plus, your loop variable is called $file while your loop uses something called $f.

You should go with sed. It could be done with bash parameter expansion too, but sed is easier.

In your case, you'd need something like

echo "mv ${file} $(echo $file | sed 's/.fasta.*/.fasta/')" #untested - the first "." in the sed expression might need escaping

If the above echos mv commands as expected, remove the echo and the double quotes surrounding the mv command to execute the command.

ADD COMMENT
0
Entering edit mode

Thank you so much RamRS for your help! I run the following script and it worked great:

#!/bin/bash

for file in * ;
do
mv ${file} $(echo $file | sed 's/.fasta.*/.fasta/');
done

Thank you again, have a nice day.

ADD REPLY
2
Entering edit mode
3.7 years ago

try this with rename:

$ rename -n "s/\.fasta.*/\.fasta/"

This would dry-run and prints the file names before and after changing. Remove -n. Script works for OP file format.

Example code:

$ ls *.fasta_results| rename -n "s/\.fasta.*/\.fasta/"    

rename(Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta_proteins.fasta_results, Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta)

with parallel:

$ parallel --dry-run mv {} '{= s:\.[^.]+$::;s:\.[^.]+$::; =}.fasta' ::: *.fasta_results

mv Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta_proteins.fasta_results Lactococcus_lactis_subsp_cremoris_strain_number_file.gbk_fasta.fasta
ADD COMMENT
0
Entering edit mode

Thank you cpad0112 for your help! Both commands work great. Even if I understand better the rename command than the parallel one. That's so useful, thanks again!

ADD REPLY

Login before adding your answer.

Traffic: 1849 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6