Linux下的xargs命令,从标准输入或者文件读取内容,按照空格或者空行将内容分割成多个参数,然后将这些参数传递给指定命令(默认echo)让其执行一次或者多次,传入的空行被忽略。该命令便于将某个命令在标准输入的执行结果作为另一个命令的执行参数,对于不支持管道操作的命令(如ls、file等)来说是一个福音。
常用参数:
-a<FILE>:从FILE指定的文件中读取内容,默认从标准输入读取 -d<delim>:指定分隔符为delim,默认视空格或空行为分隔符 -i:该参数必须配合{}使用,{}代表一个参数,主要用于批处理操作 -n<m>:单次运行指定命令最多引用m个参数,多次运行,直至参数引用完毕 -p:每次执行指定命令之前先询问用户是否确实要执行 -r:如果标准输入为空,则不执行指定命令 -t:在执行指定命令之前将命令行打印到标准错误输出(stderr)
应用实例:
1、演示xargs命令的-a、-n、-t参数
trevor@trevor-PC:~/linux/linux100$ ls xargs-test trevor@trevor-PC:~/linux/linux100$ cat xargs-test This is a test for the command xargs. trevor@trevor-PC:~/linux/linux100$ xargs -a xargs-test mkdir trevor@trevor-PC:~/linux/linux100$ ls -l 总用量 36 drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 a drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 command drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 for drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 is drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 test drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 the drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 This drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:43 xargs. -rw-r--r-- 1 trevor trevor 38 2012-01-18 14:12 xargs-test trevor@trevor-PC:~/linux/linux100$ xargs -a xargs-test -t rm -r rm -r This is a test for the command xargs. trevor@trevor-PC:~/linux/linux100$ ls -l 总用量 4 -rw-r--r-- 1 trevor trevor 38 2012-01-18 14:12 xargs-test trevor@trevor-PC:~/linux/linux100$ cat xargs-test | xargs -t -n 2 mkdir mkdir This is mkdir a test mkdir for the mkdir command xargs. trevor@trevor-PC:~/linux/linux100$ ls -l 总用量 36 drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 a drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 command drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 for drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 is drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 test drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 the drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 This drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:45 xargs. -rw-r--r-- 1 trevor trevor 38 2012-01-18 14:12 xargs-test trevor@trevor-PC:~/linux/linux100$ cat xargs-test | xargs -t -n 4 rm -r rm -r This is a test rm -r for the command xargs. trevor@trevor-PC:~/linux/linux100$ ls xargs-test trevor@trevor-PC:~/linux/linux100$
2、演示xargs命令的-i、-p参数
trevor@trevor-PC:~/linux/linux100$ ls a command for is test the This xargs. xargs-test trevor@trevor-PC:~/linux/linux100$ ls | xargs -i -p mv {} {}_bak mv a a_bak ?...y mv command command_bak ?...y mv for for_bak ?...y mv is is_bak ?...y mv test test_bak ?...y mv the the_bak ?...y mv This This_bak ?...y mv xargs. xargs._bak ?...y mv xargs-test xargs-test_bak ?...n trevor@trevor-PC:~/linux/linux100$ ls -l 总用量 36 drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 a_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 command_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 for_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 is_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 test_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 the_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 This_bak drwxr-xr-x 2 trevor trevor 4096 2012-01-18 16:59 xargs._bak -rw-r--r-- 1 trevor trevor 38 2012-01-18 14:12 xargs-test trevor@trevor-PC:~/linux/linux100$
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。