linux post command xargs

Reprinted from:  http://www.360doc.com/content/12/0405/13/532901_201081911.shtml

Usage of xargs:

xargs

xargs - build and execute command lines from standard input

When using the -exec option of the find command to process the matched files, the find command passes all the matched files to exec for execution. But some systems have a limit on the length of commands that can be passed to exec, so that after a few minutes of the find command running, an overflow error occurs. The error message is usually "parameter column too long" or "parameter column overflow". This is where the xargs command comes in, especially with the find command.

The find command passes the matched files to the xargs command, and the xargs command gets only some of the files at a time instead of all, unlike the -exec option. This way it can process the first batch of files it gets, then the next batch, and so on.

In some systems, using the -exec option will initiate a corresponding process for each matched file, instead of executing all the matched files as parameters at one time; in this way, there will be too many processes in some cases, and the system will The problem of performance degradation, and thus inefficiency;

With the xargs command there is only one process. In addition, when using the xargs command, whether to obtain all parameters at one time or obtain parameters in batches, and the number of parameters obtained each time will be determined according to the options of the command and the corresponding tunable parameters in the system kernel.

Let's see how the xargs command is used with the find command and give some examples.

The following example finds every common file in the system, and then uses the xargs command to test which kind of file they belong to

#find . -type f -print | xargs file
./.kde/Autostart/Autorun.desktop: UTF-8 Unicode English text
./.kde/Autostart/.directory:      ISO-8859 text\
......

Find the memory information dump file (core dump) in the whole system, and save the result to the /tmp/core.log file:

$ find / -name "core" -print | xargs echo "" >/tmp/core.log

The above execution is too slow, I changed it to look in the current directory

#find . -name "file*" -print | xargs echo "" > /temp/core.log
# cat /temp/core.log
./file6

Find all files in the current directory with read, write, and execute permissions for all users, and revoke the corresponding write permissions:

# ls -l
drwxrwxrwx    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxrwx    2 sam      adm             0 10月 31 01:01 httpd.conf

# find . -perm -7 -print | xargs chmod o-w
# ls -l
drwxrwxr-x    2 sam      adm          4096 10月 30 20:14 file6
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 http3.conf
-rwxrwxr-x    2 sam      adm             0 10月 31 01:01 httpd.conf

Use grep to search all ordinary files for the word hostname:

# find . -type f -print | xargs grep "hostname"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

用grep命令在当前目录下的所有普通文件中搜索hostnames这个词:

# find . -name \* -type f -print | xargs grep "hostnames"
./httpd1.conf:#     different IP addresses or hostnames and have them handled by the
./httpd1.conf:# VirtualHost: If you want to maintain multiple domains/hostnames
on your

注意,在上面的例子中, \用来取消find命令中的*在shell中的特殊含义。

find命令配合使用exec和xargs可以使用户对所匹配到的文件执行几乎所有的命令。

 

------------------------------------------------------------------------------------------------------------------------------------

要用管道必须搞清楚标准输入和标准输出的概念。

如果对编程不怎么了解,那么就简单理解:以简单方式执行一个命令(没有管道、改向),标准输入一般就是你从键盘输入的内容,输入输出就是显示在命令运行的 终端上的内容(不含另外弹出的窗口中的内容),大部分情况标准输入等同于键盘输入,但有些程序会绕过标准输入接口去读键盘输入;端口输出除了标准输出的内 容外,还常常标准错误的内容,一般可能会把出错信息和提示用户输入数据的信息从标准错误输出,但也不全是,可以在使用的过程观察体会。

而管道的作用就是把管道前命令标准输出内容作为管道后命令的标准输入。也就是后一条命令执行时本来去读键盘的,改为直接去读前一条命令的输出。

如果你直接执行 rm -f,会报错,而不是等待你从键盘输入要删除的文件名,也就是说 rm 不会读取标准输入,所以不能用管道把要删除的文件名送给 rm。

xargs 命令的作用就是读取标准输入,然后把它读到的内容作为命令行参数去执行指定的命令。所以在 rm 前加 xargs 就可以了,这时 find 输出送给了 xargs,由 xargs 去调用 rm,xargs 调用 rm 时会帮它“组装”命令行参数。

加 -exec 的是另一种情况,不用管道,跟标准输入和标准输出无关,它完全是 find 这条命令的用法,人家就是这么设计的。

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326644943&siteId=291194637