Using `-exec cmd {} +` causes find to build the command using all matching filenames before execution, rather than once per file.
Obviously the example given is necessarily simple, but this command not only saves time on the command line (saves you using "cd -" or, worse, having to type a fully qualified path if your command cd's more than once), but is vital in scripts, where I've found the behaviour of "cd -" to be a little broken at times. Show Sample Output
While it seems (to me at least) a little counter-intuitive to filter on name first, this requires less work for find, as it allows it to immediately discount any files that do not match the name directly from the directory listing on disk. Querying against file attributes requires reading the file attributes, which is performed for all files matching any name based predicates. Show Sample Output
This will cause diff to ignore any files whose path matches "*CVS*", ie any CVS control files.
The difference between this and the other alternatives here using only grep is that find will, by default, not follow a symlink. In some cases, this is definitely desirable.
Using find also allows you to exclude certain files, eg
find directory/ ! -name "*.tmp" -exec grep -ni phrase {} +
would allow you to exclude any files .tmp files.
Also note that there's no need for calling grep recursively, as find passes each found file to grep.
If your XML is appended to a line with a time stamp or other leading text irrelevant to the XML, then you can append a s/foo/bar/ command, like this:
sed -n /<Tag>/,/<\/Tag>/p; s/.*\(<Tag.*\)/\1/' logfile.log
You could start this one with
for f in *; do
BUT using the find with "-type f" ensures you only get files not any dirs you might have
It'll also create backups of the files it's overwriting
Of course, this assumes that you don't have any files with duplicated filenames in your target structure
Makes any files in the current directory (and any sub-directories) group-readable. Using the "! -perm /g=r" limits the number of files to only those that do not already have this property Using "+" on the end of the -exec body tells find to build the entire command by appending all matching files before execution, so invokes chmod once only, not once per file.
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: