最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux find命令之xargs簡(jiǎn)單概述

 更新時(shí)間:2020年08月21日 09:25:18   作者:優(yōu)雅的程序yuan  
這篇文章主要為大家詳細(xì)介紹了linux find命令之xargs的簡(jiǎn)單使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

在使用 find命令的-exec選項(xiàng)處理匹配到的文件時(shí), find命令將所有匹配到的文件一起傳遞給exec執(zhí)行。但有些系統(tǒng)對(duì)能夠傳遞給exec的命令長(zhǎng)度有限制,這樣在find命令運(yùn)行幾分鐘之后,就會(huì)出現(xiàn)溢出錯(cuò)誤。錯(cuò)誤信息通常是“參數(shù)列太長(zhǎng)”或“參數(shù)列溢出”。這就是xargs命令的用處所在,特別是與find命令一起使用。

find命令把匹配到的文件傳遞給xargs命令,而xargs命令每次只獲取一部分文件而不是全部,不像-exec選項(xiàng)那樣。這樣它可以先處理最先獲取的一部分文件,然后是下一批,并如此繼續(xù)下去。

在有些系統(tǒng)中,使用-exec選項(xiàng)會(huì)為處理每一個(gè)匹配到的文件而發(fā)起一個(gè)相應(yīng)的進(jìn)程,并非將匹配到的文件全部作為參數(shù)一次執(zhí)行;這樣在有些情況下就會(huì)出現(xiàn)進(jìn)程過多,系統(tǒng)性能下降的問題,因而效率不高; 而使用xargs命令則只有一個(gè)進(jìn)程。另外,在使用xargs命令時(shí),究竟是一次獲取所有的參數(shù),還是分批取得參數(shù),以及每一次獲取參數(shù)的數(shù)目都會(huì)根據(jù)該命令的選項(xiàng)及系統(tǒng)內(nèi)核中相應(yīng)的可調(diào)參數(shù)來確定。

使用實(shí)例:

實(shí)例1: 查找系統(tǒng)中的每一個(gè)普通文件,然后使用xargs命令來測(cè)試它們分別屬于哪類文件

命令:

find . -type f -print | xargs file

輸出:

[root@localhost test]# ll
總計(jì) 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -type f -print | xargs file
./log2014.log: empty
./log2013.log: empty
./log2012.log: ASCII text
[root@localhost test]#

實(shí)例2:在整個(gè)系統(tǒng)中查找內(nèi)存信息轉(zhuǎn)儲(chǔ)文件(core dump) ,然后把結(jié)果保存到/tmp/core.log 文件中

命令:

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

輸出:

[root@localhost test]# find / -name "core" -print | xargs echo "" >/tmp/core.log
[root@localhost test]# cd /tmp
[root@localhost tmp]# ll
總計(jì) 16
-rw-r--r-- 1 root root 1524 11-12 22:29 core.log
drwx------ 2 root root 4096 11-12 22:24 ssh-TzcZDx1766
drwx------ 2 root root 4096 11-12 22:28 ssh-ykiRPk1815
drwx------ 2 root root 4096 11-03 07:11 vmware-root

實(shí)例3:在當(dāng)前目錄下查找所有用戶具有讀、寫和執(zhí)行權(quán)限的文件,并收回相應(yīng)的寫權(quán)限

命令:

find . -perm -7 -print | xargs chmod o-w

輸出:

[root@localhost test]# ll
總計(jì) 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -perm -7 -print | xargs chmod o-w
[root@localhost test]# ll
總計(jì) 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 19:32 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#

說明:

執(zhí)行命令后,文件夾scf、test3和test4的權(quán)限都發(fā)生改變

實(shí)例4:用grep命令在所有的普通文件中搜索hostname這個(gè)詞

命令:

find . -type f -print | xargs grep "hostname"

輸出:

[root@localhost test]# find . -type f -print | xargs grep "hostname"
./log2013.log:hostnamebaidu=baidu.com
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#

實(shí)例5:用grep命令在當(dāng)前目錄下的所有普通文件中搜索hostnames這個(gè)詞

命令:

find . -name \* -type f -print | xargs grep "hostnames"


輸出:

[root@peida test]# find . -name \* -type f -print | xargs grep "hostnames"
./log2013.log:hostnamesina=sina.com
./log2013.log:hostnames=true[root@localhost test]#

說明:

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

實(shí)例6:使用xargs執(zhí)行mv

命令:

find . -name "*.log" | xargs -i mv {} test4

輸出:

[root@localhost test]# ll
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root  61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:54 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計(jì) 0
[root@localhost test4]# cd ..
[root@localhost test]# find . -name "*.log" | xargs -i mv {} test4
[root@localhost test]# ll
總計(jì) 12
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4/
[root@localhost test4]# ll
總計(jì) 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root  61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:54 log2014.log
[root@localhost test4]#

實(shí)例7:find后執(zhí)行xargs提示xargs: argument line too long解決方法:

命令:

find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f

輸出:

[root@pd test4]# find . -type f -atime +0 -print0 | xargs -0 -l1 -t rm -f
rm -f 
[root@pdtest4]#

說明:

-l1是一次處理一個(gè);-t是處理之前打印出命令

實(shí)例8:使用-i參數(shù)默認(rèn)的前面輸出用{}代替,-I參數(shù)可以指定其他代替字符,如例子中的[]

命令:

find . -name "file" | xargs -I [] cp [] ..

輸出:

[root@localhost test]# ll
總計(jì) 12
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test4
[root@localhost test4]# find . -name "file" | xargs -I [] cp [] ..
[root@localhost test4]# ll
總計(jì) 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root  61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root  0 11-12 22:54 log2014.log
[root@localhost test4]# cd ..
[root@localhost test]# ll
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root  61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root  0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 05:50 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#

說明:

使用-i參數(shù)默認(rèn)的前面輸出用{}代替,-I參數(shù)可以指定其他代替字符,如例子中的[]

實(shí)例9:xargs的-p參數(shù)的使用

命令:

find . -name "*.log" | xargs -p -i mv {} ..

輸出:

[root@localhost test3]# ll
總計(jì) 0
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root  61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root  0 11-13 06:03 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:06 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# cd test3
[root@localhost test3]# find . -name "*.log" | xargs -p -i mv {} ..
mv ./log2015.log .. ?...y
[root@localhost test3]# ll
總計(jì) 0
[root@localhost test3]# cd ..
[root@localhost test]# ll
總計(jì) 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root  61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root  0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root  0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]#

說明:

-p參數(shù)會(huì)提示讓你確認(rèn)是否執(zhí)行后面的命令,y執(zhí)行,n不執(zhí)行。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 用shell批量修改文件名的方法

    用shell批量修改文件名的方法

    這篇文章主要為大家介紹了shell對(duì)文件夾中全部文件的名稱加以批量替換、修改的方法,文中的示例代碼講解詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • linux?shell文件轉(zhuǎn)碼iconv命令的使用

    linux?shell文件轉(zhuǎn)碼iconv命令的使用

    本文主要介紹了linux?shell文件轉(zhuǎn)碼iconv命令的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • Shell腳本實(shí)現(xiàn)檢測(cè)文件是否被修改過代碼分享

    Shell腳本實(shí)現(xiàn)檢測(cè)文件是否被修改過代碼分享

    這篇文章主要介紹了Shell腳本實(shí)現(xiàn)的檢測(cè)文件是否被修改代碼分享,其實(shí)了解了原理就可以做很多事了,需要的朋友可以參考下
    2014-09-09
  • linux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)

    linux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)

    下面小編就為大家?guī)硪黄猯inux下安裝配置Memcache和PHP環(huán)境的實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • shell實(shí)現(xiàn)圖書管理系統(tǒng)

    shell實(shí)現(xiàn)圖書管理系統(tǒng)

    這篇文章主要介紹了shell實(shí)現(xiàn)圖書管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • 普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤(strace)解決分析

    普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤(strace)解決分析

    這篇文章主要為大家介紹了普通用戶啟動(dòng)supervisor報(bào)HTTP錯(cuò)誤strace的問題解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Linux下Tomcat啟動(dòng)正常,但瀏覽器無法訪問的解決方法

    Linux下Tomcat啟動(dòng)正常,但瀏覽器無法訪問的解決方法

    下面小編就為大家?guī)硪黄狶inux下Tomcat啟動(dòng)正常,但瀏覽器無法訪問的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • shell 腳本安裝PHP擴(kuò)展的簡(jiǎn)單方法

    shell 腳本安裝PHP擴(kuò)展的簡(jiǎn)單方法

    下面小編就為大家?guī)硪黄猻hell 腳本安裝PHP擴(kuò)展的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • Shell腳本調(diào)試?-n?-v?-x?-c的具體用法

    Shell腳本調(diào)試?-n?-v?-x?-c的具體用法

    本文主要介紹了Shell腳本調(diào)試?-n?-v?-x?-c的具體用法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • bash腳本截取字符串的兩種方法

    bash腳本截取字符串的兩種方法

    本文主要介紹了bash腳本截取字符串的兩種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論

阳新县| 临澧县| 噶尔县| 横峰县| 简阳市| 佛坪县| 两当县| 万源市| 福泉市| 民乐县| 赣州市| 集安市| 伊宁县| 汝城县| 永和县| 韩城市| 安溪县| 嘉义市| 广安市| 福州市| 呼图壁县| 毕节市| 井冈山市| 同江市| 唐山市| 封开县| 侯马市| 张家界市| 山西省| 前郭尔| 彩票| 辽中县| 成都市| 澜沧| 定南县| 民丰县| 固镇县| 江口县| 湾仔区| 政和县| 郧西县|