neulab/tldr huggingface dataset:
parallel eval 'cat {=s/y01/y*/=} > {=s/y01//=}' ::: *y01*
For all values of x and z it runs commands like:
cat x1y*z1 > x1z1
So you end up with x1z1 .. x5z5 each containing the content of all values of y.
EXAMPLE: Breadth first parallel web crawler/mirrorer This script below will crawl and mirror a URL in parallel.  It downloads first pages that are 1 click down, then 2 clicks down, then 3; instead of the normal depth first, where the first link link on each page is fetched first.
Run like this:
PARALLEL=-j100 ./parallel-crawl http://gatt.org.yeslab.org/
Remove the wget part if you only want a web crawler.
It works by fetching a page from a list of URLs and looking for links in that page that are within the same starting URL and that have not already been seen. These links are added to a new queue. When all the pages from the list is done, the new queue is moved to the list of URLs and the process is started over until no unseen links are found.
# E.g. http://gatt.org.yeslab.org/ URL=$1 # Stay inside the start dir BASEURL=$(echo $URL | perl -pe 's:#.*::; s:(//.*/)[^/]*:$1:') URLLIST=$(mktemp urllist.XXXX) URLLIST2=$(mktemp urllist.XXXX) SEEN=$(mktemp seen.XXXX)
# Spider to get the URLs echo $URL >$URLLIST cp $URLLIST $SEEN
while [ -s $URLLIST ] ; do cat $URLLIST | parallel lynx -listonly -image_links -dump {} \; \ wget -qm -l1 -Q1 {} \; echo Spidered: {} \>\&2 | perl -ne 's/#.*//; s/\s+\d+.\s(\S+)$/$1/ and do { $seen{$1}++ or print }' | grep -F $BASEURL | grep -v -x -F -f $SEEN | tee -a $SEEN > $URLLIST2 mv $URLLIST2 $URLLIST done
rm -f $URLLIST $URLLIST2 $SEEN
EXAMPLE: Process files from a tar file while unpacking If the files to be processed are in a tar file then unpacking one file and processing it immediately may be faster than first unpacking all files.
tar xvf foo.tgz | perl -ne 'print $l;$l=$_;END{print $l}' | \ parallel echo
The Perl one-liner is needed to make sure the file is complete before handing it to GNU parallel.
EXAMPLE: Rewriting a for-loop and a while-read-loop for-loops like this:
(for x in `cat list` ; do do_something $x done) | process_output
and while-read-loops like this:
cat list | (while read x ; do do_something $x done) | process_output
can be written like this:
For example: Find which host name in a list has IP address 1.2.3 4:
cat hosts.txt | parallel -P 100 host | grep 1.2.3.4
If the processing requires more steps the for-loop like this:
(for x in `cat list` ; do no_extension=${x%.*}; do_step1 $x scale $no_extension.jpg do_step2 <$x $no_extension done) | process_output
and while-loops like this:
cat list | (while read x ; do no_extension=${x%.*}; do_step1 $x scale $no_extension.jpg do_step2 <$x $no_extension done) | process_output
cat list | parallel "do_step1 {} scale {.}.jpg ; do_step2 <{} {.}" |\ process_output
If the body of the loop is bigger, it improves readability to use a function:
(for x in `cat list` ; do do_something $x [... 100 lines that do something with $x ...] done) | process_output
cat list | (while read x ; do do_something $x [... 100 lines that do something with $x ...] done) | process_output
can both be rewritten as:
doit() { x=$1 do_something $x [... 100 lines that do something with $x ...] } export -f doit cat list | parallel doit
EXAMPLE: Rewriting nested for-loops Nested for-loops like this:
(for x in `cat xlist` ; do for y in `cat ylist` ; do do_something $x $y done done) | process_output
parallel do_something {1} {2} :::: xlist ylist | process_output
Nested for-loops like this:
(for colour in red green blue ; do for size in S M L XL XXL ; do echo $colour $size done done) | sort
parallel echo {1} {2} ::: red green blue ::: S M L XL XXL | sort
EXAMPLE: Finding the lowest difference between files diff is good for finding differences in text files. diff | wc -l gives an indication of the size of the difference. To find the differences between all files in the current dir do:
parallel --tag 'diff {1} {2} | wc -l' ::: * ::: * | sort -nk3
This way it is possible to see if some files are closer to other files.
EXAMPLE: for-loops with column names When doing multiple nested for-loops it can be easier to keep track of the loop variable if is is named instead of just having a number. Use --header : to let the first argument be an named alias for the positional replacement string:
parallel --header : echo {colour} {size} \ ::: colour red green blue ::: size S M L XL XXL
This also works if the input file is a file with columns:
cat addressbook.tsv | \ parallel --colsep '\t' --header : echo {Name} {E-mail address}
EXAMPLE: All combinations in a list GNU parallel makes all combinations when given two lists.
To make all combinations in a single list with unique values, you repeat the list and use replacement string {choose_k}:
parallel --plus echo {choose_k} ::: A B C D ::: A B C D
parallel --plus echo 2{2choose_k} 1{1choose_k} ::: A B C D ::: A B C D
{choose_k} works for any number of input sources:
parallel --plus echo {choose_k} ::: A B C D ::: A B C D ::: A B C D
EXAMPLE: From a to b and b to c Assume you have input like:
aardvark babble cab dab each
and want to run combinations like:
aardvark babble babble cab cab dab dab each
If the input is in the file in.txt:
parallel echo {1} - {2} ::::+ <(head -n -1 in.txt) <(tail -n +2 in.txt)
If the input is in the array $a here are two solutions:
seq $((${#a[@]}-1)) | \ env_parallel --env a echo '${a[{=$_--=}]} - ${a[{}]}' parallel echo {1} - {2} ::: "${a[@]::${#a[@]}-1}" :::+ "${a[@]:1}"
EXAMPLE: Count the differences between all files in a dir Using --results the results are saved in /tmp/diffcount*.
parallel --results /tmp/diffcount "diff -U 0 {1} {2} | \ tail -n +3 |grep -v '^@'|wc -l" ::: * ::: *
To see the difference between file A and file B look at the file '/tmp/diffcount/1/A/2/B'.
EXAMPLE: Speeding up fast jobs Starting a job on the local machine takes around 10 ms. This can be a big overhead if the job takes very few ms to run. Often you can group small jobs together using -X which will make the overhead less significant. Compare the speed of these:
seq -w 0 9999 | parallel touch pict{}.jpg seq -w 0 9999 | parallel -X touch pict{}.jpg
If your program cannot take multiple arguments, then you can use GNU parallel to spawn multiple GNU parallels:
seq -w 0 9999999 | \ parallel -j10 -q -I,, --pipe parallel -j0 touch pict{}.jpg
If -j0 normally spawns 252 jobs, then the above will try to spawn 2520 jobs. On a normal GNU/Linux system you can spawn 32000 jobs using this technique with no problems. To raise the 32000 jobs limit raise /proc/sys/kernel/pid_max to 4194303.
If you do not need GNU parallel to have control over each job (so no need for --retries or --joblog or similar), then it can be even faster if you can generate the command lines and pipe those to a shell. So if you can do this:
mygenerator | sh
Then that can be parallelized like this:
mygenerator | parallel --pipe --block 10M sh
mygenerator() { seq 10000000 | perl -pe 'print "echo This is fast job number "'; } mygenerator | parallel --pipe --block 10M sh
The overhead is 100000 times smaller namely around 100 nanoseconds per job.
EXAMPLE: Using shell variables When using shell variables you need to quote them correctly as they may otherwise be interpreted by the shell.
Notice the difference between:
ARR=("My brother's 12\" records are worth <\$\$\$>"'!' Foo Bar) parallel echo ::: ${ARR[@]} # This is probably not what you want
ARR=("My brother's 12\" records are worth <\$\$\$>"'!' Foo Bar) parallel echo ::: "${ARR[@]}"
When using variables in the actual command that contains special characters (e.g. space) you can quote them using '"$VAR"' or using "'s and -q:
VAR="My brother's 12\" records are worth <\$\$\$>" parallel -q echo "$VAR" ::: '!' export VAR parallel echo '"$VAR"' ::: '!'
If $VAR does not contain ' then "'$VAR'" will also work (and does not need export):
VAR="My 12\" records are worth <\$\$\$>" parallel echo "'$VAR'" ::: '!'
If you use them in a function you just quote as you normally would do:
VAR="My brother's 12\" records are worth <\$\$\$>" export VAR myfunc() { echo "$VAR" "$1"; } export -f myfunc parallel myfunc ::: '!'
EXAMPLE: Group output lines When running jobs that output data, you often do not want the output of multiple jobs to run together. GNU parallel defaults to grouping the output of each job, so the output is printed when the job finishes. If you want full lines to be printed while the job is running you can use --line-buffer. If you want output to be printed as soon as possible you can use -u.
Compare the output of:
parallel wget --limit-rate=100k \ https://ftpmirror.gnu.org/parallel/parallel-20{}0822.tar.bz2 \ ::: {12..16} parallel --line-buffer wget --limit-rate=100k \ https://ftpmirror.gnu.org/parallel/parallel-20{}0822.tar.bz2 \ ::: {12..16} parallel -u wget --limit-rate=100k \ https://ftpmirror.gnu.org/parallel/parallel-20{}0822.tar.bz2 \ ::: {12..16}
EXAMPLE: Tag output lines GNU parallel groups the output lines, but it can be hard to see where the different jobs begin. --tag prepends the argument to make that more visible:
parallel --tag wget --limit-rate=100k \ https://ftpmirror.gnu.org/parallel/parallel-20{}0822.tar.bz2 \ ::: {12..16}
--tag works with --line-buffer but not with -u:
parallel --tag --line-buffer wget --limit-rate=100k \ https://ftpmirror.gnu.org/parallel/parallel-20{}0822.tar.bz2 \ ::: {12..16}
Check the uptime of the servers in ~/.parallel/sshloginfile:
parallel --tag -S .. --nonall uptime
EXAMPLE: Colorize output Give each job a new color. Most terminals support ANSI colors with the escape code "\033[30;3Xm" where 0 <= X <= 7:
seq 10 | \ parallel --tagstring '\033[30;3{=$_=++$::color%8=}m' seq {} parallel --rpl '{color} $_="\033[30;3".(++$::color%8)."m"' \ --tagstring {color} seq {} ::: {1..10}
To get rid of the initial \t (which comes from --tagstring):
... | perl -pe 's/\t//'
EXAMPLE: Keep order of output same as order of input Normally the output of a job will be printed as soon as it completes. Sometimes you want the order of the output to remain the same as the order of the input. This is often important, if the output is used as input for another system. -k will make sure the order of output will be in the same order as input even if later jobs end before earlier jobs.
Append a string to every line in a text file:
cat textfile | parallel -k echo {} append_string
If you remove -k some of the lines may come out in the wrong order.
Another example is traceroute:
parallel traceroute ::: qubes-os.org debian.org freenetproject.org
will give traceroute of qubes-os.org, debian.org and freenetproject.org, but it will be sorted according to which job completed first.
To keep the order the same as input run:
parallel -k traceroute ::: qubes-os.org debian.org freenetproject.org
This will make sure the traceroute to qubes-os.org will be printed first.
A bit more complex example is downloading a huge file in chunks in parallel: Some internet connections will deliver more data if you download files in parallel. For downloading files in parallel see: "EXAMPLE: Download 10 images for each of the past 30 days". But if you are downloading a big file you can download the file in chunks in parallel.
To download byte 10000000-19999999 you can use curl:
curl -r 10000000-19999999 https://example.com/the/big/file >file.part
To download a 1 GB file we need 100 10MB chunks downloaded and combined in the correct order.
seq 0 99 | parallel -k curl -r \ {}0000000-{}9999999 https://example.com/the/big/file > file
EXAMPLE: Parallel grep grep -r greps recursively through directories. On multicore CPUs GNU parallel can often speed this up.
find . -type f | parallel -k -j150% -n 1000 -m grep -H -n STRING {}
This will run 1.5 job per CPU, and give 1000 arguments to grep.
EXAMPLE: Grepping n lines for m regular expressions. The simplest solution to grep a big file for a lot of regexps is:
grep -f regexps.txt bigfile
Or if the regexps are fixed strings:
grep -F -f regexps.txt bigfile
There are 3 limiting factors: CPU, RAM, and disk I/O.
RAM is easy to measure: If the grep process takes up most of your free memory (e.g. when running top), then RAM is a limiting factor.
CPU is also easy to measure: If the grep takes >90% CPU in top, then the CPU is a limiting factor, and parallelization will speed this up.
It is harder to see if disk I/O is the limiting factor, and depending on the disk system it may be faster or slower to parallelize. The only way to know for certain is to test and measure.
Limiting factor: RAM
The normal grep -f regexps.txt bigfile works no matter the size of bigfile, but if regexps.txt is so big it cannot fit into memory, then you need to split this.
grep -F takes around 100 bytes of RAM and grep takes about 500 bytes of RAM per 1 byte of regexp. So if regexps.txt is 1% of your RAM, then it may be too big.
If you can convert your regexps into fixed strings do that. E.g. if the lines you are looking for in bigfile all looks like:
ID1 foo bar baz Identifier1 quux fubar ID2 foo bar baz Identifier2
then your regexps.txt can be converted from:
ID1.*Identifier1 ID2.*Identifier2
into:
ID1 foo bar baz Identifier1 ID2 foo bar baz Identifier2
This way you can use grep -F which takes around 80% less memory and is much faster.
If it still does not fit in memory you can do this:
parallel --pipepart -a regexps.txt --block 1M grep -F -f - -n bigfile | \ sort -un | perl -pe 's/^\d+://'
The 1M should be your free memory divided by the number of CPU threads and divided by 200 for grep -F and by 1000 for normal grep. On GNU/Linux you can do:
free=$(awk '/^((Swap)?Cached|MemFree|Buffers):/ { sum += $2 } END { print sum }' /proc/meminfo) percpu=$((free / 200 / $(parallel --number-of-threads)))k
parallel --pipepart -a regexps.txt --block $percpu --compress \ grep -F -f - -n bigfile | \ sort -un | perl -pe 's/^\d+://'
If you can live with duplicated lines and wrong order, it is faster to do:
parallel --pipepart -a regexps.txt --block $percpu --compress \ grep -F -f - bigfile
Limiting factor: CPU
If the CPU is the limiting factor parallelization should be done on the regexps:
cat regexps.txt | parallel --pipe -L1000 --roundrobin --compress \ grep -f - -n bigfile | \ sort -un | perl -pe 's/^\d+://'
The command will start one grep per CPU and read bigfile one time per CPU, but as that is done in parallel, all reads except the first will be cached in RAM. Depending on the size of regexps.txt it may be faster to use --block 10m instead of -L1000.
Some storage systems perform better when reading multiple chunks in parallel. This is true for some RAID systems and for some network file systems. To parallelize the reading of bigfile:
parallel --pipepart --block 100M -a bigfile -k --compress \ grep -f regexps.txt
This will split bigfile into 100MB chunks and run grep on each of these chunks. To parallelize both reading of bigfile and regexps.txt combine the two using --cat:
parallel --pipepart --block 100M -a bigfile --cat cat regexps.txt \ \| parallel --pipe -L1000 --roundrobin grep -f - {}
If a line matches multiple regexps, the line may be duplicated.
Bigger problem
If the problem is too big to be solved by this, you are probably ready for Lucene.
EXAMPLE: Using remote computers To run commands on a remote computer SSH needs to be set up and you must be able to login without entering a password (The commands ssh-copy-id, ssh-agent, and sshpass may help you do that).
If you need to login to a whole cluster, you typically do not want to accept the host key for every host. You want to accept them the first time and be warned if they are ever changed. To do that:
# Add the servers to the sshloginfile (echo servera; echo serverb) > .parallel/my_cluster # Make sure .ssh/config exist touch .ssh/config cp .ssh/config .ssh/config.backup # Disable StrictHostKeyChecking temporarily (echo 'Host *'; echo StrictHostKeyChecking no) >> .ssh/config parallel --slf my_cluster --nonall true # Remove the disabling of StrictHostKeyChecking mv .ssh/config.backup .ssh/config
The servers in .parallel/my_cluster are now added in .ssh/known_hosts.
To run echo on server.example.com:
seq 10 | parallel --sshlogin server.example.com echo
To run commands on more than one remote computer run:
seq 10 | parallel --sshlogin s1.example.com,s2.example.net echo
seq 10 | parallel --sshlogin server.example.com \ --sshlogin server2.example.net echo
If the login username is foo on server2.example.net use:
seq 10 | parallel --sshlogin server.example.com \ --sshlogin foo@server2.example.net echo
If your list of hosts is server1-88.example.net with login foo:
seq 10 | parallel -Sfoo@server{1..88}.example.net echo
To distribute the commands to a list of computers, make a file mycomputers with all the computers:
server.example.com foo@server2.example.com server3.example.com
Then run:
seq 10 | parallel --sshloginfile mycomputers echo
To include the local computer add the special sshlogin ':' to the list:
server.example.com foo@server2.example.com server3.example.com :
GNU parallel will try to determine the number of CPUs on each of the remote computers, and run one job per CPU - even if the remote computers do not have the same number of CPUs.
If the number of CPUs on the remote computers is not identified correctly the number of CPUs can be added in front. Here the computer has 8 CPUs.
seq 10 | parallel --sshlogin 8/server.example.com echo
EXAMPLE: Transferring of files To recompress gzipped files with bzip2 using a remote computer run:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com \ --transfer "zcat {} | bzip2 -9 >{.}.bz2"
This will list the .gz-files in the logs directory and all directories below. Then it will transfer the files to server.example.com to the corresponding directory in $HOME/logs. On server.example.com the file will be recompressed using zcat and bzip2 resulting in the corresponding file with .gz replaced with .bz2.
If you want the resulting bz2-file to be transferred back to the local computer add --return {.}.bz2:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com \ --transfer --return {.}.bz2 "zcat {} | bzip2 -9 >{.}.bz2"
After the recompressing is done the .bz2-file is transferred back to the local computer and put next to the original .gz-file.
If you want to delete the transferred files on the remote computer add --cleanup. This will remove both the file transferred to the remote computer and the files transferred from the remote computer:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com \ --transfer --return {.}.bz2 --cleanup "zcat {} | bzip2 -9 >{.}.bz2"
If you want run on several computers add the computers to --sshlogin either using ',' or multiple --sshlogin:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com,server2.example.com \ --sshlogin server3.example.com \ --transfer --return {.}.bz2 --cleanup "zcat {} | bzip2 -9 >{.}.bz2"
You can add the local computer using --sshlogin :. This will disable the removing and transferring for the local computer only:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com,server2.example.com \ --sshlogin server3.example.com \ --sshlogin : \ --transfer --return {.}.bz2 --cleanup "zcat {} | bzip2 -9 >{.}.bz2"
Often --transfer, --return and --cleanup are used together. They can be shortened to --trc:
find logs/ -name '*.gz' | \ parallel --sshlogin server.example.com,server2.example.com \ --sshlogin server3.example.com \ --sshlogin : \ --trc {.}.bz2 "zcat {} | bzip2 -9 >{.}.bz2"
With the file mycomputers containing the list of computers it becomes:
find logs/ -name '*.gz' | parallel --sshloginfile mycomputers \ --trc {.}.bz2 "zcat {} | bzip2 -9 >{.}.bz2"
If the file ~/.parallel/sshloginfile contains the list of computers the special short hand -S .. can be used:
find logs/ -name '*.gz' | parallel -S .. \ --trc {.}.bz2 "zcat {} | bzip2 -9 >{.}.bz2"
EXAMPLE: Advanced file transfer Assume you have files in in/*, want them processed on server, and transferred back into /other/dir:
parallel -S server --trc /other/dir/./{/}.out \ cp {/} {/}.out ::: in/./*
EXAMPLE: Distributing work to local and remote computers Convert *.mp3 to *.ogg running one process per CPU on local computer and server2:
parallel --trc {.}.ogg -S server2,: \ 'mpg321 -w - {} | oggenc -q0 - -o {.}.ogg' ::: *.mp3
EXAMPLE: Running the same command on remote computers To run the command uptime on remote computers you can do:
parallel --tag --nonall -S server1,server2 uptime
--nonall reads no arguments. If you have a list of jobs you want to run on each computer you can do:
parallel --tag --onall -S server1,server2 echo ::: 1 2 3
Remove --tag if you do not want the sshlogin added before the output.
If you have a lot of hosts use '-j0' to access more hosts in parallel.
EXAMPLE: Running 'sudo' on remote computers Put the password into passwordfile then run:
parallel --ssh 'cat passwordfile | ssh' --nonall \ -S user@server1,user@server2 sudo -S ls -l /root
EXAMPLE: Using remote computers behind NAT wall If the workers are behind a NAT wall, you need some trickery to get to them.
If you can ssh to a jumphost, and reach the workers from there, then the obvious solution would be this, but it does not work:
parallel --ssh 'ssh jumphost ssh' -S host1 echo ::: DOES NOT WORK
It does not work because the command is dequoted by ssh twice where as GNU parallel only expects it to be dequoted once.
You can use a bash function and have GNU parallel quote the command:
jumpssh() { ssh -A jumphost ssh $(parallel --shellquote ::: "$@"); } export -f jumpssh parallel --ssh jumpssh -S host1 echo ::: this works
Or you can instead put this in ~/.ssh/config:
Host host1 host2 host3 ProxyCommand ssh jumphost.domain nc -w 1 %h 22
It requires nc(netcat) to be installed on jumphost. With this you can simply:
parallel -S host1,host2,host3 echo ::: This does work
No jumphost, but port forwards
If there is no jumphost but each server has port 22 forwarded from the firewall (e.g. the firewall's port 22001 = port 22 on host1, 22002 = host2, 22003 = host3) then you can use ~/.ssh/config:
Host host1.v Port 22001 Host host2.v Port 22002 Host host3.v Port 22003 Host *.v Hostname firewall
And then use host{1..3}.v as normal hosts:
parallel -S host1.v,host2.v,host3.v echo ::: a b c
No jumphost, no port forwards
If ports cannot be forwarded, you need some sort of VPN to traverse the NAT-wall. TOR is one options for that, as it is very easy to get working.
You need to install TOR and setup a hidden service. In torrc put:
HiddenServiceDir /var/lib/tor/hidden_service/ HiddenServicePort 22 127.0.0.1:22
Then start TOR: /etc/init.d/tor restart
The TOR hostname is now in /var/lib/tor/hidden_service/hostname and is something similar to izjafdceobowklhz.onion. Now you simply prepend torsocks to ssh:
parallel --ssh 'torsocks ssh' -S izjafdceobowklhz.onion \ -S zfcdaeiojoklbwhz.onion,auclucjzobowklhi.onion echo ::: a b c
If not all hosts are accessible through TOR:
parallel -S 'torsocks ssh izjafdceobowklhz.onion,host2,host3' \ echo ::: a b c
See more ssh tricks on https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts
EXAMPLE: Use outrun instead of ssh outrun lets you run a command on a remote server. outrun sets up a connection to access files at the source server, and automatically transfers files. outrun must be installed on the remote system.
You can use outrun in an sshlogin this way:
parallel -S 'outrun user@server eval' command
EXAMPLE: Slurm cluster The Slurm Workload Manager is used in many clusters.
Here is a simple example of using GNU parallel to call srun:
#SBATCH --time 00:02:00 #SBATCH --ntasks=4 #SBATCH --job-name GnuParallelDemo #SBATCH --output gnuparallel.out
module purge module load gnu_parallel
my_parallel="parallel --delay .2 -j $SLURM_NTASKS" my_srun="srun --export=all --exclusive -n1 --cpus-per-task=1 --cpu-bind=cores" $my_parallel "$my_srun" echo This is job {} ::: {1..20}
EXAMPLE: Parallelizing rsync rsync is a great tool, but sometimes it will not fill up the available bandwidth. Running multiple rsync in parallel can fix this.
cd src-dir find . -type f | parallel -j10 -X rsync -zR -Ha ./{} fooserver:/dest-dir/
Adjust -j10 until you find the optimal number.
rsync -R will create the needed subdirectories, so all files are not put into a single dir. The ./ is needed so the resulting command looks similar to:
rsync -zR ././sub/dir/file fooserver:/dest-dir/
The /./ is what rsync -R works on.
If you are unable to push data, but need to pull them and the files are called digits.png (e.g. 000000.png) you might be able to do:
seq -w 0 99 | parallel rsync -Havessh fooserver:src/*{}.png destdir/
EXAMPLE: Use multiple inputs in one command Copy files like foo.es.ext to foo.ext:
ls *.es.* | perl -pe 'print; s/\.es//' | parallel -N2 cp {1} {2}
The perl command spits out 2 lines for each input. GNU parallel takes 2 inputs (using -N2) and replaces {1} and {2} with the inputs.
Count in binary:
parallel -k echo ::: 0 1 ::: 0 1 ::: 0 1 ::: 0 1 ::: 0 1 ::: 0 1
Print the number on the opposing sides of a six sided die:
parallel --link -a <(seq 6) -a <(seq 6 -1 1) echo parallel --link echo :::: <(seq 6) <(seq 6 -1 1)
Convert files from all subdirs to PNG-files with consecutive numbers (useful for making input PNG's for ffmpeg):
parallel --link -a <(find . -type f | sort) \ -a <(seq $(find . -type f|wc -l)) convert {1} {2}.png
Alternative version:
find . -type f | sort | parallel convert {} {#}.png
EXAMPLE: Use a table as input Content of table_file.tsv:
foo<TAB>bar baz <TAB> quux
To run:
cmd -o bar -i foo cmd -o quux -i baz
you can run:
parallel -a table_file.tsv --colsep '\t' cmd -o {2} -i {1}
Note: The default for GNU parallel is to remove the spaces around the columns. To keep the spaces:
parallel -a table_file.tsv --trim n --colsep '\t' cmd -o {2} -i {1}
EXAMPLE: Output to database GNU parallel can output to a database table and a CSV-file:
dburl=csv:///%2Ftmp%2Fmydir dbtableurl=$dburl/mytable.csv parallel --sqlandworker $dbtableurl seq ::: {1..10}
It is rather slow and takes up a lot of CPU time because GNU parallel parses the whole CSV file for each update.
A better approach is to use an SQLite-base and then convert that to CSV:
dburl=sqlite3:///%2Ftmp%2Fmy.sqlite dbtableurl=$dburl/mytable parallel --sqlandworker $dbtableurl seq ::: {1..10} sql $dburl '.headers on' '.mode csv' 'SELECT * FROM mytable;'
This takes around a second per job.
If you have access to a real database system, such as PostgreSQL, it is even faster:
dburl=pg://user:pass@host/mydb dbtableurl=$dburl/mytable parallel --sqlandworker $dbtableurl seq ::: {1..10} sql $dburl \ "COPY (SELECT * FROM mytable) TO stdout DELIMITER ',' CSV HEADER;"
Or MySQL:
dburl=mysql://user:pass@host/mydb dbtableurl=$dburl/mytable parallel --sqlandworker $dbtableurl seq ::: {1..10} sql -p -B $dburl "SELECT * FROM mytable;" > mytable.tsv perl -pe 's/"/""/g; s/\t/","/g; s/^/"/; s/$/"/; %s=("\\" => "\\", "t" => "\t", "n" => "\n"); s/\\([\\tn])/$s{$1}/g;' mytable.tsv
EXAMPLE: Output to CSV-file for R If you have no need for the advanced job distribution control that a database provides, but you simply want output into a CSV file that you can read into R or LibreCalc, then you can use --results:
parallel --results my.csv seq ::: 10 20 30 R > mydf <- read.csv("my.csv"); > print(mydf[2,]) > write(as.character(mydf[2,c("Stdout")]),'')
EXAMPLE: Use XML as input The show Aflyttet on Radio 24syv publishes an RSS feed with their audio podcasts on: http://arkiv.radio24syv.dk/audiopodcast/channel/4466232
Using xpath you can extract the URLs for 2019 and download them using GNU parallel:
wget -O - http://arkiv.radio24syv.dk/audiopodcast/channel/4466232 | \ xpath -e "//pubDate[contains(text(),'2019')]/../enclosure/@url" | \ parallel -u wget '{= s/ url="//; s/"//; =}'
EXAMPLE: Run the same command 10 times If you want to run the same command with the same arguments 10 times in parallel you can do:
seq 10 | parallel -n0 my_command my_args
EXAMPLE: Working as cat | sh. Resource inexpensive jobs and evaluation GNU parallel can work similar to cat | sh.
A resource inexpensive job is a job that takes very little CPU, disk I/O and network I/O. Ping is an example of a resource inexpensive job. wget is too - if the webpages are small.
The content of the file jobs_to_run:
ping -c 1 10.0.0.1 wget http://example.com/status.cgi?ip=10.0.0.1 ping -c 1 10.0.0.2 wget http://example.com/status.cgi?ip=10.0.0.2 ... ping -c 1 10.0.0.255 wget http://example.com/status.cgi?ip=10.0.0.255
To run 100 processes simultaneously do:
parallel -j 100 < jobs_to_run
As there is not a command the jobs will be evaluated by the shell.
EXAMPLE: Call program with FASTA sequence FASTA files have the format:
>Sequence name1 sequence sequence continued >Sequence name2 sequence sequence continued more sequence
To call myprog with the sequence as argument run:
cat file.fasta | parallel --pipe -N1 --recstart '>' --rrs \ 'read a; echo Name: "$a"; myprog $(tr -d "\n")'
EXAMPLE: Call program with interleaved FASTQ records FASTQ files have the format:
@M10991:61:000000000-A7EML:1:1101:14011:1001 1:N:0:28 CTCCTAGGTCGGCATGATGGGGGAAGGAGAGCATGGGAAGAAATGAGAGAGTAGCAAGG + #8BCCGGGGGFEFECFGGGGGGGGG@;FFGGGEG@FF<EE<@FFC,CEGCCGGFF<FGF
Interleaved FASTQ starts with a line like these:
@HWUSI-EAS100R:6:73:941:1973#0/1 @EAS139:136:FC706VJ:2:2104:15343:197393 1:Y:18:ATCACG @EAS139:136:FC706VJ:2:2104:15343:197393 1:N:18:1
where '/1' and ' 1:' determines this is read 1.
This will cut big.fq into one chunk per CPU core and pass it on stdin (standard input) to the program fastq-reader:
parallel --pipepart -a big.fq --block -1 --regexp \ --recend '\n' --recstart '@.*(/1| 1:.*)\n[A-Za-z\n\.~]' \ fastq-reader
EXAMPLE: Processing a big file using more CPUs To process a big file or some output you can use --pipe to split up the data into blocks and pipe the blocks into the processing program.
If the program is gzip -9 you can do:
cat bigfile | parallel --pipe --recend '' -k gzip -9 > bigfile.gz
This will split bigfile into blocks of 1 MB and pass that to gzip -9 in parallel. One gzip will be run per CPU. The output of gzip -9 will be kept in order and saved to bigfile.gz
gzip works fine if the output is appended, but some processing does not work like that - for example sorting. For this GNU parallel can put the output of each command into a file. This will sort a big file in parallel:
cat bigfile | parallel --pipe --files sort |\ parallel -Xj1 sort -m {} ';' rm {} >bigfile.sort
Here bigfile is split into blocks of around 1MB, each block ending in '\n' (which is the default for --recend). Each block is passed to sort and the output from sort is saved into files. These files are passed to the second parallel that runs sort -m on the files before it removes the files. The output is saved to bigfile.sort.
GNU parallel's --pipe maxes out at around 100 MB/s because every byte has to be copied through GNU parallel. But if bigfile is a real (seekable) file GNU parallel can by-pass the copying and send the parts directly to the program:
parallel --pipepart --block 100m -a bigfile --files sort |\ parallel -Xj1 sort -m {} ';' rm {} >bigfile.sort
EXAMPLE: Grouping input lines When processing with --pipe you may have lines grouped by a value. Here is my.csv:
Transaction Customer Item 1       a       53 2       b       65 3       b       82 4       c       96 5       c       67 6       c       13 7       d       90 8       d       43 9       d       91 10      d       84 11      e       72 12      e       102 13      e       63 14      e       56 15      e       74
Let us assume you want GNU parallel to process each customer. In other words: You want all the transactions for a single customer to be treated as a single record.
To do this we preprocess the data with a program that inserts a record separator before each customer (column 2 = $F[1]). Here we first make a 50 character random string, which we then use as the separator:
sep=`perl -e 'print map { ("a".."z","A".."Z")[rand(52)] } (1..50);'` cat my.csv | \ perl -ape '$F[1] ne $l and print "'$sep'"; $l = $F[1]' | \ parallel --recend $sep --rrs --pipe -N1 wc
If your program can process multiple customers replace -N1 with a reasonable --blocksize.
EXAMPLE: Running more than 250 jobs workaround If you need to run a massive amount of jobs in parallel, then you will likely hit the filehandle limit which is often around 250 jobs. If you are super user you can raise the limit in /etc/security/limits.conf but you can also use this workaround. The filehandle limit is per process. That means that if you just spawn more GNU parallels then each of them can run 250 jobs. This will spawn up to 2500 jobs:
cat myinput |\ parallel --pipe -N 50 --roundrobin -j50 parallel -j50 your_prg
This will spawn up to 62500 jobs (use with caution - you need 64 GB RAM to do this, and you may need to increase /proc/sys/kernel/pid_max):
cat myinput |\ parallel --pipe -N 250 --roundrobin -j250 parallel -j250 your_prg
EXAMPLE: Working as mutex and counting semaphore The command sem is an alias for parallel --semaphore.
A counting semaphore will allow a given number of jobs to be started in the background.  When the number of jobs are running in the background, GNU sem will wait for one of these to complete before starting another command. sem --wait will wait for all jobs to complete.
Run 10 jobs concurrently in the background:
for i in *.log ; do echo $i sem -j10 gzip $i ";" echo done done sem --wait
A mutex is a counting semaphore allowing only one job to run. This will edit the file myfile and prepends the file with lines with the numbers 1 to 3.
seq 3 | parallel sem sed -i -e '1i{}' myfile
As myfile can be very big it is important only one process edits the file at the same time.
Name the semaphore to have multiple different semaphores active at the same time:
seq 3 | parallel sem --id mymutex sed -i -e '1i{}' myfile
EXAMPLE: Mutex for a script Assume a script is called from cron or from a web service, but only one instance can be run at a time. With sem and --shebang-wrap the script can be made to wait for other instances to finish. Here in bash:
#!/usr/bin/sem --shebang-wrap -u --id $0 --fg /bin/bash
echo This will run sleep 5 echo exclusively
Here perl:
#!/usr/bin/sem --shebang-wrap -u --id $0 --fg /usr/bin/perl
print "This will run "; sleep 5; print "exclusively\n";
Here python:
#!/usr/local/bin/sem --shebang-wrap -u --id $0 --fg /usr/bin/python
import time print "This will run "; time.sleep(5) print "exclusively";
EXAMPLE: Start editor with filenames from stdin (standard input) You can use GNU parallel to start interactive programs like emacs or vi:
cat filelist | parallel --tty -X emacs cat filelist | parallel --tty -X vi
If there are more files than will fit on a single command line, the editor will be started again with the remaining files.
EXAMPLE: Running sudo sudo requires a password to run a command as root. It caches the access, so you only need to enter the password again if you have not used sudo for a while.
The command:
parallel sudo echo ::: This is a bad idea
is no good, as you would be prompted for the sudo password for each of the jobs. You can either do:
sudo echo This parallel sudo echo ::: is a good idea
sudo parallel echo ::: This is a good idea
This way you only have to enter the sudo password once.
EXAMPLE: GNU Parallel as queue system/batch manager GNU parallel can work as a simple job queue system or batch manager.  The idea is to put the jobs into a file and have GNU parallel read from that continuously. As GNU parallel will stop at end of file we use tail to continue reading:
true >jobqueue; tail -n+0 -f jobqueue | parallel
To submit your jobs to the queue:
echo my_command my_arg >> jobqueue
You can of course use -S to distribute the jobs to remote computers:
true >jobqueue; tail -n+0 -f jobqueue | parallel -S ..
Output only will be printed when reading the next input after a job has finished: So you need to submit a job after the first has finished to see the output from the first job.
If you keep this running for a long time, jobqueue will grow. A way of removing the jobs already run is by making GNU parallel stop when it hits a special value and then restart. To use --eof to make GNU parallel exit, tail also needs to be forced to exit:
true >jobqueue; while true; do tail -n+0 -f jobqueue | (parallel -E StOpHeRe -S ..; echo GNU Parallel is now done; perl -e 'while(<>){/StOpHeRe/ and last};print <>' jobqueue > j2; (seq 1000 >> jobqueue &); echo Done appending dummy data forcing tail to exit) echo tail exited; mv j2 jobqueue done
In some cases you can run on more CPUs and computers during the night:
# Day time echo 50% > jobfile cp day_server_list ~/.parallel/sshloginfile # Night time echo 100% > jobfile cp night_server_list ~/.parallel/sshloginfile tail -n+0 -f jobqueue | parallel --jobs jobfile -S ..
GNU parallel discovers if jobfile or ~/.parallel/sshloginfile changes.
EXAMPLE: GNU Parallel as dir processor If you have a dir in which users drop files that needs to be processed you can do this on GNU/Linux (If you know what inotifywait is called on other platforms file a bug report):
inotifywait -qmre MOVED_TO -e CLOSE_WRITE --format %w%f my_dir |\ parallel -u echo
This will run the command echo on each file put into my_dir or subdirs of my_dir.
inotifywait -qmre MOVED_TO -e CLOSE_WRITE --format %w%f my_dir |\ parallel -S ..  -u echo
If the files to be processed are in a tar file then unpacking one file and processing it immediately may be faster than first unpacking all files. Set up the dir processor as above and unpack into the dir.
Using GNU parallel as dir processor has the same limitations as using GNU parallel as queue system/batch manager.
EXAMPLE: Locate the missing package If you have downloaded source and tried compiling it, you may have seen:
$ ./configure [...] checking for something.h... no configure: error: "libsomething not found"
Often it is not obvious which package you should install to get that file. Debian has `apt-file` to search for a file. `tracefile` from https://gitlab.com/ole.tange/tangetools can tell which files a program tried to access. In this case we are interested in one of the last files:
$ tracefile -un ./configure | tail | parallel -j0 apt-file search
--round-robin, --pipe-part, --shard, --bin and --group-by are all specialized versions of --pipe.
In the following n is the number of jobslots given by --jobs. A record starts with --recstart and ends with --recend. It is typically a full line. A chunk is a number of full records that is approximately the size of a block. A block can contain half records, a chunk cannot.
--pipe starts one job per chunk. It reads blocks from stdin (standard input). It finds a record end near a block border and passes a chunk to the program.
--pipe-part starts one job per chunk - just like normal --pipe. It first finds record endings near all block borders in the file and then starts the jobs. By using --block -1 it will set the block size to 1/n * size- of-file. Used this way it will start n jobs in total.
--round-robin starts n jobs in total. It reads a block and passes a chunk to whichever job is ready to read. It does not parse the content except for identifying where a record ends to make sure it only passes full records.
--shard starts n jobs in total. It parses each line to read the value in the given column. Based on this value the line is passed to one of the n jobs. All lines having this value will be given to the same jobslot.
--bin works like --shard but the value of the column is the jobslot number it will be passed to. If the value is bigger than n, then n will be subtracted from the value until the values is smaller than or equal to n.
--group-by starts one job per chunk. Record borders are not given by --recend/--recstart. Instead a record is defined by a number of lines having the same value in a given column. So the value of a given column changes at a chunk border. With --pipe every line is parsed, with --pipe-part only a few lines are parsed to find the chunk border.
--group-by can be combined with --round-robin or --pipe-part.
Many numerical arguments in GNU parallel can be postfixed with K, M, G, T, P, k, m, g, t, or p which would multiply the number with 1024, 1048576, 1073741824, 1099511627776, 1125899906842624, 1000, 1000000, 1000000000, 1000000000000, or 1000000000000000, respectively.
You can even give it as a math expression. E.g. 1000000 can be written as 1M-12*2.024*2k.
GNU parallel is very liberal in quoting. You only need to quote characters that have special meaning in shell:
( ) $ ` ' " < > ; | \
and depending on context these needs to be quoted, too:
~ & # ! ? space * {
Therefore most people will never need more quoting than putting '\' in front of the special characters.
Often you can simply put \' around every ':
perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"' file
can be quoted:
parallel perl -ne \''/^\S+\s+\S+$/ and print $ARGV,"\n"'\' ::: file
However, when you want to use a shell variable you need to quote the $-sign. Here is an example using $PARALLEL_SEQ. This variable is set by GNU parallel itself, so the evaluation of the $ must be done by the sub shell started by GNU parallel:
seq 10 | parallel -N2 echo seq:\$PARALLEL_SEQ arg1:{1} arg2:{2}
If the variable is set before GNU parallel starts you can do this:
VAR=this_is_set_before_starting echo test | parallel echo {} $VAR
Prints: test this_is_set_before_starting
It is a little more tricky if the variable contains more than one space in a row:
VAR="two  spaces  between  each  word" echo test | parallel echo {} \'"$VAR"\'
Prints: test two  spaces  between  each  word
If the variable should not be evaluated by the shell starting GNU parallel but be evaluated by the sub shell started by GNU parallel, then you need to quote it:
echo test | parallel VAR=this_is_set_after_starting \; echo {} \$VAR
Prints: test this_is_set_after_starting
It is a little more tricky if the variable contains space:
echo test |\ parallel VAR='"two  spaces  between  each  word"' echo {} \'"$VAR"\'
$$ is the shell variable containing the process id of the shell. This will print the process id of the shell running GNU parallel:
seq 10 | parallel echo $$
And this will print the process ids of the sub shells started by GNU parallel.
seq 10 | parallel echo \$\$
If the special characters should not be evaluated by the sub shell then you need to protect it against evaluation from both the shell starting GNU parallel and the sub shell:
echo test | parallel echo {} \\\$VAR
Prints: test $VAR
GNU parallel can protect against evaluation by the sub shell by using -q:
echo test | parallel -q echo {} \$VAR
This is particularly useful if you have lots of quoting. If you want to run a perl script like this:
It needs to be quoted like one of these:
ls | parallel perl -ne '/^\\S+\\s+\\S+\$/\ and\ print\ \$ARGV,\"\\n\"' ls | parallel perl -ne \''/^\S+\s+\S+$/ and print $ARGV,"\n"'\'
Notice how spaces, \'s, "'s, and $'s need to be quoted. GNU parallel can do the quoting by using option -q:
ls | parallel -q  perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"'
However, this means you cannot make the sub shell interpret special characters. For example because of -q this WILL NOT WORK:
ls *.gz | parallel -q "zcat {} >{.}" ls *.gz | parallel -q "zcat {} | bzip2 >{.}.bz2"
because > and | need to be interpreted by the sub shell.
If you get errors like:
sh: -c: line 0: syntax error near unexpected token sh: Syntax error: Unterminated quoted string sh: -c: line 0: unexpected EOF while looking for matching `'' sh: -c: line 1: syntax error: unexpected end of file zsh:1: no matches found:
then you might try using -q.
If you are using bash process substitution like <(cat foo) then you may try -q and prepending command with bash -c:
ls | parallel -q bash -c 'wc -c <(echo {})'
Or for substituting output:
ls | parallel -q bash -c \ 'tar c {} | tee >(gzip >{}.tar.gz) | bzip2 >{}.tar.bz2'
Conclusion: To avoid dealing with the quoting problems it may be easier just to write a small script or a function (remember to export -f the function) and have GNU parallel call that.
If you want a list of the jobs currently running you can run:
killall -USR1 parallel
GNU parallel will then print the currently running jobs on stderr (standard error).
If you regret starting a lot of jobs you can simply break GNU parallel, but if you want to make sure you do not have half-completed jobs you should send the signal SIGHUP to GNU parallel:
killall -HUP parallel
This will tell GNU parallel to not start any new jobs, but wait until the currently running jobs are finished before exiting.
$PARALLEL_HOME Dir where GNU parallel stores config files, semaphores, and caches information between invocations. Default: $HOME/.parallel.
$PARALLEL_ARGHOSTGROUPS When using --hostgroups GNU parallel sets this to the hostgroups of the job.
Remember to quote the $, so it gets evaluated by the correct shell. Or use --plus and {agrp}.
$PARALLEL_HOSTGROUPS When using --hostgroups GNU parallel sets this to the hostgroups of the sshlogin that the job is run on.
Remember to quote the $, so it gets evaluated by the correct shell. Or use --plus and {hgrp}.
$PARALLEL_JOBSLOT Set by GNU parallel and can be used in jobs run by GNU parallel. Remember to quote the $, so it gets evaluated by the correct shell. Or use --plus and {slot}.
$PARALLEL_JOBSLOT is the jobslot of the job. It is equal to {%} unless the job is being retried. See {%} for details.
$PARALLEL_PID Set by GNU parallel and can be used in jobs run by GNU parallel. Remember to quote the $, so it gets evaluated by the correct shell.
This makes it possible for the jobs to communicate directly to GNU parallel.
Example: If each of the jobs tests a solution and one of jobs finds the solution the job can tell GNU parallel not to start more jobs by: kill -HUP $PARALLEL_PID. This only works on the local computer.
$PARALLEL_RSYNC_OPTS Options to pass on to rsync. Defaults to: -rlDzR.
$PARALLEL_SHELL Use this shell for the commands run by GNU parallel:
• $PARALLEL_SHELL. If undefined use:
• The shell that started GNU parallel. If that cannot be determined:
• $SHELL. If undefined use:
• /bin/sh
$PARALLEL_SSH GNU parallel defaults to using the ssh command for remote access. This can be overridden with $PARALLEL_SSH, which again can be overridden with --ssh. It can also be set on a per server basis (see --sshlogin).
$PARALLEL_SSHHOST Set by GNU parallel and can be used in jobs run by GNU parallel. Remember to quote the $, so it gets evaluated by the correct shell. Or use --plus and {host}.
$PARALLEL_SSHHOST is the host part of an sshlogin line. E.g.
4//usr/bin/specialssh user@host
$PARALLEL_SSHLOGIN Set by GNU parallel and can be used in jobs run by GNU parallel. Remember to quote the $, so it gets evaluated by the correct shell. Or use --plus and {sshlogin}.
The value is the sshlogin line with number of cores removed. E.g.
/usr/bin/specialssh user@host
$PARALLEL_SEQ Set by GNU parallel and can be used in jobs run by GNU parallel. Remember to quote the $, so it gets evaluated by the correct shell.
$PARALLEL_SEQ is the sequence number of the job running.
seq 10 | parallel -N2 \ echo seq:'$'PARALLEL_SEQ arg1:{1} arg2:{2}
{#} is a shorthand for $PARALLEL_SEQ.
$PARALLEL_TMUX Path to tmux. If unset the tmux in $PATH is used.
$TMPDIR  Directory for temporary files. See: --tmpdir.
$PARALLEL The environment variable $PARALLEL will be used as default options for GNU parallel. If the variable contains special shell characters (e.g. $, *, or space) then these need to be to be escaped with \.
cat list | parallel -j1 -k -v ls cat list | parallel -j1 -k -v -S"myssh user@server" ls
can be written as:
cat list | PARALLEL="-kvj1" parallel ls cat list | PARALLEL='-kvj1 -S myssh\ user@server' \ parallel echo
Notice the \ after 'myssh' is needed because 'myssh' and 'user@server' must be one argument.
DEFAULT PROFILE (CONFIG FILE) The global configuration file /etc/parallel/config, followed by user configuration file ~/.parallel/config (formerly known as .parallelrc) will be read in turn if they exist.  Lines starting with '#' will be ignored. The format can follow that of the environment variable $PARALLEL, but it is often easier to simply put each option on its own line.
Options on the command line take precedence, followed by the environment variable $PARALLEL, user configuration file ~/.parallel/config, and finally the global configuration file /etc/parallel/config.
Note that no file that is read for options, nor the environment variable $PARALLEL, may contain retired options such as --tollef.
If --profile set, GNU parallel will read the profile from that file rather than the global or user configuration files. You can have multiple --profiles.
Profiles are searched for in ~/.parallel. If the name starts with / it is seen as an absolute path. If the name starts with ./ it is seen as a relative path from current dir.