05-E.8.2: File Output Manipulation - xargs
- Page ID
- 32560
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
The xargs Command
xargs is a Unix command which can be used to build and execute commands from standard input.
Importance :
Some commands like grep can accept input as parameters, but some commands accepts arguments. This is where xargs came into the picture.
Syntax :
xargs [ OPTIONS ] [command]
xargs Command Options
| Options | Option Meaning |
|---|---|
| 0 | Input items are terminated by null character instead of white spaces. |
| -a file | Read items from file instead of standard input. |
| –delimiter = delim | Input items are terminated by a special character. |
| -E eof-str | Set the end of file string to eof-str. |
| -I replace-str | Replace occurrences of replace-str in the initial arguments with names read from standard input. |
| -L max-lines | Use at-most max-lines non-blank input lines per command line. |
| -p | Prompt the user about whether to run each command line and read a line from terminal. |
| -r | If the standard input does not contain any nonblanks, do not run the command. |
| -x | Exit if the size is exceeded. |
| –help | Print the summary of options to xargs and exit. |
| –version | Print the version no. of xargs and exit. |
Simply put xargs takes each input and applies the command to that input. Below are some examples of xargs:
# the file states.list contains 3 names, we cat the file and pipe the output to xargs mkdir - xargs # takes each line from the file and uses it as an argument to mkdir pbmac@pbmac-server $ cat states.list Alabama Wyoming Washington pbmac@pbmac-server $ cat states.list | xargs mkdir pbmac@pbmac-server $ ls Alabama states.list Washington Wyoming # now we use find, searching for type d (directory) and send all the output to xargs rmdir # the error message is becasue the find command also finds the . directory - the directory where we currently are pbmac@pbmac-server $ find . -type d | xargs rmdir rmdir: failed to remove '.': Invalid argument pbmac@pbmac-server $ ls pbmac@pbmac-server $ # there are 3 files ending in .txt, using the find command we pipe the output of the find to xzargs rm to remove # each .txt file. pbmac@pbmac-server $ ls one.txt states.list three.txt two.txt pbmac@pbmac-server $ find . -name "*.txt" | xargs rm pbmac@pbmac-server $ ls states.list
Adapted from:
"xargs command in Linux with examples" by Mandeep Singh, Geeks for Geeks is licensed under CC BY-SA 4.0

