Find SAM files in subfolders and convert them to BAM format by executing samtools within the subfolders
Lets say you have SAM files in subfolders of a current folder and you would like to convert them into BAM format using samtools. To do this you can copy them all to another folder using find command and then execute samtools in a loop from that folder. However, you can find each SAM file within its subfolder and execute samtools within the subfolder by specifying -execdir option in the find command.
If you dont specify -execdir but only -exec, the output BAM file will be written in the parent folder, and probably will be overwritten in each cycle.
You could achieve the same by creating a for loop that will go into each subdirectory and execute samtools, however creating such loop may be challenging if the names of the subfolders are complicated. The find oneliner is quicker and easier.