Xargs
From FreeBSDwiki
(Difference between revisions)
m (Reverted edits by 213.165.86.131 (Talk); changed back to last version by 12.47.70.7) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
+ | '''xargs''' is a command useful for acting upon the output of an earlier command; for instance, you could pipe the output of the [[find]] command to '''xargs rm''' to recursively delete all .tmp files below the current directory: | ||
+ | |||
+ | server# '''find . -name "*.tmp" | xargs rm''' | ||
+ | |||
from the manpage: | from the manpage: | ||
The xargs utility reads space, tab, newline and end-of-file delimited | The xargs utility reads space, tab, newline and end-of-file delimited | ||
Line 8: | Line 12: | ||
standard input of xargs. The utility is repeatedly executed until stan- | standard input of xargs. The utility is repeatedly executed until stan- | ||
dard input is exhausted. | dard input is exhausted. | ||
+ | |||
+ | When you need a more complex usage, the -J argument specifies a "replacement string" to allow you to insert the output of the last command into the new one in an arbitary location. For example, say you wanted to move all files that contained a certain string into a different folder: | ||
+ | |||
+ | server# '''grep -rv "I should be in newfolder!" oldfolder | xargs -J% mv oldfolder/% newfolder/''' | ||
+ | |||
+ | Hint: If your file names may contain spaces then be sure to use the -0 (zero) option: | ||
+ | |||
+ | server# '''find . -name "*.tmp" -print0 | xargs -0 rm''' | ||
+ | |||
+ | [[Category:System Commands]] |
Latest revision as of 12:15, 19 December 2008
xargs is a command useful for acting upon the output of an earlier command; for instance, you could pipe the output of the find command to xargs rm to recursively delete all .tmp files below the current directory:
server# find . -name "*.tmp" | xargs rm
from the manpage:
The xargs utility reads space, tab, newline and end-of-file delimited strings from the standard input and executes utility with the strings as arguments.
Any arguments specified on the command line are given to utility upon each invocation, followed by some number of the arguments read from the standard input of xargs. The utility is repeatedly executed until stan- dard input is exhausted.
When you need a more complex usage, the -J argument specifies a "replacement string" to allow you to insert the output of the last command into the new one in an arbitary location. For example, say you wanted to move all files that contained a certain string into a different folder:
server# grep -rv "I should be in newfolder!" oldfolder | xargs -J% mv oldfolder/% newfolder/
Hint: If your file names may contain spaces then be sure to use the -0 (zero) option:
server# find . -name "*.tmp" -print0 | xargs -0 rm