foxfirefey: A guy looking ridiculous by doing a fashionable posing with a mouse, slinging the cord over his shoulders. (geek)
[personal profile] foxfirefey posting in [community profile] command_liners
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

Date: 2009-10-07 03:18 am (UTC)
afuna: Cat under a blanket. Text: "Cats are just little people with Fur and Fangs" (Default)
From: [personal profile] afuna
I love the find/xargs combo *__*

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.

Date: 2009-10-07 09:36 am (UTC)
pne: A picture of a plush toy, halfway between a duck and a platypus, with a green body and a yellow bill and feet. (Default)
From: [personal profile] pne
find + xargs are BFF.

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.

Date: 2010-08-03 09:32 pm (UTC)
sophie: A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀ (Default)
From: [personal profile] sophie
This is a late reply, but it turns out that on GNU grep, at least, you can use the -H switch to grep as an alternative to using the /dev/null trick.

Profile

command_liners: A command line prompt with a blinking cursor after it, green against black. (Default)
Command Liners

January 2022

S M T W T F S
      1
2 345678
9101112131415
16171819202122
23242526272829
3031     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Aug. 6th, 2025 02:57 am
Powered by Dreamwidth Studios