Madza Farias-Virgens
Madza Farias-Virgens

Reputation: 1061

Find loop with variable file and path naming

I'm trying to do a bash loop to run a simple process over all files with extension .sam that can be found on a directory and its subdirectories, and use the same name (exc extension) for the output as its input, with output directed to the same folder in which the input was found.

I got to the following:

for file in $(find ~/data/finch_data/combruns_BF_genomes -name "*.sam" -type f); do 
    name="${file%.sam}"
    dir=$(pwd $file)
    samtools view -Sb "$dir"/"$name".sam > "$dir"/"$name".bam
done

but I'm getting this message:

...
>bash:/home/madzays/qsub//home/madzays/data/finch_data/combruns_ZF_transcriptomes/ZBND81X_gff/filteredfirstZBND81X_gff.bam: No such file or directory
>bash:/home/madzays/qsub//home/madzays/data/finch_data/combruns_ZF_transcriptomes/ZBND81X_gff/filteredsecZBND81X_gff.bam: No such file or directory
>bash:/home/madzays/qsub//home/madzays/data/finch_data/combruns_ZF_transcriptomes/ZBND82V_gff/filteredfirstZBND82V_gff.bam: No such file or directory
...

What could be wrong? thanks

Upvotes: 1

Views: 109

Answers (1)

LMC
LMC

Reputation: 12702

Seems to me that namevariable does not contains what you want. Try getting name with basename instead

for file in $(find ~/data/finch_data/combruns_BF_genomes -name "*.sam" -type f); do name="$(basename $file .sam)" dir=$(pwd $file); samtools view -Sb "$dir"/"$name".sam > "$dir"/"$name".bam; done

Upvotes: 1

Related Questions