I want to know how I can send the command(s) spawned by xargs to background. For example, consider
find . -type f -mtime +7 | tee compressedP.list | xargs compress
I tried
find . -type f -mtime +7 | tee compressedP.list | xargs -i{} compress {} &
.. and as unexpected, it seems to send xargs to the background instead?
How do I make each instance of the compress command go to the background?
The xargs command builds and executes commands provided through the standard input. It takes the input and converts it into a command argument for another command. This feature is particularly useful in file management, where xargs is used in combination with rm , cp , mkdir , and other similar commands.
xargs reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is echo) one or more times with any initial-arguments followed by items read from standard input.
The -I option changes the way the new command lines are built. Instead of adding as many arguments as possible at a time, xargs will take one name at a time from its input, look for the given token ( {} here) and replace that with the name.
xargs is a Unix command used to build and execute commands from the standard input. You can combine it with other powerful Unix commands, such as grep , awk , etc., by using pipes. In simple terms, it passes the output of a command as the input of another. It works on most Unix-like operating systems.
Use the --max-procs
/ -P
option to run xargs targets in the background. From the man page of GNU xargs version 4.2.27:
--max-procs=max-procs, -P max-procs
Run up to max-procs processes at a time; the default is 1. If max-procs is 0, xargs will run as many processes as possible at a time. Use the -n option with -P; otherwise chances are that only one exec will be done.
(You may want to combine this with -n 1
to makes sure that there is a new process for each file you want to compress)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With