This is a test version of Biostars. For the public version, visit https://www.biostars.org.
find -exec with multiple command

Hi!

I'm in trouble, I would like to use the find command with multiple -exec flag and print the result in a file. So far, I could do the following, which works perfectly, however I would like to add another -exec command:

find ./ref_mapped/ -name '*.sam' -exec ./samtools-1.10/samtools flagstat {} \; > bwa_ell

Here, I run samtools flagstat on all file with a .sam extension and print the result in a file called bwa_ell. However, I would like to include the names of the file, so I will be able to tell which result is for which file.

I would like to implement this somehow: -exec grep -l or something similar to include file names along with the results..Unfortunately, I could not succeed so far. :(

May I kindly ask is it possible at all?

linux unix samtools find command

I spent a half day on this...I think I have a solution...

find ./ref_mapped/ -name '*.sam' -exec basename -s .sam {} \; -exec ./samtools-1.10/samtools flagstat {} \; > bwa_ellenorzes

it seems working but not sure if it is correct...please someone confirm!

I have not used -exec with find. But a similar approach other than gnu parallel is to use xargs. It is a command I often find handy and it executes a give command taking arguments from stdin. An example would be.

find -type f -name '*.bam' |xargs -i{} sh -c "echo {} && samtools flagstat {}" > bwa_ell

1 answer

I have tried that but didn't work. Please, check my previous comment, I think it is a good solution :)

PS: You always comment on my questions :):):)

Log in to answer this question.