Linux: xargs vs. exec {}
October 29th, 2006 | Linux | Sysadmin | Unix
There is a bit of a debate in some circles about using xargs vs. the -exec {} option that’s built into find itself. To me, however, it’s not much of a debate; -exec isn’t nearly as good as xargs for what I use find for. I tend to use it to perform tasks involving many files. “Move all these files there”, “copy all those directories there”, “Delete these links.”, etc.
This is where-execbreaks down andxargsshows its superiority. When you use-execto do the work you run a separate instance of the called program for each element of input. So iffindcomes up with 10,000 results, you runexec10,000 times. Withxargs, you build up the input into bundles and run them through the command as few times as possible, which is often just once. When dealing with hundreds or thousands of elements this is a big win forxargs.
That’s all nice and stuff, but you probably want to see it in action, right? Let’s run some numbers. Below is a listing of 1,668 .jpg files on my OS X system using both -exec and xargs:
# time find . -name "*.jpg" -exec ls {} \;
real 0m6.618s user 0m1.465s sys 0m4.396sHmm, that’s not bad — seven seconds for over around 1,600 files, right? Let’s try it with
xargs.
# time find . -name "*.jpg" -print0 | xargs -0 ls
real 0m1.120s user 0m0.594s sys 0m0.527sThat's one (1) second vs seven (7) seconds. Seriously;
xargs is the way to go.
Related Posts:
- Using jk vs. jj for ESC Remapping in Vim by Steve Losh
- Ubuntu: aptitude > apt-*
- Your Problem with Vim is That You Don’t Grok Vi
- New PC Setup Steps
- Restricting Access to Your Git Directory
- Adding a Trailing Slash to Directories Using Varnish
- Node.js: Resolving ‘Cannot fiind module’ Issues
- The Power of Git Bisect
- memcache-top | Google Code
- A Git Tutorial | danielmiessler.com
☐ If you enjoyed this, please consider connecting via Twitter, RSS, or my other content.
Pingback: http://takescreenshotonmac.com
Pingback: here
Pingback: Pharr
Pingback: Buildasign Coupon
Pingback: LSAT logic games
Pingback: other
Pingback: Apartments Vrtlici
Pingback: car insurance quote
Pingback: โชคดี
Pingback: krtasiye
Pingback: messebau I standbau I prosoektstaender I prospektstander I veranstaltungstechnik I plakatständer I plexidisplay I klapprahmen I infostander I infostaender I stehtische I veranstaltungstechnik I messestand I roll-ups I werbdedisplay I Sicherheitsspiegel I