I am combining ls, xargs and bash -c to run a specific program on multiple input files (whose names match the pattern barcode0[1-4].fastq.

The program that I am calling with xargs is called krocus and below is the code I used to call the program with a single placeholder (for the multiple input files):

ls barcode0[1-4].fastq |cut -d '.' -f 1|xargs -I {} bash -c "krocus --verbose --output_file out_krocus/{}_Ecoli1.txt /nexusb/Gridion/MLST_krocus_DBs/e_coli1 {}.fastq

In the above code my placeholder are the files I want to call krocus on i.e. barcode0[1-4].fastq. I now want to add a second placeholder that will be passed to the --kmer argument of krocus. For this argument, I want to pass the numbers seq 12 14 i.e. 12, 13 and 14. In this example I would like to create a total of 12 files (which means I want to combine the three --kmer values for the four input files)

Below is how I imagine this code would look like with the placeholder for the kmer argument:

ls barcode0[1-4].fastq |cut -d '.' -f 1|xargs -I {} bash -c "krocus --verbose --output_file out_krocus/{placeholderForInputFile}_{placeholderForKmerSize}Ecoli1.txt /nexusb/Gridion/MLST_krocus_DBs/e_coli1 --kmer {placeholderForKmerSize} {placeholderForInputFile}.fastq

The problem is that I need to pipe the output of two commands i.e. ls barcode0[1-4].fastq and seq 12 14

  • does this helps? unix.stackexchange.com/q/387076/72456 Sep 23 '19 at 12:59
  • I had a look at that post. The difference is that on that post he's piping from a single command (echo {1..8) whereas I need to pipe from two commands (ls barcode0[1-4].fastq and seq 12 14)
    – BCArg
    Sep 23 '19 at 13:05
  1. Don't Parse ls

  2. xargs does not support two placeholders.

  3. Use a for loop. Actually, two loops (one for the filenames, and one for the 12..14 sequence).

    For example:


for f in barcode0[1-4].fastq; do
  bn="$(basename "$f" .fastq)"
  for k in {12..14}; do
    krocus --verbose --output_file "out_krocus/${bn}_${k}Ecoli1.txt" \
      /nexusb/Gridion/MLST_krocus_DBs/e_coli1 --kmer "$k" "$f"
  • Thanks, I ended up writing a python script, which does the same as your .sh script
    – BCArg
    Sep 23 '19 at 14:18

Using GNU Parallel it looks like this:

parallel krocus --verbose --output_file out_krocus/{1.}_{2}Ecoli1.txt /nexusb/Gridion/MLST_krocus_DBs/e_coli1 --kmer {2} {1} ::: barcode0[1-4].fastq ::: {12..14}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.