Bash — parallel command execution
Published in
2 min readAug 8, 2021
There are three ways to execute commands in parallel using bash
- plain bash: no external dependencies to be installed
- parallel: a very smart tool to execute commands in parallel
- xargs: a swiss knife tool that every Linux user must know
plain bash
Save the following as configuration.cfg
/var,*.log,log_res.txt
/var,*.gz,gzip_res.txt
Save the following as bash_plain_parallel.sh
#!/bin/bash
for n in $(cat ./configuration.cfg)
do
DIRECTORY=$(echo $n | cut -d "," -f 1)
FILES=$(echo $n | cut -d "," -f 2)
RESULTS=$(echo $n | cut -d "," -f 3)
CMD="find "${DIRECTORY}" -name "${FILES}" > "${RESULTS}
eval ${CMD} &
done
wait
echo "Script execution completed"
make the file executable
$ chmod +x bash_plain_parallel.sh
Explaining the script
The script reads each file of configuration.cfg, each line is stored in $n variable
for n in $(cat ./configuration.cfg)
Then each $n configuration line is spitted with “,” as delimiter
DIRECTORY=$(echo $n | cut -d "," -f 1)
FILES=$(echo $n | cut -d "," -f 2)
RESULTS=$(echo $n | cut -d "," -f 3)