Linux 命令大全
1撰筷、命令幫助手冊—help陈惰、man
# 查看命令的幫助文檔
man 命令
命令 --help
info 命令
# 查看命令存在哪里
[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
2 、 更改目錄—cd
# 進入opt目錄
cd /opt
#幾個特殊的符號表示
. 當(dāng)前目錄
.. 上一級目錄
- 上一次目錄
~ home目錄 ,root用戶的話進入 root目錄, 普通用戶的話進入 home/用戶名 目錄
3 平委、 創(chuàng)建文件夾—mkdir
# 創(chuàng)建一個abc文件夾
mkdir abc
# 在abc下創(chuàng)建一個123文件夾廉赔,若abc不存在,則無法創(chuàng)建123
mkdir abc/123
# 遞歸創(chuàng)建文件夾馏艾,若abc不存在,則會先創(chuàng)建abc房资,再創(chuàng)建123
mkdir -p abc/123
# 批量創(chuàng)建文件夾
mkdir {a,b,c,d}
# 批量創(chuàng)建格式類似的文件夾轰异, 創(chuàng)建 abc1 abc2..到abc10 的文件夾
mkdir abc{1..10}
4 溉浙、查看目錄下的文件—ls
語法 : ls 可選參數(shù) 文件夾
-a 列出所有文件戳稽,包含隱藏文件
-l 同 ll 命令,顯示文件詳情
-h 以人們常見的形式顯示文件大小
-t 根據(jù)文件的修改時間倒序
-F 顯示的文件互躬,不同類型有不一樣的后綴 /: 表示文件夾
@:表示軟連接(類似快捷方式)
*:表示可執(zhí)行文件
-d 顯示文件夾信息吼渡,不顯示文件夾下的內(nèi)容
-S 按照文件的大小逆序顯示
-r 反轉(zhuǎn), -S是文件從大到小排列寄雀, -Sr是文件從小到大排列
-i 顯示文件的inode節(jié)點
5 、系統(tǒng)時間—date 、 hwclock
# 顯示系統(tǒng)時間
[root@yqs work]# date
Fri Nov 20 14:06:16 CST 2020
# 指定格式顯示系統(tǒng)時間
[root@yqs work]# date "+%Y-%m-%d"
2020-11-20
[root@yqs work]# date "+%H:%M:%S"
14:57:52
# 手動設(shè)置時間
[root@yqs ~]# date -s '2020-11-20 19:45:30'
# 手動設(shè)置后,重啟后會失效,需要將時間寫入BIOS: 將系統(tǒng)時間同步到硬件時間上
[root@yqs ~]# hwclock -w
# 將硬件時間同步到系統(tǒng)時間上
[root@yqs ~]# hwclock -s
# 其他顯示時間命令
[root@yqs ~]# uptime
19:49:12 up 5 min, 1 user, load average: 0.00, 0.01, 0.01
#系統(tǒng)時間 運行時間 登錄用戶數(shù) 過去 1分鐘 5分鐘 15分鐘的系統(tǒng)負載的均值
5.1、關(guān)于硬件時間跟系統(tǒng)時間
硬件時間是只bios里面的時間命黔,系統(tǒng)時間是kernel中的時鐘
所有系統(tǒng)執(zhí)行的命令和函數(shù)都是依照系統(tǒng)時鐘而設(shè)定悍募,
當(dāng)Linux啟動時,系統(tǒng)時鐘會去讀取硬件時鐘的設(shè)定洋机,之后系統(tǒng)時鐘即獨立運作。
hwclock是一種訪問硬件時鐘的工具
顯示硬件時鐘 hwclock 或者 hwclock -r 或者 hwclock --show
詳細文檔參閱:https://blog.51cto.com/shisen/1847111
6喜鼓、文件查找—find 衔肢、 locate
6.1庄岖、stat
# stat 查看文件詳細信息
[root@localhost opt]# stat geci.txt
File: ‘geci.txt’
Size: 1111 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 8768655 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:usr_t:s0
Access: 2021-06-28 15:58:40.789724487 +0800
Modify: 2021-06-28 15:58:32.433725026 +0800
Change: 2021-06-28 15:58:32.434725026 +0800
Birth: -
# file : 文件名字
# Size : 文件大小/字節(jié)
# Access : atime 指文件上一次打開的時間
# Modify :mtime 指文件內(nèi)容上一次變動的時間
# Change :ctime 指 inode 上一次變動的時間
6.2、find
# find 在哪里找 找什么
# 根據(jù)名稱搜索文件,以下命令---在 根路徑 搜索名稱為'init'的文件,精確查找
[root@yqs ~]# find / -name init
/sys/fs/selinux/initial_contexts/init
/etc/sysconfig/init
# 根據(jù)名稱查找,模糊匹配, 結(jié)果太多,可用 | more 分頁
[root@yqs ~]# find / -name *init* | more
/boot/initramfs-0-rescue-eeab2f6320ae4cc99443c4e9e74b0760.img
/boot/initramfs-3.10.0-1127.el7.x86_64.img
/boot/initramfs-3.10.0-1127.el7.x86_64kdump.img
# 根據(jù)名稱查找,模糊匹配,指定文件名字段數(shù),init開頭,且向后模糊匹配三個字符
[root@yqs ~]# find / -name init???
/dev/initctl
/run/systemd/initctl
/etc/inittab
/usr/lib/dracut/modules.d/99base/init.sh
# 忽略大小寫查找 "." 表示在當(dāng)前目錄下查找, -i表示忽略大小寫
[root@yqs test]# find . -iname xuzhu
./xuzhu
./XuZhu
# 指定目錄層級搜索 -maxdepth n -mindepth n
[root@localhost opt]# find . -maxdepth 1 -name '123.txt'
./123.txt
[root@localhost opt]# find . -maxdepth 2 -name '123.txt'
./123.txt
./dir1/123.txt
[root@localhost opt]# find . -mindepth 2 -name '123.txt'
./dir1/123.txt
# 根據(jù)文件類型 -type b(塊設(shè)備文件)-c(字符設(shè)備文件) f(普通文件) d(目錄文件) p(管道文件) l(符號鏈接文件) -s(socket文件)
[root@localhost opt]# find . -type f -name 'yqs*'
./yqs.txt
[root@localhost opt]# find . -type d -name 'yqs*'
./yqs
# 根據(jù)文件大小 -size n[bckwMG] c(字節(jié)) k w(2字節(jié)) M G b(代表512位原組的區(qū)塊)
[root@localhost opt]# find . -size 8c +8 超過8字節(jié) -8 8字節(jié)以內(nèi)
./123.txt
# 根據(jù)各種時間 -atime -ctime -mtime
-atime -2 兩天內(nèi)訪問的文件
-atime 2 恰好在兩天前被訪問的文件
-atime +2 超過兩天前被訪問的文件
# 根據(jù)文件所屬分組 -group
# 根據(jù)文件的權(quán)限 -perm
# 根據(jù)文件所屬用戶 -user
# 取反 隅忿!
[root@localhost opt]# find . -type f -name 'yqs*'
./yqs.txt
[root@localhost opt]# find . ! -type f -name 'yqs*'
./yqs
# -a 交集 -o 并集
# 排除某個目錄 -path -prune 再加上 -o 取并集 -print 將取出的結(jié)果標(biāo)準(zhǔn)輸出
[root@localhost opt]# find . -path './dir1' -prune -o -name '123*' -print
./123.txt
# 找到文件后刪除
[root@localhost opt]# find . -name '123.txt' -ok rm {} \;
< rm ... ./123.txt > ? y
6.3背桐、locate
#該命令有點類似windows中的everything軟件,查找速度快,使用前需要先下載
[root@yqs test]# yum -y install mlocate
#下載之后要先創(chuàng)建其locatedb
[root@yqs test]# updatedb
#全局搜索
[root@yqs test]# locate xuzhu
/root/test/xuzhu
#忽略大小寫全局搜索
[root@yqs test]# locate -i xuzhu
/root/test/XuZhu
/root/test/xuzhu
#指定目錄搜索,其實就是搜索全路徑,locate會保存一份文件,里面記錄了所有文件的全路徑
#在/root/test目錄下搜索以x開頭的文件
[root@yqs test]# locate /root/test/x
/root/test/xiaofeng
/root/test/xuzhu
#搜索用戶主目錄下弊仪,所有以t開頭的文件
[root@yqs ~]# locate ~/t
/root/test
/root/test/XuZhu
/root/test/xiaofeng
/root/test/xuzhu
7驳癌、創(chuàng)建普通文件—touch、echo
# touch 其作用為修改文件時間為當(dāng)前時間曲横, 若文件不存在喂柒,則會創(chuàng)建該文件
# 創(chuàng)建abc.txt
touch abc.txt
# 創(chuàng)建多個文件, 創(chuàng)建 abc1 abc2 ... abc10
touch abc{0..10}
# 創(chuàng)建 123a 123b ... 123z
touch 123{a..z}
# 修改文件時間不瓶, 若文件不存在禾嫉,不創(chuàng)建新文件
touch -c 123.txt
# 修改文件時間為指定時間
touch -t 201212120606 123.txt
# 使用echo創(chuàng)建文件, echo命令是用于將輸入的內(nèi)容展示在控制臺上蚊丐,在其后面加上 > 可將內(nèi)容輸出到指定文件中
echo 123 > 1.txt
# >> 表示追加到指定文件
echo 4 >> 1.txt
# 使用vi熙参、vim打開一個不存在的文件,將會創(chuàng)建該文件
vi 1234.txt
8麦备、復(fù)制—cp
# 復(fù)制123.txt文件孽椰,命名為abc.txt
cp 123.txt abc.txt
# 將123.txt 復(fù)制到abc目錄下
cp 123.txt abc
# 復(fù)制多個文件到指定文件夾下
cp 123.txt 456.txt abc
# 復(fù)制以123開頭的文件到abc目錄中,若包含文件夾的話凛篙,要加 -r
cp 123* abc
#復(fù)制文件夾 -r 表示遞歸復(fù)制黍匾,會將源文件夾下的所有文件都復(fù)制
cp -r abc abc2
# 復(fù)制文件且保持其屬性不變 -p
cp -p 123.txt abc.txt
# 復(fù)制軟連接,直接cp的話呛梆,復(fù)制的是軟連接指向的內(nèi)容 加-d 參數(shù)锐涯,可復(fù)制軟連接
cp -d link_123 link_abc
# 覆蓋前詢問 -i , 若abc.txt存在填物,則會詢問是否覆蓋纹腌,其實我們正常的cp命令是個別名,其底層已經(jīng)加了 -i滞磺, 可以通過alias查看
# -i 123.txt abc.txt
9升薯、別名—alias
# 查看別名
[root@yqs test]# alias
alias cp='cp -i'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
# 設(shè)置別名(重新登錄后會失效)
[root@yqs test]# alias ls='ls --color=auto'
# 設(shè)置別名,永久生效——需要修改配置文件击困,普通用戶 ==> ~/.bashrc 涎劈, root用戶 ==> /root/.bashrc
vim /root/.bashrc
#然后執(zhí)行以下命令
source /root/.bashrc
若不生效,則需要在~/.bash_profile中加入命令 source ~/.bashrc 后保存
# 刪除別名
[root@yqs test]# unalias 別名
10、剪貼—mv
# 移動文件到指定目錄下责语,文件名不變
mv 123.txt ./acb
# 移動文件夾及其內(nèi)容到指定目錄下
mv yqs abc
# 移動多個文件到指定目錄下
mv 123.txt 456.txt ./abc
# 修改文件名
mv 123.txt abc.txt
# 移動123開頭的文件(包含文件夾)到abc目錄下
mv 123* abc
# 覆蓋前不詢問 -f
# 覆蓋前詢問 -i
11炮障、刪除—rm
# 刪除文件
rm 123.txt
# 刪除多個文件
rm 123.txt 456.txt
# 刪除文件夾,及其內(nèi)容
rm -r abc
# 刪除123開頭的文件坤候, 包含文件夾的話要加 -r
rm 123*
# 刪除空目錄胁赢,這個嘛,沒啥用白筹,若不是空的智末,刪不掉
rm -d abc
# -v 顯示刪除過程詳情
# 覆蓋前不詢問 -f
# 覆蓋前詢問 -i
# 刪除當(dāng)前目錄下的所有東西
rm -rf ./*
12、開關(guān)機 —shutdown
#重啟命令
shutdown -r now # 立即重啟
shutdown -r 10 # 10分后重啟
shutdown -r 0 # 立即重啟
reboot # 立即重啟
init 6 # 運行級別為6徒河,表示重啟
# 關(guān)機命令
shutdown -h now # 立即關(guān)機
shutdown -h 10 # 10分鐘后關(guān)機
shutdown -h 0 # 立即關(guān)機
poweroff # 立即關(guān)機系馆,且切斷電源
halt # 立即關(guān)機,但是需要手動切斷電源
init 0 # 運行級別為0顽照,0表示關(guān)機
# 注銷
logout # 注銷由蘑,退出登錄
exit # 注銷,退出登錄
# 關(guān)于Linux的運行級別
運行級別0:系統(tǒng)停機狀態(tài)代兵,系統(tǒng)默認運行級別不能設(shè)為0尼酿,否則不能正常啟動
運行級別1:單用戶工作狀態(tài),root權(quán)限植影,用于系統(tǒng)維護裳擎,禁止遠程登陸
運行級別2:多用戶狀態(tài)(沒有NFS)
運行級別3:完全的多用戶狀態(tài)(有NFS),登陸后進入控制臺命令行模式
運行級別4:系統(tǒng)未使用思币,保留
運行級別5:X11控制臺鹿响,登陸后進入圖形GUI模式
運行級別6:系統(tǒng)正常關(guān)閉并重啟,默認運行級別不能設(shè)為6谷饿,否則不能正常啟動
13惶我、快捷鍵
ctrl + c # 終止當(dāng)前操作
ctrl + a # 光標(biāo)移動到行首
ctrl + e # 光標(biāo)移動到行尾
ctrl + u # 刪除光標(biāo)到行首之間的內(nèi)容
ctrl + d # 注銷,退出登錄
ctrl + l # 相當(dāng)于clear博投,清屏
14绸贡、重定向符號
# 輸出覆蓋 >
[root@localhost opt]# echo 與七書不是一本書 > wenben1.txt
[root@localhost opt]# ll
total 8
-rw-r--r--. 1 root root 18 Jun 28 14:57 number.txt
-rw-r--r--. 1 root root 25 Jun 28 15:06 wenben1.txt
# 讀取內(nèi)容 輸出到新文件 >
[root@localhost opt]# cat wenben1.txt > wenben2.txt
[root@localhost opt]# ll
total 12
-rw-r--r--. 1 root root 18 Jun 28 14:57 number.txt
-rw-r--r--. 1 root root 25 Jun 28 15:06 wenben1.txt
-rw-r--r--. 1 root root 25 Jun 28 15:09 wenben2.txt
# 追加內(nèi)容 >>
[root@localhost opt]# echo 火影忍者 >> wenben2.txt
[root@localhost opt]# cat wenben2.txt
與七書不是一本書
火影忍者
# 標(biāo)準(zhǔn)輸入重定向 <
[root@localhost opt]# cat < wenben1.txt
與七書不是一本書
火影忍者
# 將一行數(shù)據(jù)分幾段顯示
[root@localhost opt]# cat number.txt
1 2 3 4 5 6 7 8 9
[root@localhost opt]# xargs -n 3 < number.txt
1 2 3
4 5 6
7 8 9
# 追加輸入玩法 <<, EOF表示文件的邊界符號 end of file
[root@localhost opt]# cat >> shige.txt <<EOF
> 枯藤老樹昏鴉
> 小橋流水人家
> 古道西風(fēng)瘦馬
> EOF
[root@localhost opt]# cat shige.txt
枯藤老樹昏鴉
小橋流水人家
古道西風(fēng)瘦馬
15贬堵、查看文件內(nèi)容—cat
# 查看文件內(nèi)容
cat 123.txt
# 顯示行號
cat -n 123.txt
# 顯示每行結(jié)尾的$符號
[root@localhost opt]# cat -E 123.txt
abcdefg$
# 合并多個文件
[root@localhost opt]# cat shige.txt shige2.txt > shige3.txt
[root@localhost opt]# cat shige3.txt
枯藤老樹昏鴉
小橋流水人家
古道西風(fēng)瘦馬
夕陽西下恃轩,斷腸人在天涯
# 非交互式編輯
[root@localhost opt]# cat >> shige3.txt << EOF
> 作者
> 馬致遠
> EOF
[root@localhost opt]# cat shige3.txt
枯藤老樹昏鴉
小橋流水人家
古道西風(fēng)瘦馬
夕陽西下,斷腸人在天涯
作者
馬致遠
# 將文件內(nèi)容設(shè)置為空
[root@localhost opt]# cat /dev/null > shige3.txt
[root@localhost opt]# cat shige3.txt
[root@localhost opt]#
16黎做、查看文件內(nèi)容—more叉跛、less
# more分屏查看文件 -num ,顯示前幾行, 回車鍵向下翻一行蒸殿,空格鍵向下翻一頁筷厘,=輸出當(dāng)前行的行號
more -5 文件名
# less分屏查看鸣峭,比more更強大,支持先前翻頁酥艳, -n顯示行號
less -N 文件名
17摊溶、查看文件內(nèi)容—head、tail
# head 查看前五行充石,默認是顯示十行
head -5 文件名
# 查看前五個字符
head -c 5 文件名
# tail 查看后五行莫换,默認顯示十行
tail -5 文件名
# 實時更新文件內(nèi)容變化
tail -f 文件名字
# 實時更新文件內(nèi)容變化,若文件不存在骤铃,也會阻塞住拉岁,等文件創(chuàng)建
tail -F 文件名
18、修剪查看文件內(nèi)容—cut
# cut 分割文件內(nèi)容查看
# 查看文件的每行的第3個字符
[root@localhost opt]# cut -c 3 geci.txt
啡
住
想
# 查看文件的每行的第3到第5個字符惰爬,-n:第一到第n個字符喊暖, m-:第m到行尾的字符
[root@localhost opt]# cut -c 3-5 geci.txt
啡離開
住的情
想挽回
# 查看文件的每行的第3和第5個字符,
[root@localhost opt]# cut -c 3,5 geci.txt
啡開
住情
想回
# 將文件根據(jù)指定符號分割撕瞧,并顯示第2個分割部分的內(nèi)容陵叽, -d 指定分割符,-f 指定顯示分割后的區(qū)域丛版,用法給 -c 一樣
[root@localhost opt]# cat studet.txt
1:小明:男:17歲
2:小紅:女:17歲
3:小藍:男:18歲
[root@localhost opt]# cut -d ':' -f 2 studet.txt
小明
小紅
小藍
19巩掺、排序查看文件內(nèi)容—sort
文件內(nèi)容:
1:小一:男:17
7:小七:男:18
3:小三:男:18
2:小二:女:17
8:小八:男:16
6:小六:女:17
4:小四:女:20
5:小五:女:17
9:小九:女:19
# 按照每行的數(shù)字大小排序, -n 按照數(shù)字從小到大排序硼婿,可再加 -r 反轉(zhuǎn)
[root@localhost opt]# sort -n studet.txt
1:小一:男:17
2:小二:女:17
3:小三:男:18
4:小四:女:20
5:小五:女:17
6:小六:女:17
7:小七:男:18
8:小八:男:16
9:小九:女:19
# 按照指定分割符分割后的指定區(qū)域進行排序锌半,根據(jù)年齡排序禽车, -t:指定分隔符寇漫,-k 選擇分割后的塊
[root@localhost opt]# sort -n -t ':' -k 4 studet.txt
8:小八:男:16
1:小一:男:17
2:小二:女:17
5:小五:女:17
6:小六:女:17
3:小三:男:18
7:小七:男:18
9:小九:女:19
4:小四:女:20
# 去重排序 根據(jù)年齡去重排序, -u:根據(jù)指定塊去重
[root@localhost opt]# sort -n -u -t ':' -k 4 studet.txt
8:小八:男:16
1:小一:男:17
7:小七:男:18
9:小九:女:19
4:小四:女:20
20、去重查看文件內(nèi)容—uniq
文件內(nèi)容:
1:小一:男:17
1:小一:男:17
7:小七:男:18
3:小三:男:18
2:小二:女:17
2:小二:女:17
8:小八:男:16
1:小一:男:17
6:小六:女:17
4:小四:女:20
4:小四:女:20
5:小五:女:17
1:小一:男:17
9:小九:女:19
# 連續(xù)重復(fù)的去重殉摔,不在連續(xù)位置的重復(fù)不去除
[root@localhost opt]# uniq studet.txt
1:小一:男:17
7:小七:男:18
3:小三:男:18
2:小二:女:17
8:小八:男:16
1:小一:男:17
6:小六:女:17
4:小四:女:20
5:小五:女:17
1:小一:男:17
9:小九:女:19
# 完全去除重復(fù)
[root@localhost opt]# sort -n studet.txt | uniq
1:小一:男:17
2:小二:女:17
3:小三:男:18
4:小四:女:20
5:小五:女:17
6:小六:女:17
7:小七:男:18
8:小八:男:16
9:小九:女:19
# 完全去除重復(fù)州胳,顯示重復(fù)次數(shù)
[root@localhost opt]# sort -n studet.txt | uniq -c
4 1:小一:男:17
2 2:小二:女:17
1 3:小三:男:18
2 4:小四:女:20
1 5:小五:女:17
1 6:小六:女:17
1 7:小七:男:18
1 8:小八:男:16
1 9:小九:女:19
# 完全去除重復(fù),顯示只出現(xiàn)過一次的數(shù)據(jù)
[root@localhost opt]# sort -n studet.txt | uniq -u
3:小三:男:18
5:小五:女:17
6:小六:女:17
7:小七:男:18
8:小八:男:16
9:小九:女:19
# 完全去除重復(fù)逸月,顯示只出現(xiàn)過多次的數(shù)據(jù)
[root@localhost opt]# sort -n studet.txt | uniq -d
1:小一:男:17
2:小二:女:17
4:小四:女:20
21栓撞、統(tǒng)計文件內(nèi)容—wc
文本內(nèi)容:
火影忍者
一人之下
海賊王
龍珠
與七書不是一本書
# 統(tǒng)計文件行數(shù)
[root@localhost opt]# wc -l dongman.txt
5 dongman.txt
# 統(tǒng)計字符數(shù), 包含了每行結(jié)尾的$符碗硬,共26個字符
[root@localhost opt]# wc -m dongman.txt
26 dongman.txt
# 統(tǒng)計單詞數(shù)瓤湘, 以空格去統(tǒng)計的。
[root@localhost opt]# wc -w dongman.txt
5 dongman.txt
# 統(tǒng)計最長行字符數(shù)恩尾,按字節(jié)算的
[root@localhost opt]# echo '與七書不是一本書' | wc -L
16
22弛说、標(biāo)準(zhǔn)輸入替換— tr
本文內(nèi)容:
You make me cry make me smile
# 將文本中的小寫字母替換為大寫
[root@localhost opt]# cat english.txt | tr '[a-z]' '[A-Z]'
YOU MAKE ME CRY MAKE ME SMILE
# 替換a 為 A
[root@localhost opt]# cat english.txt | tr 'a' 'A'
You mAke me cry mAke me smile
# 刪除內(nèi)容中所有的 me
[root@localhost opt]# cat english.txt | tr -d 'me'
You ak cry ak sil
# 把連續(xù)重復(fù)的指定字符去重顯示,
[root@localhost opt]# echo 'aabbccdefgg' | tr -s 'abcd'
abcdefgg
23翰意、參數(shù)傳遞—xargs
# xargs(英文全拼: eXtended ARGuments)是給命令傳遞參數(shù)的一個過濾器木人,也是組合多個命令的一個工具信柿。
# xargs 可以將管道或標(biāo)準(zhǔn)輸入(stdin)數(shù)據(jù)轉(zhuǎn)換成命令行參數(shù),也能夠從文件的輸出中讀取數(shù)據(jù)醒第。
# xargs 也可以將單行或多行文本輸入轉(zhuǎn)換為其他格式渔嚷,例如多行變單行,單行變多行稠曼。
# xargs 默認的命令是 echo形病,這意味著通過管道傳遞給 xargs 的輸入將會包含換行和空白,不過通過 xargs 的處理霞幅,換行和空白將被空格取代窒朋。
# xargs 是一個強有力的命令,它能夠捕獲一個命令的輸出蝗岖,然后傳遞給另外一個命令侥猩。
# xargs 一般是和管道一起使用。
# 之所以能用到這個命令抵赢,關(guān)鍵是由于很多命令不支持|管道來傳遞參數(shù)欺劳,而日常工作中有有這個必要,所以就有了 xargs 命令铅鲤,例如:
find /sbin -perm +700 |ls -l #這個命令是錯誤的
find /sbin -perm +700 |xargs ls -l #這樣才是正確的
# 將多行文件合成一行輸出
[root@localhost opt]# cat number2.txt
123
456
789
101112
[root@localhost opt]# xargs < number2.txt
123 456 789 101112
# 顯示每行輸出個數(shù)划提,按照源文件的換行符來分隔
101112
[root@localhost opt]# xargs -n 2 < number2.txt
123 456
789 101112
# 分隔 -d
[root@localhost opt]# echo 'a,b,c,d,e,f,g,h,i,j,k,l,m,n' | xargs -d ','
a b c d e f g h i j k l m n
[root@localhost opt]# echo 'a,b,c,d,e,f,g,h,i,j,k,l,m,n' | xargs -d ',' -n 5 # 每行顯示輸出5個
a b c d e
f g h i j
k l m n
# 將搜索到的文件放入指定文件夾中 -i {} 用花括號替代傳輸?shù)臄?shù)據(jù)
[root@localhost opt]# find . -name 'yqs[1-9]*.txt' | xargs -i mv {} ./yqs
# 搜索到的文件放入上一級目錄中 -I 用字符串替代傳輸?shù)臄?shù)據(jù)
[root@localhost yqs]# find . -name '*.txt'| xargs -I files mv files ../
23、進程查看—ps
ps -ef | grep java
ps -aux | grep java
# 查看CPU/內(nèi)存占用率最高的進程 head -11 是因為標(biāo)題也算一行
# +邢享、-號可以調(diào)整排序鹏往,-pcpu 表示降序,+pcpu 表示升序
ps -aux --sort=-pcpu | head -11