![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png)
There is something imminently satisfying about using find and xargs to accomplish tasks on files in the folder tree:
find . -name "*.orig" | xargs rm
I use it to delete certain leftover files from editors or version control systems, or to change the permissions on all of a certain kind of file. I also use it to recursively grep in certain instances:
find . -name "*.pl" | xargs grep -i "OpenID"
Here are a couple of articles for those interested in learning more about this awesome combo:
Advanced techniques for using the find command
Unix Xargs Piping Toolkit Utility
find . -name "*.orig" | xargs rm
I use it to delete certain leftover files from editors or version control systems, or to change the permissions on all of a certain kind of file. I also use it to recursively grep in certain instances:
find . -name "*.pl" | xargs grep -i "OpenID"
Here are a couple of articles for those interested in learning more about this awesome combo:
Advanced techniques for using the find command
Unix Xargs Piping Toolkit Utility
no subject
Date: 2009-10-07 03:18 am (UTC)One trick for deleting found (find-ed?) files, I usually use:
find . -name "*.orig" -delete
just so I'm sure I don't accidentally add arguments to rm.
no subject
Date: 2009-10-08 06:36 am (UTC)no subject
Date: 2009-10-07 09:36 am (UTC)Another trick: if your filenames have spaces in them, xargs may not do what you expect. If you have GNU find and xargs, you can get around this with
find ... -print0 | xargs -0 ...
, which will cause find to separate arguments with null bytes (which can't occur in filenames), so xargs can pick them apart unambiguously.Two common-ish invocations of find + xargs + grep for me are:
find . -name 'whatever' | xargs grep -l string
for telling me only which files the string appears in, but not listing all the lines that match, and
find . -name 'whatever' | xargs grep string /dev/null
for find the matches but making sure that each matching line has the name of the file at the beginning.
The idea behind the latter is that grep adds the name of the file only if you asked it to search through more than one file (otherwise it thinks that you already know the name, since you just gave that one name to it), and that xargs doesn't tack all the names on the commandline but batches them (subject to the maximum length of the commandline, I think)... so if the batch size happened to be, say, 50, and you had 101 files coming out of find, xargs would call grep three times: with 50, 50, 1 arguments.
So if the match was in the last file output by find, grep would think that it was called with only one argument (which it was) and wouldn't report the filename before the match.
Adding /dev/null to the end of the commandline makes sure that grep will always have at least two files (in the example, grep would see
grep string /dev/null lastfilename.txt
, and will search both the empty file and the real file.no subject
Date: 2009-10-08 06:36 am (UTC)no subject
Date: 2010-08-03 09:32 pm (UTC)