The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
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
Help with xargs JimJim UNIX for Dummies Questions & Answers 4 02-08-2005 07:08 PM
xargs jpprial UNIX for Dummies Questions & Answers 4 09-17-2001 08:29 PM
difference between source, exec and ./script 98_1LE UNIX for Dummies Questions & Answers 1 04-15-2001 09:24 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-06-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
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.
  #2 (permalink)  
Old 06-06-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,211
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
  #3 (permalink)  
Old 06-06-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,119
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.
  #4 (permalink)  
Old 06-06-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,211
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.
  #5 (permalink)  
Old 06-07-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
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
  #6 (permalink)  
Old 06-07-2005
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,211
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.
  #7 (permalink)  
Old 06-07-2005
vibhor_agarwali vibhor_agarwali is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 259
Thanks,

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0