From charlesreid1

(Created page with "http://en.wikipedia.org/wiki/Xargs")
 
No edit summary
Line 1: Line 1:
http://en.wikipedia.org/wiki/Xargs
xargs is a very handy command line utility that allows you to perform operations on a list.  For example, you can take a list of files and tell xargs to run the "rm" command on the list to remove each file on the list.  Or you can use xargs to unzip a list of files.  Think of it as a quick-and-dirty way to do a bash loop like
 
<pre>
for i in `cat list_of_files`; do
command $i
done
</pre>
 
It has the additional advantage that it performs the command once for each argument in the list.  To understand this as an advantage, see [[wikipedia:xargs]] - the example they give is for the "rm" command, which cannot take extremely long lists of files.  To circumvent this, xargs will run the "rm" command once for each file in the list.
 
=Usage=
 
==Basic Usage==
 
There are all kinds of examples of ways to use xargs that are super handy.  For me, the most useful is probably using it to grep through files, or to remove lists of files.  For example:
 
 
 
==Advanced Usage==
 
==Xargs Jujitsu==
 
===SVN===
 
I use xargs in pipes to do some handy stuff.  For example, if I am in a directory that is part of an SVN repository, I can run the command "svn status" to tell me about files that have been changed, etc.:
 
<pre>
$ svn status .
M      Core/Grid/Patch.h
M      Core/Grid/Level.cc
M      Core/Grid/Level.h
M      Core/Grid/sub.mk
?      Core/Grid/AMR_CoarsenRefine.cc
?      Core/Grid/LevelP.h
?      Core/Grid/AMR_CoarsenRefine.h
M      Core/Math/Matrix3.cc
M      Core/Math/Matrix3.h
M      Core/Parallel/Parallel.cc
M      Core/Parallel/Parallel.h
M      Core/Containers/Handle.h
M      Core/Containers/RunLengthEncoder.h
</pre>
 
Those "?"s mean the files have not been added to the repository yet.  In this case, there's only 3, so it would be straightforward to copy-and-paste the file names into an "svn add" command to add the files to the repository.  But the smart solution is to make the computer work for you!
 
First, use grep to reduce the list only to files that haven't been added to the repository yet:
 
<pre>
$ svn status . | grep "?"
?      Core/Grid/AMR_CoarsenRefine.cc
?      Core/Grid/LevelP.h
?      Core/Grid/AMR_CoarsenRefine.h
</pre>
 
Then use [[Awk]] to strip the leading "?":
 
<pre>
$ svn status . | grep "?" | awk -F" " '{print $2}'
Core/Grid/AMR_CoarsenRefine.cc
Core/Grid/LevelP.h
Core/Grid/AMR_CoarsenRefine.h
</pre>
 
And finally, use xargs to add each file, one-at-a-time, to the repository:
 
<pre>
$ svn status . | grep "?" | awk -F" " '{print $2}' | xargs svn add
A      Core/Grid/AMR_CoarsenRefine.cc
A      Core/Grid/LevelP.h
A      Core/Grid/AMR_CoarsenRefine.h
</pre>
 
Or, if we wanted to delete them (this would be the case if we wanted to "clean out" and revert a directory to the repository version), we could replace "svn add" with "rm":
 
<pre>
$ svn status . | grep "?" | awk -F" " '{print $2}' | xargs rm
</pre>
 
'''REMEMBER''': before you run the "rm" command on anything, leave off the "xargs" portion, and make sure that you're slicing-and-dicing correctly (doing everything up until the awk command will just print the names of the files on which "rm" will operate).
 
[[Category:Programs]]
[[Category:Computers]]

Revision as of 22:47, 5 April 2011

xargs is a very handy command line utility that allows you to perform operations on a list. For example, you can take a list of files and tell xargs to run the "rm" command on the list to remove each file on the list. Or you can use xargs to unzip a list of files. Think of it as a quick-and-dirty way to do a bash loop like

for i in `cat list_of_files`; do
command $i
done

It has the additional advantage that it performs the command once for each argument in the list. To understand this as an advantage, see wikipedia:xargs - the example they give is for the "rm" command, which cannot take extremely long lists of files. To circumvent this, xargs will run the "rm" command once for each file in the list.

Usage

Basic Usage

There are all kinds of examples of ways to use xargs that are super handy. For me, the most useful is probably using it to grep through files, or to remove lists of files. For example:


Advanced Usage

Xargs Jujitsu

SVN

I use xargs in pipes to do some handy stuff. For example, if I am in a directory that is part of an SVN repository, I can run the command "svn status" to tell me about files that have been changed, etc.:

$ svn status .
M      Core/Grid/Patch.h
M      Core/Grid/Level.cc
M      Core/Grid/Level.h
M      Core/Grid/sub.mk
?      Core/Grid/AMR_CoarsenRefine.cc
?      Core/Grid/LevelP.h
?      Core/Grid/AMR_CoarsenRefine.h
M      Core/Math/Matrix3.cc
M      Core/Math/Matrix3.h
M      Core/Parallel/Parallel.cc
M      Core/Parallel/Parallel.h
M      Core/Containers/Handle.h
M      Core/Containers/RunLengthEncoder.h

Those "?"s mean the files have not been added to the repository yet. In this case, there's only 3, so it would be straightforward to copy-and-paste the file names into an "svn add" command to add the files to the repository. But the smart solution is to make the computer work for you!

First, use grep to reduce the list only to files that haven't been added to the repository yet:

$ svn status . | grep "?"
?      Core/Grid/AMR_CoarsenRefine.cc
?      Core/Grid/LevelP.h
?      Core/Grid/AMR_CoarsenRefine.h

Then use Awk to strip the leading "?":

$ svn status . | grep "?" | awk -F" " '{print $2}'
Core/Grid/AMR_CoarsenRefine.cc
Core/Grid/LevelP.h
Core/Grid/AMR_CoarsenRefine.h

And finally, use xargs to add each file, one-at-a-time, to the repository:

$ svn status . | grep "?" | awk -F" " '{print $2}' | xargs svn add
A      Core/Grid/AMR_CoarsenRefine.cc
A      Core/Grid/LevelP.h
A      Core/Grid/AMR_CoarsenRefine.h

Or, if we wanted to delete them (this would be the case if we wanted to "clean out" and revert a directory to the repository version), we could replace "svn add" with "rm":

$ svn status . | grep "?" | awk -F" " '{print $2}' | xargs rm

REMEMBER: before you run the "rm" command on anything, leave off the "xargs" portion, and make sure that you're slicing-and-dicing correctly (doing everything up until the awk command will just print the names of the files on which "rm" will operate).