Search code examples
unixcommand-linefile-extensionsamtools

How to run a command on multiple files within subdirectories with similar file suffix?


I am still battling to understand and improve my command line skills. This time, I have a directory with 25 subdirectories. In each subdirectory, there are 4 sorted bam files which I want to index. I do not want to go to each subdirectory and manually do samtools index ... as it isn't efficient coding.

When in main directory, I do

find . -name "*.bam" | wc -l 

and this does give me the 100 files. If I were in one directory and did

for i in "*.bam"; do samtools index -b $i $i.bai; done

This would produce the desired output for each bam file. My issue is how do I combine both of these commands to convert all my bam files? The respective .bai files need to be created within their subdirectories, not in the main directory. Any help is appreciated.


Solution

  • If I'm understanding this correctly, you want to run the command "samtools index -b <filename> <filename>.bai" on each file found by the find command. I think that find's exec option should work for this. However, if you need to be in the directory of the file when you run the command, I have a different solution for that. Try the following command and let me know if it works as you want.

    find . -name "*.bam" -exec samtools index -b {} {}.bai \;

    this uses
    {} as a placeholder for the filename found
    \; to end the exec command