Difference between xargs and exec | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Difference between xargs and exec

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-06-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
 
Join Date: Jan 2005
Last Activity: 11 April 2014, 2:37 AM EDT
Posts: 307
Thanks: 1
Thanked 1 Time in 1 Post
Difference between xargs and exec

Hi,

I have tried both the options in small dummy scripts, but somehow i can't differentiate between the two.

find . -name H* -exec ls -l {} \;
find . -name H* | xargs ls -l

Both work the ditto way.

Any help is appreciated.
Sponsored Links
    #2  
Old 06-06-2005
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
find . -name H* -exec ls -l {} \; executes the command ls -l on each individual file.

find . -name H* | xargs ls -l constructs an argument list from the output of the find commend and passes it to ls.

consider if the ouput of the find command produced:
H1
H2
H3

the first command would execute
ls -l H1
ls -l H2
ls -l H3

but the second would execute
ls -l H1 H2 H3
Sponsored Links
    #3  
Old 06-06-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 31 August 2014, 1:29 AM EDT
Location: Ashburn, Virginia
Posts: 9,923
Thanks: 60
Thanked 433 Times in 258 Posts
Quote:
Originally Posted by vibhor_agarwali

find . -name H* -exec ls -l {} \;
find . -name H* | xargs ls -l
As reborg mentioned, the second is faster because xargs will collect file names and execute a command with as long as a length as possible. Often this will be just a single command. This was discussed in grep command-HELP?!.

However the xargs solution will fail if the shell has trouble parsing the file names. Try:
touch "stupid name"
and then retry the two commands.

There is a third solution that combines the best of both worlds. It is in Posix but not every version of the find command supports it. It's like the first syntax except that instead of \; you just use + to terminate the command.
The Following User Says Thank You to Perderabo For This Useful Post:
reid (09-14-2011)
    #4  
Old 06-06-2005
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
As Perderabo siad the + is a useful option if available, and is in POSIX but not implemented by all versions of find, most noticeably ( for a lot of people ) it is not found in the GNU version of find

from the GNU find man page:

Code:
-exec command +
              This variant of -exec is not yet supported, but is required by POSIX.

Sponsored Links
    #5  
Old 06-07-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
 
Join Date: Jan 2005
Last Activity: 11 April 2014, 2:37 AM EDT
Posts: 307
Thanks: 1
Thanked 1 Time in 1 Post
Wow,

Just some small confusions.

Your + option works for me. How can i found out whether my find is Gnu or not.
I have tried "what find" - no use.

"stupid name" fails in xargs, but its executing in exec
Even if i do a "ls stupid name", it gives me error.
I have to quote it, does exec automatically quotes it parameters.

Thanks
Sponsored Links
    #6  
Old 06-07-2005
reborg's Avatar
reborg reborg is offline Forum Advisor  
Administrator Emeritus
 
Join Date: Mar 2005
Last Activity: 29 March 2012, 7:00 PM EDT
Location: Ireland
Posts: 4,464
Thanks: 0
Thanked 11 Times in 11 Posts
Quote:
Originally Posted by vibhor_agarwali
I have to quote it, does exec automatically quotes it parameters.
Thanks
Each filename is passes to exec as a single value, with special characters escaped, so it is as if the filename has been enclosed in single quotes.
Sponsored Links
    #7  
Old 06-07-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
 
Join Date: Jan 2005
Last Activity: 11 April 2014, 2:37 AM EDT
Posts: 307
Thanks: 1
Thanked 1 Time in 1 Post
Thanks,

Any idea about finding out whether my find in Gnu one or not.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
difference bewteen pipe, xargs, and exec guessingo Programming 1 10-12-2011 12:28 PM
Difference between using xargs and backticks msarro Shell Programming and Scripting 1 12-14-2010 08:57 PM
Difference in Using xargs Karthikeyan K AIX 1 10-10-2008 08:14 PM
String substitution on find results inside exec/xargs myndcraft Shell Programming and Scripting 2 05-17-2008 02:36 PM
MV files with xargs or -exec malaymaru Shell Programming and Scripting 4 04-28-2008 10:40 AM



All times are GMT -4. The time now is 04:17 AM.