Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Xargs
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Shell trick: any number=== Another way to achieve a similar effect is to use a shell as the launched command, and deal with the complexity in that shell, for example: <syntaxhighlight lang="shell-session"> $ mkdir ~/backups $ find /path -type f -name '*~' -print0 | xargs -0 sh -c 'for filename; do cp -a "$filename" ~/backups; done' sh </syntaxhighlight> The word {{code|sh}} at the end of the line is for the [[POSIX shell]] {{code|sh -c}} to fill in for {{code|$0}}, the "executable name" part of the positional parameters (argv). If it weren't present, the name of the first matched file would be instead assigned to <code>$0</code> and the file wouldn't be copied to <code>~/backups</code>. One can also use any other word to fill in that blank, {{code|my-xargs-script}} for example. Since {{code|cp}} accepts multiple files at once, one can also simply do the following: <syntaxhighlight lang="shell-session"> $ find /path -type f -name '*~' -print0 | xargs -0 sh -c 'if [ $# -gt 0 ]; then cp -a "$@" ~/backup; fi' sh </syntaxhighlight> This script runs {{code|cp}} with all the files given to it when there are any arguments passed. Doing so is more efficient since only one invocation of {{code|cp}} is done for each invocation of {{code|sh}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)