Question: find -exec with multiple command
0
gravatar for endretoth
4 weeks ago by
endretoth20
endretoth20 wrote:

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?

ADD COMMENTlink modified 4 weeks ago by Joe17k • written 4 weeks ago by endretoth20

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!

ADD REPLYlink written 4 weeks ago by endretoth20

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

ADD REPLYlink modified 4 weeks ago • written 4 weeks ago by microfuge1.8k
0
gravatar for Joe
4 weeks ago by
Joe17k
United Kingdom
Joe17k wrote:

https://stackoverflow.com/questions/5119946/find-exec-with-multiple-commands

ADD COMMENTlink written 4 weeks ago by Joe17k

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 :):):)

ADD REPLYlink written 4 weeks ago by endretoth20

Ah I see, the question isn't actually about how you use multiple -exec statements, its how to manipulate filenames within find's syntax?

Try parameter expansion? https://unix.stackexchange.com/questions/476763/how-to-use-parameter-expansion-with-find-exec

ADD REPLYlink written 4 weeks ago by Joe17k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 756 users visited in the last hour