There are some commands that turn out to be more useful than first meets the eye. In my opinion, xargs is one of those commands. It takes the standard input and uses it to build a command line. It's nothing fancy, but it's very handy in some situations.
As soon as you have a list of files, you can easily do something to them. A favorite, common enough to have a shell script of its own on my machine, is clean-titles.sh. It simply locates all backup files using the pattern *~, and then passes them on to rm. The result is a nice and clean current working directory and sub-tree.
#!/bin/sh find -iname '*~' | xargs rm
Do not forget your single quotes around the pattern, otherwise bash might expand it for you.
Another place where xargs comes in handy is when you want to find files based on contents and perform some sort of action on them. For instance, let's locate all those pesky TODO comments and open up those files in kate.
grep TODO -r . | sed 's/:.*//' | sort -u | xargs kate -u
The -u argument to kate ensures that xargs reuses an existing session instead of opening a new window. This is just the way that I prefer to have it, and I even have an alias setup for kate, so that I always used kate -u. However, aliases are not used by xargs, so I have to add the flag explicitly.
Something completely different, but somewhat similar, is the xclip command. In a perfect world, I just might want to give all the TODOs to a colleague. Just replacing xargs with xclip puts all the filenames in the clipboard.
grep TODO -r . | sed 's/:.*//' | sort -u | xclip
Now I only need to add the header before I paste it all into a mail. "Hi, I expect you to complete these by tomorrow!"
Johan Thelin is a consultant working with Qt, embedded and free
software. On-line, he is known as e8johan.
Webinar: 8 Signs You’re Beyond Cron
On Demand NOW
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.View Now!
|Chrome-Colored Parakeets||May 05, 2015|
|Mumblehard--Let's End Its Five-Year Reign||May 04, 2015|
|An Easy Way to Pay for Journalism, Music and Everything Else We Like||May 04, 2015|
|When Official Debian Support Ends, Who Will Save You?||May 01, 2015|
|May 2015 Issue of Linux Journal: Cool Projects||May 01, 2015|
|May 2015 Video Preview||May 01, 2015|
- Chrome-Colored Parakeets
- Mumblehard--Let's End Its Five-Year Reign
- An Easy Way to Pay for Journalism, Music and Everything Else We Like
- When Official Debian Support Ends, Who Will Save You?
- Ubuntu Ditches Upstart
- "No Reboot" Kernel Patching - And Why You Should Care
- Video On Demand: 8 Signs You're Beyond Cron
- DevOps: Better Than the Sum of Its Parts
- Picking Out the Nouns
- May 2015 Issue of Linux Journal: Cool Projects