1

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

4
  • 1
    Check the path, it includes /home/madzays/ twice, which should be incorrect. Commented Apr 8, 2018 at 1:22
  • dir=$(pwd) not dir=$(pwd $file), but it's not the issue here Commented Apr 8, 2018 at 1:45
  • 1
    I suggest: samtools view -Sb "$name.sam" > ... (without $dir) Commented Apr 8, 2018 at 1:47
  • Yeah this worked. But why? I thought find returned only the file name, but it returns the whole path, up to the file name! Commented Apr 8, 2018 at 20:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah. The name variable already included the whole path, up to the file name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.