find -exec with multiple command
1
0
Entering edit mode
4.4 years ago
endretoth ▴ 40

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 • 1.8k views
ADD COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY
0
0
Entering edit mode

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 REPLY
0
Entering edit mode

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 REPLY

Login before adding your answer.

Traffic: 1529 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6