Skip to main content

Handle Too-Long Command Lines with xargs

Handle Too-Long Command Lines with xargs

xargs is one of those UNIX utilities that seems pretty useless when you first hear about it -- but turns into one of the handiest tools you can have.

xargs reads a group of arguments from its standard input, then runs a UNIX command with that group of arguments. It keeps reading arguments and running the command until it runs out of arguments. The shell's backquotes do the same kind of thing, but they give all the arguments to the command at once. This can give you aToo many arguments error.

Here are a couple of examples:

  • If you want to print most of the files in a large directory, put the output of ls into a file. Edit the file to leave just the filenames you want printed. Give the file toxargs' standard input:

     % ls > allfiles.tmp % vi allfiles.tmp % xargs lpr <> 

    What did that do? With lines like these in allfiles.tmp:

     % cat allfiles.tmp afile application    ... yoyotest zapme 

    xargs ran one or more lpr commands, each with a group of arguments, until it had read every word in the file:

     lpr afile application ...    ... lpr ... yoyotest zapme 

  • The standard output of xargs is the standard output of the commands it runs. So, if you'd created allfiles.tmp above but you wanted to format the files with pr first, you could type:

     % xargs pr <> 

    Then xargs would run all of these pr commands. The shell would pipe their standard outputs to a single lpr command. Actually, the shell is piping the standard output of xargs. As I said above, xargs sends the standard output of commands it runs to its own standard output:

     pr afile application ...    ... pr ... yoyotest zapme 

    In this next example, find gets a list of all files in the directory tree. Next, we use xargs to read those filenames and run grep - l to find which files contain the word "WARNING". Next, we pipe that to a setup with pr and lpr, like the one in the previous example:

     % find . -type f -print | xargs grep -l WARNING | xargs pr | lpr 

    "Huh?" you might say. Just take that step by step. The output of find is a list of filenames, like ./afile ./bfile ... ./adir/zfileand so on. The first xargs gives those filenames to one or more grep - l commands:

     grep -l WARNING ./afile ./bfile ...    ... grep -l WARNING ./adir/zfile ... 

    The standard output of all those greps is a (shortened) list of filenames that match. That's piped to another xargs - it runs prcommands with the filenames that grep found.

    UNIX is weird and wonderful!

  • Sometimes you don't want xargs to run its command with as many arguments as it can fit on the command line. The - n option sets the maximum number of arguments xargs will give to each command. Another handy option, - p, prompts you before running each command.

    Here's a directory full of files with errors (whose names end with .bad) and corrected versions (named .fixed). I use ls to give the list of files to xargs; it reads two filenames at once, then asks whether I want to run diff - c to compare those two files. It keeps prompting me and running diff - c until it runs out of file pairs:

     % ls chap1.bad chap1.fixed chap2.bad chap2.fixed     ... chap9.bad chap9.fixed % ls | xargs -p -n2 diff -c diff -c chap1.bad chap1.fixed ?...y    ...... diff -c chap2.bad chap2.fixed ?...n diff -c chap3.bad chap3.fixed ?...y    ......     ... 

As tomorrow's article explains, xargs can have trouble if an argument has white space inside a word. Luckily, the GNU xargs solves the problem.

Source : http://linuxdevcenter.com/pub/a/linux/lpt/09_21.html


Comments

Popular posts from this blog

Cisco Command "Auto secure"

Cisco Command "Auto secure" In today's article, I'm going to quickly inform you about the Privileged EXEC command named "auto secure". Network administrators (like you) use the "auto secure" command to secure the management and forwarding planes of a router. Another way of saying it is, CCNAs use this command to secure a router by disabling common IP services which can be exploited by attackers to initiate network attacks. When the command is typed on a router, it takes the user (ccna) through a command line-interface (CLI) semi-interactive session (which is also known as the AutoSecure dialogue). Below is the command's syntax: auto secure [management | forwarding] [no-interact | full] [ntp | login | ssh | firewall | tcp-intercept] As you can see, the command can use several "optional" keywords: management - This (optional) keyword is used to only secure the management plane of a router. forwarding - This (optional) keyword is used to

NetSurveyor :: 802.11 (WiFi) Network Discovery / Scanner Tool

NetSurveyor :: 802.11 (WiFi) Network Discovery / Scanner Tool http://www.performancewifi.net/performance-wifi/main/NetSurveyor.htm NetSurveyor is an 802.11 (WiFi) network discovery tool and, as such, its goal in life is to gather information about nearby wireless access points in real time and display it in a useful way. Similar in purpose to NetStumbler , it includes many more features. The data is displayed using a variety of different diagnostic views and charts. Data can be recorded for extended periods and played-back at a later date/time. Also, reports can be generated in Adobe PDF format. Applications for NetSurveyor include the following: During the installation of a wireless network, as an aid in verifying the network is properly configured and antennas are positioned at locations to achieve efficient transmission / reception -- that is, for use in verifying WiFi coverage and maximizing beacon signal strength Trouble-shooting an existing network or wireless environm

The Seven Habits

The Seven Habits Habit 1: Be Proactive - Changes starts form within; highly effective people make the decisions to improve their lives through the things that they can influence rather than simply reacting to external forces. Habit 2: Begin with the end with mind. - Develop a principle– centered personal mission statement into long- term goals based on personal principle. Habit 3: Put first thing first. - Spend time doing what fits into your personal mission, observing the proper balance between production and building production capacity. Identify the key roles that you take on in life, and make time for each other. Habit 4: Think win / win - Seek agreements and relationships that are mutually beneficial. In cases where a win / win deal cannot be achieved, accept the fact that agreeing to make “no deal”, may be the best alternative. In developing the organizational culture, be sure to reward win / win behavior among employees and avoid inadvertently rewarding win / lose behavior. Habi