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.
Fast/Flexible Linux OS Recovery
On Demand Now
In this live one-hour webinar, learn how to enhance your existing backup strategies for complete disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible full-system recovery solution for UNIX and Linux systems.
Join Linux Journal's Shawn Powers and David Huffman, President/CEO, Storix, Inc.
Free to Linux Journal readers.Register Now!
- Back to Backups
- Download "Linux Management with Red Hat Satellite: Measuring Business Impact and ROI"
- Google's Abacus Project: It's All about Trust
- Secure Desktops with Qubes: Introduction
- Seeing Red and Getting Sleep
- Fancy Tricks for Changing Numeric Base
- Secure Desktops with Qubes: Installation
- Working with Command Arguments
- Linux Mint 18
- A New Version of Rust Hits the Streets
Until recently, IBM’s Power Platform was looked upon as being the system that hosted IBM’s flavor of UNIX and proprietary operating system called IBM i. These servers often are found in medium-size businesses running ERP, CRM and financials for on-premise customers. By enabling the Power platform to run the Linux OS, IBM now has positioned Power to be the platform of choice for those already running Linux that are facing scalability issues, especially customers looking at analytics, big data or cloud computing.
￼Running Linux on IBM’s Power hardware offers some obvious benefits, including improved processing speed and memory bandwidth, inherent security, and simpler deployment and management. But if you look beyond the impressive architecture, you’ll also find an open ecosystem that has given rise to a strong, innovative community, as well as an inventory of system and network management applications that really help leverage the benefits offered by running Linux on Power.Get the Guide