1

I process files found by ls as

ls /folder/ | parallel -j20 ./command {}

but I need to pass the job number as well. I tried

ls /folder/ | parallel -j20 ./command {1} {2} ::: {1..20}
ls /folder/ | parallel -j20 ./command {} {} ::: {1..20}

but it does not work. I also tried {#} for passing the job number.

6
  • 1
    Note that you should never use the output of ls in a script. Using a for loop or find is always the better solution. Feb 20, 2022 at 11:19
  • @mashuptwice may I ask why?
    – Googlebot
    Feb 20, 2022 at 11:27
  • 1
  • @mashuptwice oh thanks. I always work with well-formatted directories, and thus, never thought of that.
    – Googlebot
    Feb 20, 2022 at 11:32
  • 1
    Please edit your question and show us the commands you are trying to run. Give us a few file names and then what number should be passed an how. Are you trying to launch things like ./command file1.txt 1 and ./command file2.txt 2 etc.?
    – terdon
    Feb 20, 2022 at 11:57

2 Answers 2

2

The {#} replacement string should be exactly what you need here. For example, given

$ ls
 file1  'file2 with spaces'  'file3'$'\n''with'$'\n''newlines'   file4   file5

then

parallel --null echo {#} {} ::: *
1 file1
2 file2 with spaces
3 file3
with
newlines
4 file4
5 file5

or, if you have enough files to exceed the ARG_MAX limit you could use

printf '%s\0' * | parallel --null echo {#} {}
2
  • You could also just do parallel --null echo {#} {} ::: *.
    – terdon
    Feb 20, 2022 at 13:19
  • @terdon good point - I'm overcomplicating things as usual... Feb 20, 2022 at 13:52
0

It is unclear to me what you want to do. Maybe one of these?

ls /folder/ | parallel -j20 ./command {1} {2} :::: - ::: {1..20}
ls /folder/ | parallel -j20 ./command {1} {2} :::: - :::+ {1..20}
ls /folder/ | parallel -j20 ./command {1} {#}
ls /folder/ | parallel -j20 ./command {1} {%}

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.