Faster Finds with the xargs Command

Turbocharger

© Lead Image © rouslan, 123RF.com

© Lead Image © rouslan, 123RF.com

Article from Issue 303/2026
Author(s):

If you're processing a group of files in a single command pipe, the xargs tool just might save you some precious execution time.

Everyone is familiar with the classic problem of finding a string of text in a mass of files. You could approach this problem in a naive way by simply using grep and a parameter set (Listing 1, first line). But be aware that this approach often ends with the error shown in the second line of Listing 1. Houston, we have a problem: There are too many files in the target folder for grep to pass them as parameters in Bash.

Another option is find. If you take a quick look at the man page for the find tool, you will soon encounter the -exec parameter. This approach involves find passing the current search results to the command after -exec, represented by a pair of curly brackets (Listing 1, last line). A final semicolon, which you need to escape with a backslash, guarantees that find will detect the end of the -exec argument.

This approach does solve the problem, but at a price. What happens here is slow and, above all, wasteful. For each search result, find has to start its own grep process, irrespective of whether the search term that you want grep to find even exists in the file.

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • The Watchmen

    Two monitoring tools, watch and fswatch, let you gather system information from the command line.

  • Tiny Core Linux

    Tiny Core Linux does not boast a big repository. Sooner or later, you'll need to create your own extensions to get the most out of Tiny Core. This article shows you how.

  • ICgrep

    One of the most common tasks when working on computers involves browsing texts for search patterns. Here, ICgrep offers a modern, parallel, and Unicode-enabled alternative to the classic grep.

  • Backup Integrity

    A backup policy can protect your data from malware attacks and system crashes, but first you need to ensure that you are backing up uncorrupted data.

  • gdu, godu, duf

    Three modern tools, gdu, godu, and duf, make the task of checking the utilization level of hard disks easier thanks to fast execution speed and a good graphical implementation.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News