每天学一点儿shell:xargs 命令

导读:本篇文章讲解 每天学一点儿shell:xargs 命令,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、标准输入和管道

举一个例子:

[root@hadoop-master test-grep]# cat -n ./file.txt | grep leo
     1	leo hello
     2	leo2 hello2
     3	leo3 hello3

上面这个例子使用了管道命令(|),管道命令的作用是将左侧的命令(cat -n ./file.txt )的标准输出作为右边的标准输入,提供给右边的命令(grep leo)作为参数。因此上面的代码等同于如下:

[root@hadoop-master test-grep]# grep -n leo file.txt 
1:leo hello
2:leo2 hello2
3:leo3 hello3

但是大多数命令都不接收标准输入作为参数,只能直接在命令行输入参数,这导致无法用管道命令传递参数。
例如下面的语句是没有任何输出的

[root@hadoop-master test-grep]# echo "leo" | echo

[root@hadoop-master test-grep]# 

二、xargs命令的作用

xargs命令的作用,是将标准输入转为命令行参数。

[root@hadoop-master test-grep]# echo "leo" | xargs  echo
leo

xargs命令格式如下:

xargs [OPTION]... COMMAND INITIAL-ARGS...

整整执行的命令是紧跟在xargs后面的COMMAND,接收xargs传来的参数
例如:

[root@hadoop-master test-grep]# echo "one two three" | xargs mkdir
[root@hadoop-master test-grep]# ll
总用量 8
drwxrwxrwx. 2 root root 22 9月  22 16:53 dir2
-rwxrwxrwx. 1 root root 60 9月  22 16:48 file2.text
-rwxrwxrwx. 1 root root 45 9月  21 23:07 file.txt
drwxr-xr-x. 2 root root  6 9月  26 17:42 one
drwxr-xr-x. 2 root root  6 9月  26 17:42 three
drwxr-xr-x. 2 root root  6 9月  26 17:42 two

上面这个命令是创建one two three 三个目录,如果不加xargs会报如下错误:

[root@hadoop-master test-grep]# echo "one two three" | mkdir
mkdir: 缺少操作数
Try 'mkdir --help' for more information.
选项 解释
-a file 从文件中读入作为sdtin
-e flag ,注意有的时候可能会是-E,flag必须是一个以空格分隔的标志,当xargs分析到含有flag这个标志的时候就停止。
-p 当每次执行一个argument的时候询问一次用户。
-n num 后面加次数,表示命令在执行的时候一次用的argument的个数,默认是用所有的。
-t 表示先打印命令,然后再执行。
-i 或者是-I,这得看linux支持了,将xargs的每项名称,一般是一行一行赋值给 {},可以用 {} 代替。
-r no-run-if-empty 当xargs的输入为空的时候则停止xargs,不用再去执行了。
-s num 命令行的最大字符数,指的是 xargs 后面那个命令的最大命令行字符数。
-L num 从标准输入一次读取 num 行送给 command 命令。
-l 同 -L。
-d delim 分隔符,默认的xargs分隔符是回车,argument的分隔符是空格,这里修改的是xargs的分隔符。
-x exit的意思,主要是配合-s使用。。
-P 修改最大的进程数,默认是1,为0时候为as many as it can ,这个例子我没有想到,应该平时都用不到的吧。

三、xargs命令的实例

1、创建多个文件目录

[root@hadoop-master test-grep]# echo "one two three" | mkdir

递归创建多个文件目录:

[root@hadoop-master dir1]# echo "dir1/20201011 dir2/20201011 dir3/20201011"|xargs mkdir -p

2、多行内容变单行输出

[root@hadoop-master test-grep]# cat file.txt 
leo hello
leo2 hello2
leo3 hello3
hello grep
[root@hadoop-master test-grep]# cat file.txt | xargs 
leo hello leo2 hello2 leo3 hello3 hello grep

3、将内容按照”,”分隔

[root@hadoop-master test-grep]# echo "leo,leo,leo" | xargs -d','
leo leo leo

4、找到所有的txt文件并压缩

[root@hadoop-master test-grep]# find . -type f -name "*.txt" -print | xargs tar -czvf txts.tar.gz
./file.txt
./dir2/file.txt

5、找到所有的txt文件并删除(慎用)

[root@hadoop-master test-grep]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f

删除符合条件的目录下文件,目录结构如下,需要删除时间是20201011下的文件:

[root@hadoop-master dir1]# tree
.
├── dir1
│   └── 20201011
├── dir2
│   ├── 20201011
│   │   └── file2.txt
│   ├── 20201012
│   │   └── file2.txt
│   └── 20201013
│       └── file2.txt
└── dir3
    └── 20201011
        └── file.txt

命令如下:

[root@hadoop-master dir1]# find . -type f -name "*.txt" -print0 | grep -FzZ '20201011' | xargs -0 rm -f
[root@hadoop-master dir1]# tree
.
├── dir1
│   └── 20201011
├── dir2
│   ├── 20201011
│   ├── 20201012
│   │   └── file2.txt
│   └── 20201013
│       └── file2.txt
└── dir3
    └── 20201011

如果没有tree命令,可以使用yum下载一个:

[root@hadoop-master dir1]# yum install tree

6、配合ps和kill批量杀掉进程(实用)

[root@hadoop-master test-grep]# ps -ef|grep -v 'grep'|grep '测试程序'|awk '{print $2}'|xargs kill -9

7、配合cp将文件拷贝到多个目录(实用)

[root@hadoop-master BJ]# echo 20201011 20201012 20201013 | xargs -n 1 cp -v ./test01.txt 

打印结果:
图7
20201011、20201012、20201013 是三个目录,。/test01.txt代表需要复制的文件
-n 1 :表示每一个命令行只有一个参数,并且传给cp命令
cp :表示复制命令
-v:verbose表示将复制过程中的详细信息显示出来

当然可以正则匹配目录和文件名:

[root@hadoop-master dir1]# echo */20201011 | xargs -n 1 cp -v ./*.txt 
"./file1.txt" -> "dir1/20201011/file1.txt"
"./file2.txt" -> "dir1/20201011/file2.txt"
"./file1.txt" -> "dir2/20201011/file1.txt"
"./file2.txt" -> "dir2/20201011/file2.txt"
"./file1.txt" -> "dir3/20201011/file1.txt"
"./file2.txt" -> "dir3/20201011/file2.txt"

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/72648.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!