Saturday, November 14, 2015

How to pipe multiple unix commands within find -exec command

If you want to e.g. find all fastq.gz files in your current folder and all its subfolders use sudo -find command:

sudo find ./  -iname '*fastq.gz' -exec ls -l {} \;


This will list all fastq.gz files in current folder and subfolders.
-rw-rw-r-- 1 clint clint 24254450268 Jan 26  2015 ./1410UNHS-0007/2102/141107_H0E37_2102_L002_R1.fastq.gz
-rw-rw-r-- 1 clint clint 24760915673 Jan 26  2015 ./1410UNHS-0007/2102/141107_H0E37_2102_L002_R2.fastq.gz
-rw-rw-r-- 1 clint clint 31186684227 Jan 26  2015 ./1410UNHS-0007/2102/141125_H22TJ_2102_L002_R2.fastq.gz
-rw-rw-r-- 1 clint clint 29152539538 Jan 26  2015 ./1410UNHS-0007/2102/141125_H22TJ_2102_L002_R1.fastq.gz
-rw-rw-r-- 1 clint clint 42068280338 Jan 26  2015 ./1410UNHS-0007/1448_1/141125_H22TC_1448_1_L007_R1.fastq.gz
-rw-rw-r-- 1 clint clint 43958530387 Jan 26  2015 ./1410UNHS-0007/1448_1/141125_H22TC_1448_1_L007_R2.fastq.gz
-rw-rw-r-- 1 clint clint 37881056340 Jan 26  2015 ./1410UNHS-0007/2228_1/141125_H22TJ_2228_1_L004_R2.fastq.gz

-rw-rw-r-- 1 clint clint 34110547387 Jan 26  2015 ./1410UNHS-0007/2228_1/141125_H22TJ_2228_1_L004_R1.fastq.gz
...

Now if you want to perform an operation on these files that involves piping multiple unix commands invoke shell with sh -c

sudo find ./  -iname '*fastq.gz' -exec sh -c "zcat {} | paste - - - - | wc -l" \;


This example will count the number of sequences in all existing fastq.gz files with a pipe composed of zcat, paste and wc commands.


No comments:

Post a Comment