Xargs: Difference between revisions
From charlesreid1
No edit summary |
|||
| Line 13: | Line 13: | ||
==Basic Usage== | ==Basic Usage== | ||
xargs is almost always called to receive information from a pipe. | |||
One of the most convenient uses of xargs is to grep through files, or to remove lists of files. | |||
For example, to search through C++ code files for the word "turbulence," you could use xargs as follows: | |||
<pre> | |||
$ find . -name "*.cc" | xargs grep "turbulence" | |||
./Arches.cc: //db->require("turbulence_model", turbModel); | |||
./BoundaryCondition.cc: // --- new turbulence inlet flow generator --- | |||
./BoundaryCondition.cc: turb_db->get("turbulence_intensity",intensity); | |||
./CompDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./CompDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./CompLocalDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./CompLocalDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./EnthalpySolver.cc: // if it is set in turbulence model | |||
./IncDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./IncDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model | |||
./OdtClosure.cc:// Schedule recomputation of the turbulence sub model | |||
./ReactiveScalarSolver.cc: // if it is set in turbulence model | |||
./ScalarSolver.cc: // if it is set in turbulence model | |||
./ScalarSolver.cc: // we only need to set mixture fraction Pr number in turbulence model | |||
./ScaleSimilarityModel.cc:// Schedule recomputation of the turbulence sub model | |||
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model | |||
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model | |||
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model | |||
</pre> | |||
==Advanced Usage== | ==Advanced Usage== | ||
Revision as of 23:10, 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
xargs is almost always called to receive information from a pipe.
One of the most convenient uses of xargs is to grep through files, or to remove lists of files.
For example, to search through C++ code files for the word "turbulence," you could use xargs as follows:
$ find . -name "*.cc" | xargs grep "turbulence"
./Arches.cc: //db->require("turbulence_model", turbModel);
./BoundaryCondition.cc: // --- new turbulence inlet flow generator ---
./BoundaryCondition.cc: turb_db->get("turbulence_intensity",intensity);
./CompDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./CompDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./CompLocalDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./CompLocalDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./EnthalpySolver.cc: // if it is set in turbulence model
./IncDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./IncDynamicProcedure.cc:// Schedule recomputation of the turbulence sub model
./OdtClosure.cc:// Schedule recomputation of the turbulence sub model
./ReactiveScalarSolver.cc: // if it is set in turbulence model
./ScalarSolver.cc: // if it is set in turbulence model
./ScalarSolver.cc: // we only need to set mixture fraction Pr number in turbulence model
./ScaleSimilarityModel.cc:// Schedule recomputation of the turbulence sub model
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model
./SmagorinskyModel.cc:// Schedule recomputation of the turbulence sub model
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).