Difference between revisions of "Xargs"
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{WikiEntry|key=Xargs|qCode=306592}} is a unix command for assigning various inputs to different other unix commands. | |||
=The Natural Function Connector= | =The Natural Function Connector= | ||
This command/program is particularly useful, and relevant to programming in general, because it is a way to redirect inputs from data pipes. In other words, it is a bridge between input sources and any chosen programs that can be run at the Unix command line. While using this instruction, I realized that <code>xargs</code> is a way to help users experience interactive [[functional programming]]. Similar impression can be attained by watching people writing [[CSS]] ([[2D]]) or using [[Geometry Node]] ([[3D]]) in [[Blender]]. See more observations in the page on [[automation and data scraping]]. | This command/program is particularly useful, and relevant to programming in general, because it is a way to redirect inputs from data pipes. In other words, it is a bridge between input sources and any chosen programs that can be run at the Unix command line. While using this instruction, I realized that <code>xargs</code> is a way to help users experience interactive [[functional programming]]. Similar impression can be attained by watching people writing [[CSS]] ([[2D]]) or using [[Geometry Node]] ([[3D]]) in [[Blender]]. See more observations in the page on [[automation and data scraping]]. | ||
Line 9: | Line 9: | ||
Note that one must supply the <code>-i</code> option, so that <code>{}</code> can become the placeholder for content piped from the previous process. | Note that one must supply the <code>-i</code> option, so that <code>{}</code> can become the placeholder for content piped from the previous process. | ||
=Accepting special separators== | ==A practical use in MediaWiki data loading== | ||
To extract the file names as filtering strings for a text file, one can refer to the following example: | |||
ls MODULE/ | xargs -n 1 -i grep {} moduleNameDictionary.txt > selectedModule.txt | |||
In this case, <code>selecteModule.txt</code> could be processed using [[grep]] UNIX command. | |||
==Accepting special separators== | |||
In certain cases, when the input data embeds separators, one needs to turn on the <code>-0</code> option. For example: | In certain cases, when the input data embeds separators, one needs to turn on the <code>-0</code> option. For example: | ||
ls | xargs -0 -n 1 cp {} /targetDirectory | ls | xargs -0 -n 1 cp {} /targetDirectory | ||
Line 20: | Line 25: | ||
[[Category:Unix Command]] | [[Category:Unix Command]] | ||
[[Category:Xargs]] | [[Category:Xargs]] | ||
[[Category:Web Scraping]] |
Latest revision as of 15:45, 1 February 2022
Xargs(Q306592) is a unix command for assigning various inputs to different other unix commands.
The Natural Function Connector
This command/program is particularly useful, and relevant to programming in general, because it is a way to redirect inputs from data pipes. In other words, it is a bridge between input sources and any chosen programs that can be run at the Unix command line. While using this instruction, I realized that xargs
is a way to help users experience interactive functional programming. Similar impression can be attained by watching people writing CSS (2D) or using Geometry Node (3D) in Blender. See more observations in the page on automation and data scraping.
Executing one line at a time
To execute commands for each of the line feed, use the following option:-n 1
. For example:
ls | xargs -n 1 -i echo {}
Note that one must supply the -i
option, so that {}
can become the placeholder for content piped from the previous process.
A practical use in MediaWiki data loading
To extract the file names as filtering strings for a text file, one can refer to the following example:
ls MODULE/ | xargs -n 1 -i grep {} moduleNameDictionary.txt > selectedModule.txt
In this case, selecteModule.txt
could be processed using grep UNIX command.
Accepting special separators
In certain cases, when the input data embeds separators, one needs to turn on the -0
option. For example:
ls | xargs -0 -n 1 cp {} /targetDirectory
Related Pages
Video/HOW TO USE Xargs!!! Linux Core Utils, Video/Intro to xargs, Video/Xargs Should Be In Your Command Line Toolbag, Video/all about xargs ! (beginner - intermediate) anthony explains, Xargs