Skip to content

Instantly share code, notes, and snippets.

@copyleftdev
Created August 24, 2024 06:51
Show Gist options
  • Save copyleftdev/3704d8d0baee6ba85cb2b759cd386e12 to your computer and use it in GitHub Desktop.
Save copyleftdev/3704d8d0baee6ba85cb2b759cd386e12 to your computer and use it in GitHub Desktop.
GNU Parallel Mastery: The Ultimate Cheat Sheet

πŸš€ GNU Parallel Mastery: The Ultimate Cheat Sheet

πŸ“Š Basic Usage

Command Description Example
parallel echo ::: A B C πŸ”€ Process items in parallel Output: A, B, C (in any order)
parallel echo {} ::: *.txt πŸ” Use {} as placeholder Echoes names of all .txt files
cat file.txt | parallel echo πŸ“₯ Read input from stdin Processes each line of file.txt
parallel -j4 command ::: item1 item2 item3 πŸ”’ Limit to 4 jobs at a time Runs 'command' on items, max 4 parallel

πŸ”§ Input Sources

Syntax Description Example
::: πŸ“Š Command line input parallel echo ::: A B C
:::: πŸ“‚ File input (one item per line) parallel echo :::: items.txt
:::+ πŸ”— Combine arguments from multiple sources parallel echo ::: A B C :::+ X Y Z
-a file πŸ“„ Read input from file parallel -a items.txt echo

πŸ”€ Job Control

Option Description Example
-j N πŸ”’ Run N jobs in parallel parallel -j4 command ::: 1 2 3 4 5 6
-k πŸ”€ Keep output in order parallel -k echo ::: C B A
--halt soon,fail=1 πŸ›‘ Stop all jobs on first failure parallel --halt soon,fail=1 command ::: 1 2 3
--eta ⏱️ Show estimated time of completion parallel --eta command ::: {1..100}
--bar πŸ“Š Display progress bar parallel --bar command ::: {1..100}

πŸ” Replacement Strings

String Description Example
{} πŸ”„ Input line parallel echo {} ::: A B C
{.} πŸ”ͺ Input line without extension parallel echo {.} ::: file.txt
{/} πŸ“‚ Basename of input line parallel echo {/} ::: /path/to/file.txt
{//} πŸ“ Dirname of input line parallel echo {//} ::: /path/to/file.txt
{#} πŸ”’ Sequence number of job parallel echo {#} {} ::: A B C
{%} πŸ”’ Job slot number parallel -j2 echo {%} {} ::: A B C D

πŸ“‘ Remote Execution

Command Description Example
--sshlogin user@host1,user@host2 πŸ–₯️ Run on remote hosts parallel --sshlogin user@host1,user@host2 command ::: 1 2 3
--transfer πŸ“€ Transfer files to remote parallel --transfer --sshlogin user@host command ::: file1 file2
--return {.}.out πŸ“₯ Return output files from remote parallel --return {.}.out --sshlogin user@host command ::: file1 file2

πŸ”§ Advanced Techniques

Technique Description Example
--pipe 🚿 Split input stream cat bigfile.txt | parallel --pipe grep 'pattern'
--xargs πŸ”¨ Emulate xargs find . -name '*.jpg' | parallel --xargs convert {} {.}.png
--shebang πŸ“œ Use in scripts #!/usr/bin/parallel --shebang
--dry-run πŸƒβ€β™‚οΈ Show commands without executing parallel --dry-run echo ::: A B C
--results outdir πŸ“ Store output in directory parallel --results outdir command ::: 1 2 3

πŸ† Pro Tips

  1. πŸ“š Use man parallel_tutorial for an extensive guide
  2. πŸ” Use parallel --citation in your publications if you use GNU Parallel
  3. πŸ”’ Use {1}, {2}, etc. for multiple input sources
  4. πŸ“Š Use --joblog logfile to keep a log of executed jobs
  5. πŸ”„ Use --resume to resume interrupted runs
  6. πŸ”’ Use --compress when transferring large amounts of data to remote hosts
  7. πŸ”§ Use --tag to prefix each line of output with the input line
  8. πŸ“ˆ Use --progress for a live progress update
  9. πŸ” Use --regexp to use regular expressions in replacement strings
  10. πŸ”€ Use parallel --link to process input sources in lockstep

πŸ“œ Example: Image Processing

#!/usr/bin/env bash

# Resize all .jpg images to 50% of their original size
find . -name "*.jpg" | parallel convert {} -resize 50% {.}_resized.jpg

# Convert all .png images to .jpg in parallel, utilizing all CPU cores
parallel convert {} {.}.jpg ::: *.png

# Process images on remote servers
parallel --sshlogin user@server1,user@server2 --transfer \
    "convert {} -resize 50% {.}_resized.jpg && rm {}" ::: *.jpg

# Generate thumbnails for all images, showing progress
parallel --bar "convert {} -resize 100x100^ -gravity center -extent 100x100 {}_thumb.jpg" ::: *.jpg

# Process large log file in parallel
cat large_log.txt | parallel --pipe grep 'ERROR' > errors.txt

# Parallel download of files
cat urls.txt | parallel --eta wget {}

Remember, GNU Parallel is a powerful tool. Use it responsibly and always test your commands with --dry-run before executing them on important data! πŸš€πŸ’»

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment