linux系統中的rm rf命令詳解?1,查看xargs命令版本[root@test3 ~]# xargs --version
,我來為大家科普一下關于linux系統中的rm rf命令詳解?以下内容希望對你有幫助!
1,查看xargs命令版本
[root@test3 ~]# xargs --version
2、多行變單行
[root@test3 ~]# cat test.txt
1 this is a test!
2 this is the second number.
3 are you there?
[root@test3 ~]# cat test.txt |xargs
1 this is a test! 2 this is the second number. 3 are you there?
3、單行轉多行
[root@test3 ~]# cat 1.txt
hello, where are you? this is a test!
[root@test3 ~]# cat 1.txt |xargs -n3
hello, where are
you? this is
a test!
4、按照指定分隔符分割字符串
[root@test3 ~]# echo “wuhs@sunr@sunr@wuhs@wuzy” |xargs -d@
wuhs sunr sunr wuhs wuzy
[root@test3 ~]# echo “wuhs@sunr@sunr@wuhs@wuzy” |xargs -d@ -n2
wuhs sunr
sunr wuhs
wuzy
5、删除查找到的文件
[root@test3 ~]# find . -name test.txt
./test.txt
[root@test3 ~]# find . -name test.txt |xargs rm -rf
[root@test3 ~]# find . -name test.txt
6、将查找到的文件打包
[root@test3 ~]# find . -type f -name “*.cfg” |xargs tar -zcvf cfg.tar.gz
./original-ks.cfg
./anaconda-ks.cfg
7、批量複制文件到指定目錄
[root@test3 ~]# find . -type f -name “.cfg" |xargs -n1 -I {} cp {} /home
[root@test3 ~]# find . -type f -name ".txt” |xargs -n1 -I {} cp {} /home
[root@test3 ~]# ll /home/
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:34 1.txt
-rw-r–r-- 1 root root 64 Jul 21 16:34 2.txt
-rw------- 1 root root 2762 Jul 21 16:34 anaconda-ks.cfg
drwx------ 7 es es 184 Jun 22 15:47 es
drwx------ 7 gzgk gzgk 198 Jul 20 14:06 gzgk
-rw------- 1 root root 2042 Jul 21 16:34 original-ks.cfg
drwx------. 15 wuhs wuhs 4096 Dec 16 2020 wuhs
8、删除文件名包含空格的文件
find命令有一個特别的參數 -print 0,指定輸出的文件列表以null分隔。然後,xargs命令的 -0參數表示用null當作分隔符。
[root@test3 ~]# touch “abc d”
[root@test3 ~]# ll
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-r–r-- 1 root root 64 Jul 21 15:57 2.txt
-rw-r–r-- 1 root root 0 Jul 21 16:43 abc d
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
[root@test3 ~]# find . -type f -name abc* |xargs -0 rm
rm: cannot remove ‘./abc d\n’: No such file or directory
[root@test3 ~]# find . -type f -name abc* -print0 |xargs -0 rm
[root@test3 ~]# ll
total 20
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-r–r-- 1 root root 64 Jul 21 15:57 2.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
9、打印待執行命令确認後執行
使用 -p 參數打印待執行的命令,輸入y或yes回車确認後執行,直接回車不執行命令。
[root@test3 ~]# find . -type f -name 1.txt |xargs -p rm
rm ./1.txt ?..
[root@test3 ~]# find . -type f -name 2.txt |xargs -p rm
rm ./2.txt ?..yes
[root@test3 ~]# ll
total 16
-rw-r–r-- 1 root root 38 Jul 21 16:01 1.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
10、打印最終執行命令但不做确認
-t 參數則是打印出最終要執行的命令,然後直接執行,不需要用戶确認。
[root@test3 ~]# find . -type f -name 1.txt |xargs -t rm
rm ./1.txt
11、批量創建目錄
[root@test3 ~]# echo “one two there” |xargs mkdir
[root@test3 ~]# ll
total 16
-rw-r–r-- 1 root root 64 Jul 21 16:53 2.txt
-rw-------. 1 root root 2762 Dec 16 2020 anaconda-ks.cfg
-rw-r–r-- 1 root root 1513 Jul 21 16:29 cfg.tar.gz
drwxr-xr-x 2 root root 6 Jul 21 16:58 one
-rw-------. 1 root root 2042 Dec 16 2020 original-ks.cfg
drwxr-xr-x 2 root root 6 Jul 21 16:58 there
drwxr-xr-x 2 root root 6 Jul 21 16:58 two
12、将命令參數傳遞給多個命令
使用 -I 參數将命令行參數傳給多個命令,-I file指定每一項命令行參數的替代字符串。選項是将每一行參數傳遞給echo和mkdir命令,即顯示并創建目錄。
[root@test3 ~]# cat 3.txt
one
two
three
[root@test3 ~]# cat 3.txt |xargs -I file sh -c ‘echo file;mkdir file’
one
two
three
三、使用語法及參數說明
1、使用語法
command | xargs [選項] command
2、參數說明
-0, --null:項目之間用null分隔,而不是空格。禁用引号和反斜杠處理
-a, --arg-file=FILE: 從文件中讀入作為 stdin
-d, --delimiter=CHARACTER: 自定義分隔符
-E END: xargs分析到含有flag這個标志的時候就停止
-e [END], --eof[=END]: 注意有的時候可能會是-E,flag必須是一個以空格分隔的标志,當xargs分析到含有flag這個标志的時候就停止。
–help: 打印幫助選項
-I R: 将xargs的每項名稱,一般是一行一行賦值給 {},可以用 {} 代替
-i,–replace=[R]: 将xargs的每項名稱,一般是一行一行賦值給 {},可以用 {} 代替
-L,-l, --max-lines=MAX-LINES: 如果标準輸入包含多行,-L參數指定多少行作為一個命令行參數。
-l: 從标準輸入一次讀取 num 行送給 command 命令。
-n, --max-args=MAX-ARGS: 後面加次數,表示命令在執行的時候一次用的argument的個數,默認是用所有的
-P, --max-procs=MAX-PROCS: xargs默認隻用一個進程執行命令。如果命令要執行多次,必須等上一次執行完,才能執行下一次,0表示不限制進程數。
-p, --interactive: 當每次執行一個argument的時候詢問一次用戶。
–process-slot-var=VAR: 在子進程中設置環境變量var過程
-r, --no-run-if-empty: 當xargs的輸入為空的時候則停止xargs,不用再去執行了。
-s, --max-chars=MAX-CHARS: 命令行的最大字符數,指的是 xargs 後面那個命令的最大命令行字符數。
–show-limits: 顯示命令行長度的限制
-t, --verbose: 在執行之前詳細打印命令
–version: 打印版本号信息
-x, --exit: exit的意思,主要是配合-s使用
更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!