每天学一点儿shell:xargs 命令
文章目錄
- 一、標(biāo)準(zhǔn)輸入和管道
- 二、xargs命令的作用
- 三、xargs命令的實(shí)例
- 1、創(chuàng)建多個(gè)文件目錄
- 2、多行內(nèi)容變單行輸出
- 3、將內(nèi)容按照","分隔
- 4、找到所有的txt文件并壓縮
- 5、找到所有的txt文件并刪除(慎用)
- 6、配合ps和kill批量殺掉進(jìn)程(實(shí)用)
- 7、配合cp將文件拷貝到多個(gè)目錄(實(shí)用)
一、標(biāo)準(zhǔn)輸入和管道
舉一個(gè)例子:
[root@hadoop-master test-grep]# cat -n ./file.txt | grep leo1 leo hello2 leo2 hello23 leo3 hello3上面這個(gè)例子使用了管道命令(|),管道命令的作用是將左側(cè)的命令(cat -n ./file.txt )的標(biāo)準(zhǔn)輸出作為右邊的標(biāo)準(zhǔn)輸入,提供給右邊的命令(grep leo)作為參數(shù)。因此上面的代碼等同于如下:
[root@hadoop-master test-grep]# grep -n leo file.txt 1:leo hello 2:leo2 hello2 3:leo3 hello3但是大多數(shù)命令都不接收標(biāo)準(zhǔn)輸入作為參數(shù),只能直接在命令行輸入?yún)?shù),這導(dǎo)致無法用管道命令傳遞參數(shù)。
例如下面的語句是沒有任何輸出的
二、xargs命令的作用
xargs命令的作用,是將標(biāo)準(zhǔn)輸入轉(zhuǎn)為命令行參數(shù)。
[root@hadoop-master test-grep]# echo "leo" | xargs echo leoxargs命令格式如下:
xargs [OPTION]... COMMAND INITIAL-ARGS...整整執(zhí)行的命令是緊跟在xargs后面的COMMAND,接收xargs傳來的參數(shù)
例如:
上面這個(gè)命令是創(chuàng)建one two three 三個(gè)目錄,如果不加xargs會(huì)報(bào)如下錯(cuò)誤:
[root@hadoop-master test-grep]# echo "one two three" | mkdir mkdir: 缺少操作數(shù) Try 'mkdir --help' for more information.| -a | file 從文件中讀入作為sdtin |
| -e | flag ,注意有的時(shí)候可能會(huì)是-E,flag必須是一個(gè)以空格分隔的標(biāo)志,當(dāng)xargs分析到含有flag這個(gè)標(biāo)志的時(shí)候就停止。 |
| -p | 當(dāng)每次執(zhí)行一個(gè)argument的時(shí)候詢問一次用戶。 |
| -n | num 后面加次數(shù),表示命令在執(zhí)行的時(shí)候一次用的argument的個(gè)數(shù),默認(rèn)是用所有的。 |
| -t | 表示先打印命令,然后再執(zhí)行。 |
| -i | 或者是-I,這得看linux支持了,將xargs的每項(xiàng)名稱,一般是一行一行賦值給 {},可以用 {} 代替。 |
| -r | no-run-if-empty 當(dāng)xargs的輸入為空的時(shí)候則停止xargs,不用再去執(zhí)行了。 |
| -s | num 命令行的最大字符數(shù),指的是 xargs 后面那個(gè)命令的最大命令行字符數(shù)。 |
| -L | num 從標(biāo)準(zhǔn)輸入一次讀取 num 行送給 command 命令。 |
| -l | 同 -L。 |
| -d | delim 分隔符,默認(rèn)的xargs分隔符是回車,argument的分隔符是空格,這里修改的是xargs的分隔符。 |
| -x | exit的意思,主要是配合-s使用。。 |
| -P | 修改最大的進(jìn)程數(shù),默認(rèn)是1,為0時(shí)候?yàn)閍s many as it can ,這個(gè)例子我沒有想到,應(yīng)該平時(shí)都用不到的吧。 |
三、xargs命令的實(shí)例
1、創(chuàng)建多個(gè)文件目錄
[root@hadoop-master test-grep]# echo "one two three" | mkdir遞歸創(chuàng)建多個(gè)文件目錄:
[root@hadoop-master dir1]# echo "dir1/20201011 dir2/20201011 dir3/20201011"|xargs mkdir -p2、多行內(nèi)容變單行輸出
[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 grep3、將內(nèi)容按照","分隔
[root@hadoop-master test-grep]# echo "leo,leo,leo" | xargs -d',' leo leo leo4、找到所有的txt文件并壓縮
[root@hadoop-master test-grep]# find . -type f -name "*.txt" -print | xargs tar -czvf txts.tar.gz ./file.txt ./dir2/file.txt5、找到所有的txt文件并刪除(慎用)
[root@hadoop-master test-grep]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f刪除符合條件的目錄下文件,目錄結(jié)構(gòu)如下,需要?jiǎng)h除時(shí)間是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下載一個(gè):
[root@hadoop-master dir1]# yum install tree6、配合ps和kill批量殺掉進(jìn)程(實(shí)用)
[root@hadoop-master test-grep]# ps -ef|grep -v 'grep'|grep '測(cè)試程序'|awk '{print $2}'|xargs kill -97、配合cp將文件拷貝到多個(gè)目錄(實(shí)用)
[root@hadoop-master BJ]# echo 20201011 20201012 20201013 | xargs -n 1 cp -v ./test01.txt打印結(jié)果:
20201011、20201012、20201013 是三個(gè)目錄,。/test01.txt代表需要復(fù)制的文件
-n 1 :表示每一個(gè)命令行只有一個(gè)參數(shù),并且傳給cp命令
cp :表示復(fù)制命令
-v:verbose表示將復(fù)制過程中的詳細(xì)信息顯示出來
當(dāng)然可以正則匹配目錄和文件名:
[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"總結(jié)
以上是生活随笔為你收集整理的每天学一点儿shell:xargs 命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 每天学一点儿shell:Linux中cr
- 下一篇: 每天学一点儿shell:猜数字游戏