Find absolutely is a beautiful tool. It does one and only one thing: apply predicates to the file system.
> du -a . | grep filename
Except that's going to match on the dirname content as well, whereas `find -type f -name filename` will only match the actual file name.
And you're going to get complete junk in the first section of each line which you'll have to cleanup afterwards.
> (the awk is necessary to remove file sizes, you could combine du and awk in a shell function if desired)
Right, whereas if you use `find` it gives exactly what you want (or need)
> Why do you need both "-exec" and "-delete"?
You don't need both, but -delete provides additional clarity and forces depth-first traversal.
> Except that as I've shown above, it's already quite easy to execute a program for every file that matches some criteria
Except your examples are all broken since you're not using `basename`. Turns out it's not that easy, and it soon becomes complete shell-soup.
> and you don't have to learn the dozens of options used in find
They're not actually options, they're predicates.
Minimalism at the expense of helping the user get the job done isn't the right tradeoff.
du -a | cut -f2
is a less sledgehammery approach than requiring awk. I don't recognise the OP's shell syntax (unless it's intended more as pseudo-code), but I think at least in bash, you'd have problems with embedded whitespace in filenames.Find and Xargs have the option nul-separated output records specifically for this reason.
Also in Plan 9, we typically don't have spaces in file names, but I think when we do they come out quoted... as far as I know there's no xargs.
I simply do find <path> to give me a list of all the paths under the current directory and then pipe that into grep.
That's because they're not options, they're predicates. A sequence of predicates to be precise (they execute strictly in-order and are short-circuited: the first predicate failure will stop the whole evaluation unless you're using `-or`). They're closer to `test`'s operator than to `grep`'s switches.
find | while read i; do grep 'foo' $i; doneAnd what are you going to do when you want to find all files modified more then 32 hours ago?
-delete is necessary because otherwise there is a race condition which can allow a regular user to make root delete any file.
See: http://news.ycombinator.com/item?id=2699050
You should not use -exec, you should use -execdir, -exec is not safe.
To just find a file:
To find every file containing the string "foo" (the awk is necessary to remove file sizes, you could combine du and awk in a shell function if desired): Really, the for loop above takes care of a few of find's options. Why do you need both "-exec" and "-delete"? Exec is far more versatile, you can just exec "rm" on the files. Except that as I've shown above, it's already quite easy to execute a program for every file that matches some criteria, and you don't have to learn the dozens of options used in find--you just apply the shell programming knowledge you should (hopefully) already have.