Linux Xargs
Links: 104 Linux Index
Always on the right hand side of the pipe.
Why use xargs¶
- Many Linux commands support both standard input (Stdin) and command arguments, such as
sed,grep, andawk. - On the other hand, some commands don’t read Stdin and only support arguments
cp,rm,ls, andmv. - So, when we want to execute these commands with Stdin as the input, we need to convert Stdin into arguments. The
xargscommand is born to do that. - It is usually used in combination with other commands through piping.
echo "file1 file2 file3" | xargs touch- In the example above, we are piping the standard input to
xargs, and the command is run for each argument, creating three files. This is the same as if you would run:touch file1 file2 file3
- In the example above, we are piping the standard input to
Individual lines¶
- The
-n(--max-args) option specifies the number of arguments to be passed to the given command. xargsruns the specified command as many times as necessary until all arguments are exhausted.echo "file1 file2 file3" | xargs -n 1 -t touch: The touch command is executed separately for each argument- Useful when some commands won't take multiple arguments
Useful switches¶
- To print the command on the terminal before executing it use the
-toption - Make
xargsask for permission before executing the command using-p fd test | xargs -t -p -n 1 rm -rf: In this examplexargswould ask for permission for all files. Important example to understand-n 1flag.fd test | xargs -t -p rm -rf: Will only ask for permission once-Pnumber of processes to use
With find¶
xargsis most often used in combination with the command.- You can use
findto search for specific files and then usexargsto perform operations on those files.
- You can use
find /var/www/.cache -type f | xargs rm -f- In the above example,
findwill print the full names of all files inside the/var/www/.cachedirectory andxargswill pass the file paths to thermcommand.
- In the above example,
Files with spaces¶
xargsdoes not automatically include files which contain blank spaces in their names.- To include those files too, use the
-print0option forfind, and the-0option forxargsfind [location] -name "[search-term]" -type f -print0 | xargs -0 [command]
- Example
- The above happened because of space between the file name "hello world"
- This works since
-print0uses\0(null) as separator. This can be seen using hexdump
xargs vs exec¶
The find command supports the -exec option that allows arbitrary commands to be performed on found files. The following are equivalent.
But xargs is far more efficient.
You can verify this by using time in front of the command.
With grep¶
- Using
xargswithgrepfind • -name '*. txt' | xargs grep 'example'- The example above searched for all the files with the
.txtextension and piped them toxargs, which then executed thegrepcommand on them.
Run multiple commands with xargs¶
- It is possible to run multiple commands with
xargsby using the-Iflag. This replaces occurrences of the argument with the argument passed toxargs. - The following echos a string and creates a folder.
Insert arguments at a particular position¶
- The
xargscommand offers options to insert the listed arguments at some arbitrary position other than the end of the command line. - The
-Ioption takes a string that gets replaced with the supplied input before the command executes.- Although this string can be any set of characters, a common choice for it is
%.
- Although this string can be any set of characters, a common choice for it is
find ./log -type f -name "*.log" | xargs -I % mv % backup/- Another way :
find ./log -type f -name "*.log" | xargs bash "mv $1 backup/" - If you use it again and again then the same argument will be used
ls * | xargs -n 1 -I % cp % %.bak- Create a
.bakfile for all the files in the directory
Types of arguments¶
- Commands can have multiple arguments in two scenarios:
- All command arguments –
COMMAND ARG1 ARG2 ARG3- All the above examples were all command arguments
- Option arguments –
COMMAND -a ARG1 -b ARG2 -c ARG3
- All command arguments –
Option arguments example¶
echo "Tom Likes Jerry" | xargs bash -c './threeOptions.sh -A $0 -B $1 -C $2'- or
echo "Tom Likes Jerry" | xargs bash './threeOptions.sh -A $1 -B $2 -C $3' - or
echo "Tom Likes Jerry" | xargs sh './threeOptions.sh -A $1 -B $2 -C $3'
- or
- We need to keep in mind that if we use
bash -c the_real_command, the first argument is assigned to$0instead of$1 - Since the arguments have been indexed, we can easily change the arguments’ order or decide which argument to pass to which option.
echo "Tom Likes Jerry" | xargs bash -c './threeOptions.sh -A $2 -B $1 -C $0'
Example: Delete log files older than 7 days¶
find . -type f -mtime +7 | rm- This prints an error message sincermexpects arguments and can’t read them from STDINfind . -type f -mtime +7 | xargs rm- solution
Miscellaneous¶
- By default if you don't give
xargsa second command it will useechols * | xargsis same asls * | xargs echo
References¶
Last updated: 2022-06-15