Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629921
  • 博文数量: 140
  • 博客积分: 2635
  • 博客等级: 少校
  • 技术积分: 1353
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-04 15:46
文章分类
文章存档

2015年(2)

2014年(12)

2013年(10)

2012年(10)

2011年(85)

2010年(21)

分类: Python/Ruby

2011-03-09 09:27:27

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

只要你有一个文件列表,你能很容为它们做一些事情。一个最喜欢的,在我们机器中有一个很相同的脚本,是clean-titleshs

它简单的查找所有格式‘*~'

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

阅读(1033) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~