0

I have a list of 100 files in my working directory
for example

GCF_000021605.1_ASM2160v1_genomic.fa  
GCF_001887455.1_ASM188745v1_genomic.fa  
GCF_003719755.1_ASM371975v1_genomic.fa  
GCF_000021625.1_ASM2162v1_genomic.fa

I want to rename them to

Vibrio_cholerae_O1_biovar_El_Tor_str._N16961_strain=N16961z
Vibrio_cholerae_O395_strain=O395
Vibrio_cholerae_M66-2_strain=M66-2
Vibrio_cholerae_O395_strain=O395

These new file names are in a text document arranged in the same order as the list of the original file names in the directory.

Is there a way of renaming the files altogether?

while read -u 3 -r name1 && read -u 4 -r name2
do
    mv "${name1}" "${name2}".fasta 
done 3<old_name_4.txt 4<new_name_4.1.txt

old_name.4.txt file contains the file name in the directory that needs to be renamed and the new_4.1.txt file contains the new names for the files in the same order.

Strangely my code doesn't work. When I run the command, it shows the error

mv: cannot move 'GCF_000021605.1_ASM2160v1_genomic.fa' to 'Vibrio_cholerae_O1_biovar_El_Tor_str._N16961_strain=N16961'$'\r''.fasta': No such file or directory

Is there a way of automating this task? Any kind of help would be highly appreciated. Thank you!!

5
  • It doesn't work. I made a mistake in the post. Sorry. Fixed the mistake. When i run the command, it shows the error "mv: cannot move 'GCF_000021605.1_ASM2160v1_genomic.fa' to 'Vibrio_cholerae_O1_biovar_El_Tor_str._N16961_strain=N16961'$'\r''.fasta': No such file or directory"
    – nayeim77
    Jul 1, 2019 at 19:46
  • Please don't put updates here in the comments. Fix and update your post so it's easy for everyone to see. Jul 1, 2019 at 20:20
  • Where did you create the file? You've got foreign line endings in it. Jul 1, 2019 at 20:22
  • I made the text file in excel. Did excel add foreign line endings to the text file? These new files names are a column in an excel CSV metadata sheet. So I just copied that column and made a text file out of it in excel
    – nayeim77
    Jul 1, 2019 at 21:38
  • OK, so you created the file on a Windows computer. Please look up how to convert Windows files to UNIX/Linux files - you'll find many here. Jul 1, 2019 at 22:53

1 Answer 1

0

At least one of your source files came from a Windows system, and so has different line endings (CR-LF instead of CR). You need to post-process the text file to fix this

dos2unix new_name_4.1.txt

or

tr -d '\r' <new_name_4.1.txt >tmp && mv -f tmp new_name_4.1.txt
1
  • Thank you so much for the help @roaima! It did the trick!! I really appreciate the help!
    – nayeim77
    Jul 1, 2019 at 23:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .