一個優(yōu)秀的網(wǎng)站友情鏈接怎么交換
文章目錄
- 基本命令使用
- 查看文本文件內(nèi)容-cat命令
- 分頁查看文本文件-less命令
- 查看CPU信息-lscpu命令
- 查看系統(tǒng)內(nèi)核版本-uname命令
- 查看機(jī)修改主機(jī)名-hostname命令
- 查看IP地址-ifconfig命令
- 創(chuàng)建目錄-mkdir命令
- 創(chuàng)建空文件-touch命令
- 查看文件前幾行-head命令
- 查看文件后幾行-tail命令
- 快速編輯技巧
- 關(guān)機(jī)及重啟
- 別名管理-alias/unalias
- 刪除操作-rm命令
- 刪除空目錄rmdir
- 移動操作-mv
- 復(fù)制操作-cp
- 軟連接
- 硬鏈接
- 軟連接硬鏈接區(qū)別實(shí)驗(yàn)
- 通配符
- 重定向操作
- 管道操作
- find精確查找
- find基本使用
- find多條件使用
- find高級使用
- 總結(jié)
基本命令使用
本節(jié)需要一臺CentOS7.9的虛擬機(jī),將其開機(jī)并使用root用戶登錄即可!
查看文本文件內(nèi)容-cat命令
格式:cat \[選項(xiàng)\] 目標(biāo)文件
查看/etc/passwd文件
[root@localhost ~]# cat /etc/shells
查看/etc/centos-release文件
[root@localhost ~]# cat /etc/centos-release
查看/etc/passwd文件
[root@localhost ~]# cat /etc/passwd
查看/proc/meminfo文件(該文件用于記錄當(dāng)前系統(tǒng)內(nèi)存信息)
[root@localhost ~]# cat /proc/meminfo
MemTotal: 1828200 kB #總內(nèi)存
MemFree: 126980 kB #剩余內(nèi)存
分頁查看文本文件-less命令
格式: less 目標(biāo)文件
-
less查看文件內(nèi)容為交互式查看,在交互式模式中:
-
按/表示搜索關(guān)鍵詞,按n查找下一個、按N查找上一個
-
按空格以頁為單位翻頁瀏覽,按回車以行為單位翻頁瀏覽
-
按q退出交互式界面
-
分頁查看/etc/passwd文件
[root@localhost ~]# less /etc/passwd
查看CPU信息-lscpu命令
格式:lscpu
[root@localhost ~]# lscpu
查看系統(tǒng)內(nèi)核版本-uname命令
格式:uname [選項(xiàng)]
-r
輸出內(nèi)核發(fā)行號
[root@localhost ~]# uname -r
查看機(jī)修改主機(jī)名-hostname命令
格式:hostname [主機(jī)名]
- 該方式修改主機(jī)名為臨時生效
- 提示符中的主機(jī)名只會顯示主機(jī)名第一個小數(shù)點(diǎn)左邊的內(nèi)容(例如a.b.c,只會在提示符中顯示a)
- 修改主機(jī)名后,提示符上不會馬上顯示,關(guān)閉終端重新打開即可
查看主機(jī)名
[root@localhost ~]# hostname
修改主機(jī)名為som.tedu.cn
[root@localhost ~]# hostname som.tedu.cn #修改主機(jī)名,并重新打開終端
[root@som ~]# #提示符已發(fā)生改變
[root@som ~]# hostname #查看完整主機(jī)名
查看IP地址-ifconfig命令
格式:ifconfig [網(wǎng)卡]
[root@som ~]# ifconfig #查看IP地址
[root@som ~]# ifconfig ens160 #指定網(wǎng)卡查看IP地址
創(chuàng)建目錄-mkdir命令
格式:mkdir [選項(xiàng)] [/路徑/]目錄名 ...
-p
:連同父目錄一并創(chuàng)建
創(chuàng)建/opt/som01目錄
[root@som ~]# mkdir /opt/som01
創(chuàng)建/opt/aa/bb/cc/dd
[root@som ~]# ls /opt/ #查看/opt/沒有aa目錄
a.txt som01
[root@som ~]# mkdir /opt/aa/bb/cc/dd #此時直接創(chuàng)建報(bào)錯
mkdir: 無法創(chuàng)建目錄 “/opt/aa/bb/cc/dd”: 沒有那個文件或目錄
[root@som ~]# mkdir -p /opt/aa/bb/cc/dd #加上-p選項(xiàng),成功創(chuàng)建
[root@som ~]# ls -R /opt/aa/ #遞歸查看/opt/aa
創(chuàng)建空文件-touch命令
格式:touch [選項(xiàng)] 目標(biāo)文件 ...
創(chuàng)建空文件/opt/1.txt、/opt/2.txt
[root@som ~]# touch /opt/1.txt
[root@som ~]# touch /opt/2.txt
查看文件前幾行-head命令
格式:head [-n] 行數(shù) 目標(biāo)文件
-n
選項(xiàng)也可以省略,直接寫-行數(shù)- 當(dāng)沒有指定看前幾行時間,默認(rèn)看前10行
查看/etc/passwd文件的前2行
[root@som ~]# head -n 2 /etc/passwd
[root@som ~]# head -2 /etc/passwd
查看/etc/passwd文件的前10行
[root@som ~]# head -n 10 /etc/passwd
[root@som ~]# head -10 /etc/passwd
[root@som ~]# head /etc/passwd
查看文件后幾行-tail命令
格式:tail [-n] 行數(shù) 目標(biāo)文件
-n
選項(xiàng)也可以省略,直接寫-行數(shù)- 當(dāng)沒有指定看前幾行時間,默認(rèn)看后10行
查看/etc/group文件后2行
[root@som ~]# tail -n 2 /etc/group
[root@som ~]# tail -2 /etc/group
查看/etc/group文件后10行
[root@som ~]# tail -n 10 /etc/group
[root@som ~]# tail -10 /etc/group
[root@som ~]# tail /etc/group
快速編輯技巧
- Tab鍵補(bǔ)齊:命令、選項(xiàng)、參數(shù)、文件路徑、軟件名、服務(wù)名…
- 快捷鍵
- Ctrl l:清空整個屏幕
- Ctrl c:廢棄當(dāng)前編輯的命令行操作
- Esc . 或者ALT .:粘貼上一個命令的參數(shù)
關(guān)機(jī)及重啟
重啟:reboot
關(guān)機(jī):poweroff
[root@som ~]# reboot #重新啟動
[root@som ~]# poweroff #關(guān)閉機(jī)器
別名管理-alias/unalias
作用:別名相當(dāng)于生活中的的“外號”,用于將復(fù)雜的命令簡單化
格式:alias [別名='真實(shí)命令']
- 查看當(dāng)前系統(tǒng)已有別名直接alias
- alias定義別名是臨時生效的
- 一般不要把其他命令關(guān)鍵字作為別名(例如將ls作為其他命令的別名),因?yàn)閯e名優(yōu)先級更高
- 取消別名:
unalias 別名
查看當(dāng)前已有別名
[root@som ~]# alias
將hn定義為hostname的別名
[root@som ~]# alias hn='hostname' #定義hn為hostname的別名
[root@som ~]# alias #查看已有別名
[root@som ~]# hn #驗(yàn)證別名是否生效
取消別名hn
[root@som ~]# unalias hn #取消別名hn
[root@som ~]# alias #查看是否取消別名hn
[root@som ~]# hn #查看hn是否生效
刪除操作-rm命令
格式:rm [選項(xiàng)] 參數(shù) ...]
-i
: 提示是否刪除
-r
: 遞歸刪除
-f
: 強(qiáng)制刪除
- rm 默認(rèn)是rm -i的別名
- -f優(yōu)先級大于-i,所以當(dāng)-f和-i同時出現(xiàn)時,優(yōu)先使用-f選項(xiàng)的功能
使用rm命令刪除文件
[root@som ~]# touch /opt/test.txt #創(chuàng)建文件
[root@som ~]# rm /opt/test.txt #直接刪除文件
rm:是否刪除普通空文件 '/opt/test.txt'?y #會有提示(回答y表示刪除,回答n表示不刪除)
[root@som ~]# ls /opt/ #查看/opt/是否還有test.txt[root@som ~]# touch /opt/test2.txt #創(chuàng)建/opt/test2.txt
[root@som ~]# rm -f /opt/test2.txt #強(qiáng)制刪除/opt/test2.txt
[root@som ~]# ls /opt/ #查看/opt/是否還有test2.txt
使用rm命令刪除目錄
[root@som ~]# mkidr -p /opt/aa/bb/cc/dd #創(chuàng)建目錄
[root@som ~]# rm /opt/aa/ #直接刪除報(bào)錯,想要刪除目錄必須加上-r
rm: 無法刪除'/opt/aa/': 是一個目錄
[root@som ~]# ls /opt/ #查看/opt/aa目錄還在[root@som ~]# rm -r /opt/aa/ #使用-r選項(xiàng)遞歸刪除(因?yàn)槭莚m -i的別名所以有提示)
rm:是否進(jìn)入目錄'/opt/aa/'? y
rm:是否進(jìn)入目錄'/opt/aa/bb'? y
rm:是否進(jìn)入目錄'/opt/aa/bb/cc'? y
rm:是否刪除目錄 '/opt/aa/bb/cc/dd'?y
rm:是否刪除目錄 '/opt/aa/bb/cc'?y
rm:是否刪除目錄 '/opt/aa/bb'?y
rm:是否刪除目錄 '/opt/aa/'?y[root@som ~]# mkdir -p /opt/aa/bb/cc/dd #再次創(chuàng)建目錄
[root@som ~]# rm -rf /opt/aa/ #加上-f選項(xiàng)不在有提示
刪除空目錄rmdir
- rmdir命令是Linux系統(tǒng)下一個用于刪除空目錄的命令
[root@som ~]# mkdir /som1 #創(chuàng)建目錄/som1
[root@som ~]# mkdir /som2 #創(chuàng)建目錄/som2
[root@som ~]# touch /som1/test.txt #創(chuàng)建文件/som1/test.txt
[root@som ~]# rmdir /som1/ #刪除非空目錄/som1,失敗
rmdir: 刪除 '/som1/' 失敗: 目錄非空
[root@som ~]# rmdir /som2 #刪除空目錄/som2,成功
移動操作-mv
格式:mv <源數(shù)據(jù)> ... <目標(biāo)位置>
- mv移動數(shù)據(jù)會使源文件消失
- mv可以同時移動多個參數(shù),只有最后一個是目標(biāo)位置
- mv可以再移動的過程中改名
- 目標(biāo)地址不變的移動等于重命名
移動文件操作
[root@som ~]# mkdir /opt/som10 #創(chuàng)建目錄/opt/som10
[root@som ~]# touch /opt/1.txt #創(chuàng)建/opt/1.txt
[root@som ~]# mv /opt/1.txt /opt/som10 #將/opt/1.txt移動至/opt/som10目錄
[root@som ~]# ls /opt/som10/ /opt/ #同時查看/opt/som10和/opt目錄驗(yàn)證
移動過程中改名操作
[root@som ~]# touch /root/linux.txt #創(chuàng)建/root/linux.txt
[root@som ~]# mv /root/linux.txt /opt/cc.txt #將linux.txt移動至/opt下并改名為cc.txt
[root@som ~]# ls /opt/ /root/ #同時查看/opt/和/root/目錄驗(yàn)證
使用mv進(jìn)行"重命名"操作
[root@som ~]# ls /opt/
[root@som ~]# mv /opt/som10 /opt/game #將/opt/som01重命名為/opt/game
[root@som ~]# ls /opt/
復(fù)制操作-cp
格式:cp [選項(xiàng)] <源數(shù)據(jù)> ... <目標(biāo)路徑>
-f
:強(qiáng)制覆蓋
-r
:遞歸拷貝
-p
:保留數(shù)據(jù)原屬性復(fù)制
- cp支持多參數(shù),最后一個為目標(biāo)路徑
- cp不會使源文件消失
- cp操作可以在復(fù)制過程中對目標(biāo)文件改名
- cp默認(rèn)是cp -i的別名,其-i選項(xiàng)優(yōu)先級高于-f,所以當(dāng)-i和-f同時出現(xiàn)時,使用-i的屬性
拷貝文件及目錄
[root@som ~]# cp /etc/passwd /opt/ #將/etc/passwd文件復(fù)制到/opt/目錄
[root@som ~]# cp /etc/shells /opt/ #將/etc/shells文件復(fù)制到/opt/目錄
[root@som ~]# cp /etc/passwd /opt/ps.txt #將/etc/passwd文件復(fù)制到/opt/目錄下改名為ps.txt
[root@som ~]# cp /boot/ /opt/ #將/boot目錄復(fù)制到/opt/目錄下,報(bào)錯
[root@som ~]# cp -r /boot/ /opt/ #將/boot目錄復(fù)制到/opt/目錄下,成功
[root@som ~]# ls /opt #驗(yàn)證是否成功拷貝
cp多參數(shù)拷貝(最后一個為目標(biāo)路徑)
[root@som ~]# cp -r /boot/ /home/ /etc/passwd /etc/shells /mnt/ #將/boot/,/home/,/etc/passwd,/etc/shells復(fù)制到/mnt目錄下
[root@som ~]# ls /mnt/ #驗(yàn)證
cp與.連用(.表示當(dāng)前路徑)
[root@som mnt]# cd /mnt #切換至/mnt
[root@som mnt]# cp /etc/fstab . #將/etc/fstab復(fù)制到當(dāng)前路徑下
[root@som mnt]# ls #驗(yàn)證
cp復(fù)制保持屬性不變
[root@som ~]# ls -ld /home/lisi/
drwx------. 4 lisi lisi 113 2月 24 10:22 /home/lisi/
[root@som ~]# cp -r /home/lisi/ /opt/ #將/home/lisi目錄復(fù)制到/opt下
[root@som ~]# ls -ld /opt/lisi/ #所有者與所屬組發(fā)生了改變
drwx------. 4 root root 113 2月 28 12:33 /opt/lisi/[root@som ~]# rm -rf /opt/lisi/ #刪除/opt/lisi目錄
[root@som ~]# cp -rp /home/lisi/ /opt/ #保留屬性將/home/lisi目錄復(fù)制到/opt下
[root@som ~]# ls -ld /opt/lisi/ #查看目標(biāo)屬性不變
drwx------. 4 lisi lisi 113 2月 24 10:22 /opt/lisi/
軟連接
- 軟連接 --> 原始文檔 --> 文檔數(shù)據(jù)
- 格式:
ln -s 原始文件或目錄 軟連接文件
- 若原始文件或目錄被刪除,連接文件將失效
- 軟連接可存放在不同分區(qū)/文件系統(tǒng)
[root@som ~]# ln -s /etc/passwd /opt/ps.txt #將/opt/ps.txt設(shè)置為/etc/passwd的軟連接
硬鏈接
- 硬鏈接 --> 文檔數(shù)據(jù)
- 格式:
ln 原始文件 硬鏈接文件
- 若原始文件被刪除,鏈接文件仍可用
- 硬鏈接與原始文件必須在同一分區(qū)/文件系統(tǒng)
[root@som ~]# ln /etc/shells /opt/shells #將/opt/shells設(shè)置為/etc/shells的硬鏈接
軟連接硬鏈接區(qū)別實(shí)驗(yàn)
創(chuàng)建素材文件
[root@som ~]# cp /etc/centos-release /opt/a.txt #產(chǎn)生素材文件
[root@som ~]# ln -s /opt/a.txt /opt/b.txt #將/opt/b.txt設(shè)置為/opt/a.txt的軟連接
[root@som ~]# ln /opt/a.txt /opt/c.txt #將/opt/c.txt設(shè)置為/opt/a.txt的硬鏈接
[root@som ~]# rm -rf /opt/a.txt #刪除源文件/opt/a.txt
[root@som ~]# cat /opt/b.txt #無法查看軟連接文件/opt/b.txt
[root@som ~]# cat /opt/c.txt #可以正常查看硬鏈接文件/opt/c.txt
通配符
概念:使用固定的字符對數(shù)據(jù)進(jìn)行模糊匹配
類似生活中:張三、張三豐、張三花,張xx(此時xx可能代表三豐、三花),張x(x可以表示三)
*
:匹配任意字符,包含0個字符
?
:匹配任意單個字符
[0-9]
:匹配0-9任意單個數(shù)字
[a-z]
:匹配任意單個字符
{xx,yy,zz}
:嚴(yán)格匹配大括號內(nèi)的元素
[root@som ~]# ls /dev/tty* #匹配/dev/tty后邊任意字符內(nèi)容
[root@som ~]# ls /dev/tty? #匹配/dev/tty后邊1個字符內(nèi)容
[root@som ~]# ls /dev/tty?? #匹配/dev/tty后邊2個字符內(nèi)容
[root@som ~]# ls /dev/tty[0-9] #匹配/dev/tty后邊1個數(shù)字內(nèi)容
[root@som ~]# ls /dev/tty[0-9][0-9] #匹配/dev/tty后邊2個數(shù)字內(nèi)容 [root@som ~]# touch /opt/{a,b,c}.txt #創(chuàng)建/opt/a.txt,/opt/b.txt,/opt/c.txt
[root@som ~]# ls /opt/[a-z].txt #查看/opt/下一個字符.txt的文件
[root@som ~]# ls -d /m{edia,nt} #嚴(yán)格匹配/media和/mnt
重定向操作
作用:將前方命令輸出內(nèi)容保存到文件
>
:覆蓋重定向
>>
:追加重定向
- 當(dāng)目標(biāo)文件存在時,則使用目標(biāo)文件存儲數(shù)據(jù)
- 當(dāng)目標(biāo)文件不存在時,重定向會創(chuàng)建該目標(biāo)文件并存儲數(shù)據(jù)
補(bǔ)充:echo
指令,echo會將指定內(nèi)容輸出到屏幕上
例如
[root@som ~]# echo hahaxixi #在屏幕輸出hahaxixi
hahaxixi
[root@som ~]# echo "I LOVE YOU" #在屏幕輸出I LOVE YOU
I LOVE YOU
覆蓋重定向
[root@som ~]# cat /opt/hn.txt #查看/opt/hn.txt,沒有此文件
[root@som ~]# hostname > /opt/hn.txt #將hostname輸出結(jié)果寫入/opt/hn.txt文件
[root@som ~]# cat /opt/hn.txt #查看/opt/hn.txt,有文件,有內(nèi)容
[root@som ~]# ifconfig > /opt/hn.txt #將ifconfig輸出結(jié)果寫入/opt/hn.txt文件
追加重定向
[root@som ~]# echo 123 > /opt/a.txt #將123重定向至/opt/a.txt文件
[root@som ~]# cat /opt/a.txt #查看/opt/a.txt文件內(nèi)容
[root@som ~]# echo 456 >> /opt/a.txt #將456追加重定向至/opt/a.txt文件
[root@som ~]# cat /opt/a.txt #查看/opt/a.txt文件內(nèi)容(123沒有被覆蓋)
管道操作
作用:將前方命令輸出結(jié)果最為后方命令參數(shù)
- 管道可以多重使用
[root@som ~]# ls --help | less #將ls --help輸出信息交給less命令作為參數(shù)
[root@som ~]# ifconfig | head -2 #將ifconfig輸出信息交給head -2作為參數(shù)
find精確查找
find基本使用
格式:find 查找目錄 條件
- find可用于在Linux操作系統(tǒng)中精確查找某些資料
- find查找為"地毯式搜索"不會放過任何一個子目錄、隱藏目錄
- 查找過程中如果遇到/proc目錄下的報(bào)錯,屬于正常現(xiàn)象,因?yàn)?proc不占用磁盤空間,占用的是內(nèi)存空間
- 常用查找條件
-type
按類型查找(f、d、l)-name
按名字查找(可與通配符連用)-iname
按名字查找(可忽略名字大小寫查找、可與通配符連用)-size
按數(shù)據(jù)大小查找(k、M、G)-mtime
按數(shù)據(jù)最近修改時間查找-user
按數(shù)據(jù)所有者查找
按類型查找
[root@som ~]# find /boot/ -type f #在/boot/下遞歸查找文件
[root@som ~]# find /boot/ -type d #在/boot/下遞歸查找目錄
[root@som ~]# find /boot/ -type l #在/boot/下遞歸查找連接文件(快捷方式)
按名字查找
[root@som ~]# find /etc/ -name "passwd" #在/etc/下遞歸查找名為passwd的數(shù)據(jù)
[root@som ~]# find /etc/ -name "*tab" #在/etc/下遞歸查找tab結(jié)尾的數(shù)據(jù)
[root@som ~]# find /etc/ -name "*passwd*" #在/etc/下遞歸查找名字包含passwd的數(shù)據(jù)
忽略名字大小寫查找
[root@som ~]# find /etc/ -iname "PaSSwd" #在/etc/查找名為passwd的數(shù)據(jù)(忽略大小寫)
按大小查找
k
:kb作為單位M
:MB作為單位G
:GB作為單位- 注意:在使用大小進(jìn)行查找時,對于某些目錄可能存在一定的BUG,例如1M需要寫成1024k,當(dāng)然這里需要多測試才會發(fā)現(xiàn)
[root@som ~]# find /boot/ -size +2M #在/boot/查找文件大小大于2M的數(shù)據(jù)
[root@som ~]# ls -lh /boot/grub2/fonts/unicode.pf2 #驗(yàn)證大小
-rw-r--r--. 1 root root 2.5M 12月 19 12:10 /boot/grub2/fonts/unicode.pf
[root@som ~]# find /boot/ -size -1M #在/boot/查找文件大小小于1M的數(shù)據(jù)(出現(xiàn)BUG)
[root@som ~]# find /boot/ -size -1024k #在/boot/查找文件大小小于1024k的數(shù)據(jù)(小于1M)
按修改時間
[root@som ~]# find /etc/ -mtime +10 #查找10天前修改的數(shù)據(jù)
[root@som ~]# find /etc/ -mtime -10 #查找近10天修改的數(shù)據(jù)
查找按所有者查找
[root@som ~]# ls -ld /home/lisi/ #查看“文檔”屬性,查看所有者
drwx------. 4 lisi lisi 113 2月 24 10:22 /home/lisi/
[root@som ~]# find /home/ -user lisi #從/home中查找所有者為lisi的數(shù)據(jù)
[root@som ~]# find /etc/ -user root #從/etc中查找所有者為root的數(shù)據(jù)
find多條件使用
-a
:and邏輯與,多個條件同時成立才滿足條件(默認(rèn)為邏輯與)
-o
:or邏輯或,多個條件成立其中一個即可
創(chuàng)建實(shí)驗(yàn)素材
[root@som ~]# touch /root/som01.txt #創(chuàng)建/root/som01.txt
[root@som ~]# touch /root/som02.txt #創(chuàng)建/root/som02.txt
[root@som ~]# mkdir /root/som03 #創(chuàng)建/root/som03目錄
邏輯與案例-a,在/root/下查找som開頭的文件
[root@som ~]# find /root/ -name "som*" -a -type f
[root@som ~]# find /root/ -name "som*" -type f #-a也可以省略不寫,默認(rèn)就是-a
邏輯或案例-o,在/root/下查找som開頭的資料,或者是文件即可
[root@som ~]# find /root/ -name "som*" -o -type f
find高級使用
- 可用于處理找到的文件
- 格式:
find [范圍] [條件] -exec 處理命令 {} \;
- {}表示查找到的每一個結(jié)果
- \;表示操作結(jié)束
[root@som ~]# find /boot/ -size +10M -exec ls {} \; #ls查看/boot下大于10M的數(shù)據(jù)
[root@som ~]# find /boot/ -size +10M | xargs ls; #同上(xargs傳遞|前查找到的每個數(shù)據(jù))
將/boot下vm開頭的數(shù)據(jù)拷貝到/root/findfiles中
[root@som ~]# mkdir /root/findfiles #新建文件夾/root/findfiles
[root@som ~]# find /boot/ -name "vm*" -exec cp -r {} /root/findfiles/ \; #查找并拷貝
總結(jié)
- 掌握Linux常用命令
- hostname、ifconfig、mkdir、touch、head、tail、alias
- rm、rmdir、mv、cp、ln
- 掌握管道、重定向
- 掌握find命令使用
- 掌握find命令高級使用