# 学习 ```bash find /opt/test/ -type f | awk -F":" '{print $1}' |xargs sed -i's/linux/windows/g' # 利用 -n 參數決定一行輸出幾個 Item echo a b c d e f| xargs -n 3 # grep 出目前目錄底下所有 PHP 檔案有使用到 SongAll Model 的檔案 find . -name "*.php" | xargs grep SongAll # 用 find 找出 *.garbage 系列的檔案並刪除 find . -name "*.garbage" | xargs rm # 為了避免換行字元造成 xargs 誤判成多個檔案,因此只使用 -print0 將換行改成用 ASCII 的 NULL('\0')字元取代 # xargs 也要使用 -0 參數將判斷改成 '\0' find . -name "*.garbage" -print0 | xargs -0 rm # 利用 -I 參數將輸出的結果暫存進變數,以供後續指令使用 # 以 song.list 每行資料的為檔名,建立出一系列 {song_id}.song_id 的檔案 # 冷知識:變數名稱傳統上都用 %,原因是現下許多系統的 command 或者程式語言的 printf 系列語法輸出的 prefix 都是 % 開頭 cat song.list | xargs -I % touch %.song_id # 利用 -S 參數調整參數的最大長度 cat file.list | xargs -S 1024 -I % php newFile.php % # 利用 -P 參數選擇執行的 process 數量 # 請謹慎使用 cat song.list | xargs -P 5 -I % php deleteSong.php % ls | xargs -P 2 -I% echo "This: [%]" # 查找/tmp目录中文件名为core的文件,并删除。 # 使用find使用-print0选项将输入用null分隔,避免文件名中包含空格造成处理出错 find /tmp -name core -type f -print0 | xargs -0 /bin/rm -f find /tmp -depth -name core -type f -delete # 使用sh -c执行复杂的处理 ls / |xargs -I% sh -c 'n=%;echo "[$n]"' # 查看所有java进程的线程数量 jps |xargs -I{} sh -c 'echo -n {} threads:;ps auxTH|grep -v grep|grep $(echo {}|awk "{print \$1}")|wc -l' jps |xargs -I{} sh -c 'echo -n {} threads:;p=$(echo {}|awk "{print \$1}");ps auxTH|grep -v grep|grep $p|wc -l' ###################使用 i 参数 ################## find . -type f -name "*.txt" | xargs -i cp {}  /tmp/k/ ###################  使用 I  参数 ################ find . -type f -name "*.txt" | xargs -I {} cp {}  /tmp/n/ # 加 -i 参数 直接用 {} 就能代替管道之前的标准输出的内容 # 加 -I 参数 需要事先指定替换字符 ps -aux | grep ping6 | cut -c 9-15 | xargs kill -9 # 杀掉所有nsq相关进程 ps -ef | grep nsq | grep -v grep | awk '{print $2}' | xargs kill ps -ef | grep ora|grep “LOCAL=YES”|awk ‘{print $2}’|xargs -I {} kill -9 {} ps -ef | grep ora|grep “LOCAL=NO”|awk ‘{print $2}’|xargs -I {} kill -9 {} ps aux | grep qemu | awk'{print $ 2}'| xargs kill -9 ps x | grep qemu | grep -v grep | cut -c 1-5 | xargs kill -9 # 使用awk批量杀进程的命令: ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}'|sh # 列出了当前主机中运行的进程中包含firefox关键字的进程 ps -ef | grep firefox | grep -v grep # 列出了要kill掉这些进程的命令,并将之打印在了屏幕上 ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}' # 后面加上|sh后,则执行这些命令,进而杀掉了这些进程 ps -ef | grep firefox | grep -v grep | awk '{print "kill -9 "$2}' | sh # 使用cut批量杀进程的命令 ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -9 # 列出了当前主机中运行的进程中包含firefox关键字的进程 ps -ef | grep firefox | grep -v grep # 截取第9至15字符(进程id),列出了要kill掉这些进程的id,并将之打印在了屏幕上 ps -ef | grep firefox | grep -v grep | cut -c 9-15 # 后面加上'xargs kill -9'后,则执行这些命令,进而杀掉了这些进程 ps -ef | grep firefox | grep -v grep | cut -c 9-15 | xargs kill -9 ## 让xargs忽略一个空结果 --no-run-if-empty -r # If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension. sudo ss --listening --tcp --processes "( sport = 8880 )" | grep -oP 'pid=\K\d+' |xargs --no-run-if-empty ps hu --pid ```