• Animal
  • Auto
  • Computers
  • Education
  • Finance
  • Health
  • Math
  • Mobiles
  • Programming
  • Science
  • Sports
  • Technology
  • Terms
  • Web

  • Difference Between xargs and exec

    xargs vs exec

    Both xargs and exec are unix / linux commands. Both do similar tasks and often people while working want to take a decision as to which command to use xargs or exec and get confused. They have subtle difference and should use the right one when we need to do a specific task. They are very similar But both have differences between them. Read the following point to point Difference Between xargs and exec. Confusion between the words xargs and exec is very common.

    To show the difference between xargs and exec, I have to first take an example and then explain the differences. I will be using “find” unix command. for those who don’t know find command below is some thing.

    Differences between xargs and exec commands

    1. xargs command is faster than exec.
    2. xargs needs to be used with | (piped) [check example below] where as exec command doesn’t need be piped.
    3. xargs command fails if the filename or filenames has some weird character in it whereas exec works
    4. exec command can be used to execute commands without returning and opening and closing files whereas xargs uses pipes
    5. xargs supports large file counts thus uses memory compared to exec

    What is find unix command

    The find command helps user to find files. The user may want to find the files and apply something on those found files. for ex: apply “grep” or “copy” or “remove” or “move”, etc.,

    Let me take an example:

    – Find *.C files and grep sprintf (pattern) on each file found by the find command.

    for the above I can use exec and write the below command:

    find . -name “*.C” -exec grep sprintf {};

    The above command find & exec command will find all *.C files from the current path (.) and then each file is sent as an argument to grep command using exec command.

    1. -exec executes rm command
    2. {} is replaced by the file name found by the find command

    Lets say if the number of files from the current directory (.) are 100,000 Files. Here the exec command will run grep command will run for 100000 times.

    This is inefficient which is where xargs unix command is better, smarter and efficient than the exec command

    find . -name “*.C” | xargs grep sprintf {};

    The above xargs will apply grep sprintf on 100,000 files in batches instead of applying for each single file that is sent as an argument by the find command. This is where it is faster and efficient.

    These are the differences between xargs and exec command. Appreciate if you could share more differences by leaving the comment.

    Posted under: Technology
    1957 readers are already subscribed to this blog! Why don't you be one of them? Subscribe to this blog via your favorite RSS feed reader or by entering your email address on the form below:

    One Comment

    1. Ole Tange says:

      Please do not use find combined with xargs without -print0. It may lead to dangerous situations (see http://en.wikipedia.org/wiki/Xargs#The_separator_problem).

      Consider using GNU Parallel instead. Watch the intro videos to learn more: http://nd.gd/039

    Leave a Reply

    page counter
    NiharsWorld on Twitter NiharsWorld on Facebook NiharsWorld RSS Feed