Question: How to rename file titles that contain underscores?
gravatar for Audrey
10 weeks ago by
Audrey20 wrote:

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:


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

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!

bash file titles underscores • 140 views
ADD COMMENTlink written 10 weeks ago by Audrey20
gravatar for RamRS
10 weeks ago by
Baylor College of Medicine, Houston, TX
RamRS30k wrote:

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 COMMENTlink modified 10 weeks ago • written 10 weeks ago by RamRS30k

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


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

Thank you again, have a nice day.

ADD REPLYlink written 10 weeks ago by Audrey20
gravatar for cpad0112
10 weeks ago by
cpad011214k wrote:

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 COMMENTlink modified 10 weeks ago • written 10 weeks ago by cpad011214k

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 REPLYlink modified 10 weeks ago • written 10 weeks ago by Audrey20
Please log in to add an answer.


Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1710 users visited in the last hour