简体   繁体   中英

Bash script: rename output directory

I did a bash script for running a program (Velveth) that takes as input all Fasta files (.fa) in a directory, and give the results in a new directory.

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth outdirectory 21 -fasta -short "$filename"  
done

I can loop throw all fasta files but each iteration rewrites the result in the same folder. How could I rename for each iteration the output directory?

I tried giving the same name as the file:

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth "$filename" 21 -fasta -short "$filename"  
done

but it does not work.

thanks.

you could try something like this:

I=0
for filename in /home/lpp/Desktop/test/*.fa; do
    velveth outdirectory 21 -fasta -short "$filename"  
    mv "folder/result" "folder/result$I"
    I=$I+1
done

it will create files like result0 result1 and so on

What I would recomment you to do is create the directoy before using it as output and also not create directory with the same names as the files. If you want to put the output in the same directory as the file, I would recommend doing this:

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    mkdir ${filename%.*}
    velveth ${filename%.*} 21 -fasta -short "$filename"  
done

${filename%.*} will remove the suffix of the file, thus making foo out of foo.fa , that way you wont create directory with the exact same name as the file... creating directory foo for the file foo.fa .

The first parameter to velveth is the output directory, and both of the scripts you provided are wrong

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth outdirectory 21 -fasta -short "$filename"  
done

Wrong: Same directory for all

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth "$filename" 21 -fasta -short "$filename"  
done

Wrong: Directory is equal to the output file

velveth creates the output directory if it does not exist, so I guess there will be no problem if you pass an argument based on the name of the file:

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth "${filename%.*}" 21 -fasta -short "$filename"  
done

Given an input file /home/lpp/Desktop/test/test.fa , velveth will save the output files in the directory /home/lpp/Desktop/test/test .

Another options might be to just "add" something to the name of the file in order to setup the output directory:

#!/bin/bash
for filename  in /home/lpp/Desktop/test/*.fa; do
    velveth "$filename.out" 21 -fasta -short "$filename"  
done

Given an input file /home/lpp/Desktop/test/test.fa , velveth will save the output files in the directory /home/lpp/Desktop/test/test.fa.out .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM