文件和目錄管理,剛開(kāi)始學(xué)這塊的時(shí)候感覺(jué)內(nèi)容很多很雜,但是學(xué)完進(jìn)行總結(jié)后,發(fā)現(xiàn)其實(shí)很有條理的而且沒(méi)什么難度,只是熟練掌握這些常用的命令就行了。至于Vim編輯器,不得不說(shuō),用了這個(gè)編輯器之后,感覺(jué)windows的notepad很沒(méi)有技術(shù)含量了。
先簡(jiǎn)單總結(jié)一下文件和目錄常用到的命令,簡(jiǎn)單的用法就略過(guò)。
文件操作命令:touch、file、which、find、cp、rm、mv、ln
文件內(nèi)容操作命令:cat、more、less,head、tail,wc、grep
目錄操作命令:pwd、cd、ls、mkdir、du
歸檔及壓縮命令:gzip、bzip2、tar
[jzhou@localhost ~]$ pwd ==>顯示當(dāng)前目錄
/home/jzhou
[jzhou@localhost ~]$ mkdir dirtest ==>創(chuàng)建一個(gè)目錄
[jzhou@localhost ~]$ cd dirtest ==>進(jìn)入這個(gè)目錄
[jzhou@localhost dirtest]$ touch testfile ==>創(chuàng)建一個(gè)文件
[jzhou@localhost dirtest]$ mkdir dirtest1 ==>創(chuàng)建子目錄
[jzhou@localhost dirtest]$ ls ==>列出當(dāng)前目錄內(nèi)容
dirtest1 testfile
[jzhou@localhost dirtest]$ echo hello linux >>testfile ==>追加內(nèi)容到文件
[jzhou@localhost dirtest]$ cat testfile ==>顯示文件內(nèi)容
hello linux
[jzhou@localhost dirtest]$ file testfile ==>查看文件類(lèi)型
testfile: ASCII text
[jzhou@localhost dirtest]$ du -sh testfile ==>顯示文件所占空間
8.0K testfile
[jzhou@localhost dirtest]$ wc testfile ==>統(tǒng)計(jì)文件行數(shù)、字?jǐn)?shù)、字符數(shù)
2 12 testfile
[jzhou@localhost dirtest]$ echo haha,I love Linux >> testfile ==>追加內(nèi)容
[jzhou@localhost dirtest]$ echo no,no ,I hate C plus plus >> testfile
[jzhou@localhost dirtest]$ echo OK,the end >> testfile
[jzhou@localhost dirtest]$ cat testfile ==>查看內(nèi)容
hello linux
haha,I love Linux
no,no ,I hate C plus plus
OK,the end
[jzhou@localhost dirtest]$ head -2 testfile ==>查看文件前兩行內(nèi)容
hello linux
haha,I love Linux
[jzhou@localhost dirtest]$ tail -2 testfile ==>查看文件最后兩行內(nèi)容
no,no ,I hate C plus plus
OK,the end
[jzhou@localhost dirtest]$ cat testfile | grep "Linux" 查找特定關(guān)鍵字
haha,I love Linux
[jzhou@localhost dirtest]$
以上只是展示部分命令的簡(jiǎn)單用法,很多選項(xiàng)沒(méi)有加入,head和tail命令默認(rèn)是顯示前10行和后10行記錄,du是查看目錄或文件所占的空間,通常比實(shí)際大小要大,且通常為4的整數(shù)倍。
more和less命令也是查看文件內(nèi)容的方法,不過(guò)less已經(jīng)漸漸取代more了,因?yàn)閙ore的所有功能less都具有,而且less可以向上翻頁(yè)查看,more則不可以,cat是直接將文件內(nèi)容一屏顯示出來(lái),不管多長(zhǎng),所有如果文件很長(zhǎng)時(shí),則使用less命令,同樣,也是按q鍵退出。
[jzhou@localhost dirtest]$ cd dirtest1 ==>進(jìn)入到剛才建的子目錄
[jzhou@localhost dirtest1]$ touch testfile1 ==>在子目錄中創(chuàng)建一個(gè)新文件
[jzhou@localhost dirtest1]$ echo haha >> testfile1
[jzhou@localhost dirtest1]$ cd .. ==>返回到上一目錄
[jzhou@localhost dirtest]$ ls
dirtest1 testfile
[jzhou@localhost dirtest]$ cp testfile ./dirtest1/ ==>把文件testfile復(fù)制到子目錄dirtest1下
[jzhou@localhost dirtest]$ cd dirtest1/ ==>進(jìn)入到子目錄
[jzhou@localhost dirtest1]$ ls ==>查看子目錄下多了一個(gè)剛才復(fù)制過(guò)來(lái)的文件
testfile testfile1
[jzhou@localhost dirtest1]$ cd ..
[jzhou@localhost dirtest]$ ls
dirtest1 testfile
[jzhou@localhost dirtest]$ rm -f testfile ==>強(qiáng)制刪除dirtest目錄下的testfile文件
[jzhou@localhost dirtest]$ ls ==>testfile文件已經(jīng)被刪除
dirtest1
[jzhou@localhost dirtest]$ cd ./dirtest1/ ==>進(jìn)入到子目錄
[jzhou@localhost dirtest1]$ mv testfile ./testfile ==>這里我嘗試移動(dòng)的目標(biāo)目錄錯(cuò)誤
testfile testfile1
[jzhou@localhost dirtest1]$ pwd ==>所以我要查看當(dāng)前目錄,以使用絕對(duì)路徑
/home/jzhou/dirtest/dirtest1
[jzhou@localhost dirtest1]$ mv testfile /home/jzhou/dirtest/==>將testfile文件移到dirtest目錄下
[jzhou@localhost dirtest1]$ cd ..
[jzhou@localhost dirtest]$ ls ==>很好,testfile文件已經(jīng)被移動(dòng)過(guò)來(lái)了
dirtest1 testfile
[jzhou@localhost dirtest]$ ln -s testfile linkfile ==>建立軟鏈接
[jzhou@localhost dirtest]$ ls -l ==>注意下面軟鏈接文件的顯示方式
總計(jì) 20
drwxrwxr-x 2 jzhou jzhou 4096 03-05 22:43 dirtest1
lrwxrwxrwx 1 jzhou jzhou 8 03-05 22:45 linkfile -> testfile
-rw-rw-r-- 1 jzhou jzhou 67 03-05 22:40 testfile
[jzhou@localhost dirtest]$
rm 文件作用在文件與目錄的唯一區(qū)別就是是否帶有-r選項(xiàng),因?yàn)閯h除目錄時(shí),目錄里面可能嵌套有文件和目錄,所以必須要有-r選項(xiàng),cp和rm的格式都是: cp/rm 原文件 目標(biāo)文件(注意這里的路徑問(wèn)題)
ln鏈接文件:分為軟鏈接和硬鏈接,軟鏈接又稱(chēng)符號(hào)鏈接,即帶有-s選項(xiàng)。軟鏈接即相當(dāng)于windows下的快捷方式,若原文件損壞,則快捷方式無(wú)效,而硬鏈接則相當(dāng)于對(duì)原文件的一個(gè)拷貝,通常情況,硬鏈接用的很少。所以建立鏈接文件時(shí),通常加-s選項(xiàng)即建立軟鏈接。鏈接文件的文件類(lèi)型位為:l,后續(xù)筆記文件權(quán)限中會(huì)介紹這個(gè)位。
另外要注意的是:不能為目錄建立硬鏈接文件,而且硬鏈接與原始文件必須位于同一分區(qū)(文件系統(tǒng))中。
[jzhou@localhost ~]$ cd dirtest/
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile
[jzhou@localhost dirtest]$ tar cf test.tar dirtest1 testfile ==>歸檔目錄和文件
[jzhou@localhost dirtest]$ ls ==>多了一個(gè)剛新建的歸檔文件test.tar
dirtest1 linkfile testfile test.tar
[jzhou@localhost dirtest]$ rm -rf dirtest1 testfile ==>刪除原文件,方便后面確認(rèn)文件是否歸檔
[jzhou@localhost dirtest]$ ls
linkfile test.tar
[jzhou@localhost dirtest]$ pwd ==>查看一下當(dāng)前目錄,后面要解歸檔在這個(gè)目錄
/home/jzhou/dirtest
[jzhou@localhost dirtest]$ tar xf test.tar -C /home/jzhou/dirtest/ ==>解開(kāi)歸檔,testfile文件釋放了
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile test.tar
[jzhou@localhost dirtest]$ rm -f test.tar ==>刪除這個(gè)歸檔包,助于后面測(cè)試
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile
[jzhou@localhost dirtest]$ gzip -9 testfile ==>將這個(gè)文件以gz格式壓縮
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile.gz ==>這個(gè)就是壓縮后自動(dòng)生成的文件名
[jzhou@localhost dirtest]$ gzip -d testfile.gz ==>將剛壓縮的包解開(kāi)
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile ==>看,testfile被解壓出來(lái)了
[jzhou@localhost dirtest]$ bzip2 -9 testfile ==>將這個(gè)文件以bz2格式壓縮
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile.bz2 ==>看,這個(gè)bz2就是剛生成的
[jzhou@localhost dirtest]$ bzip2 -d testfile.bz2 ==>解開(kāi)這個(gè)壓縮包
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile ==>看,它被釋放出來(lái)了
[jzhou@localhost dirtest]$ tar jcf test.tar.bz2 testfile ==>這個(gè)是bz2格式歸檔壓縮,注意選項(xiàng)是j
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile test.tar.bz2
[jzhou@localhost dirtest]$ rm -r testfile
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile test.tar.bz2
[jzhou@localhost dirtest]$ tar jxf test.tar.bz2 -C /home/jzhou/dirtest/ ==>解開(kāi)歸檔壓縮
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile test.tar.bz2
[jzhou@localhost dirtest]$ tar zcf test.tar.gz dirtest1 ==>這個(gè)是gz格式歸檔壓縮,注意選項(xiàng)是z
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest]$ rm -rf dirtest1
[jzhou@localhost dirtest]$ ls
linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest]$ tar zxf test.tar.gz -C /home/jzhou/dirtest/ ==>解開(kāi)歸檔壓縮
[jzhou@localhost dirtest]$ ls
dirtest1 linkfile testfile test.tar.bz2 test.tar.gz
[jzhou@localhost dirtest]$
上面命令顯示格式不太友好,因?yàn)樵谡鎸?shí)環(huán)境下,若刪除原文件,軟鏈接文件會(huì)處于不可用狀態(tài)背景會(huì)變成紅底。不過(guò)這個(gè)不影響理解呵呵。
注意歸檔只是將文件或者目錄打在一個(gè)包里,并不進(jìn)行壓縮,而gzip和bzip2是進(jìn)行壓縮,上述最后幾行命令是將二者結(jié)合起來(lái)使用的,即先歸檔后壓縮。
tar和gzip bzip2的命令格式如下:
tar [選項(xiàng)]... 歸檔文件名 源文件或目錄 ==》制作歸檔文件/p>
p>tar [選項(xiàng)]... 歸檔文件名 [-C 目標(biāo)目錄](méi) ==》解開(kāi)歸檔文件/p>
p>gzip/bzip2 [-9] 文件名或目錄 ==》制作壓縮文件/p>
p>gzip/bzip2 -d .gz/.bz2格式的壓縮文件 ==》解開(kāi)壓縮文件
對(duì)于上述命令,只是舉出最簡(jiǎn)單的用法,至于要實(shí)現(xiàn)更強(qiáng)大的功能,使用時(shí)那就要去查每個(gè)命令帶有哪些選項(xiàng),或者直接找man命令幫助,那些選項(xiàng)太多,所以我認(rèn)為只要知道有某個(gè)命令,至于具體用法用到時(shí)再去查而沒(méi)必要記住所有的選項(xiàng)含義。
VIM編輯器的常用快捷編輯方式
文本編輯器可用來(lái)創(chuàng)建或修改文本文件,以及維護(hù)Linux系統(tǒng)中的各種配置文件。首次接觸這個(gè)編輯器時(shí)會(huì)由于不熟練而是影響編輯效率,但是掌握常用快捷鍵后,非常神速。下面只是簡(jiǎn)單介紹下Vim編輯器,至于更深入的用法大家可以網(wǎng)上找找。
Unix和早期的Linux中默認(rèn)使用的文本編輯器是Vi,現(xiàn)在用的都是vi的增強(qiáng)版vim,由于vi用的比較習(xí)慣了,所以現(xiàn)在仍然叫vi,其實(shí)是別名 alias vi='/usr/bin/vim',這個(gè)可以通過(guò)命令which vi看到。
Vim編輯器有3種工作模式:命令模式、輸入模式、末行模式,在有的資料中可能說(shuō)有2中工作模式,不將‘末行模式’列在其中,這個(gè)不重要,總之前兩種模式確實(shí)很重要;因?yàn)樵谶@兩種模式下可以做很多事。各種模式之間的切換如下圖:
![](/d/20211018/8c612b0995d007fb89ee87c875e855b0.gif)
這幾個(gè)模式的轉(zhuǎn)換也要非常熟練。
命令模式中的基本操作:
(1)顯示行號(hào)::set nu 取消行號(hào): :set nonu
(2)行間快速跳轉(zhuǎn):#G:跳轉(zhuǎn)到文件中的第#行;G:跳轉(zhuǎn)到文件的末尾行;1G或gg:跳轉(zhuǎn)到文件的行首。
(3)行內(nèi)快速跳轉(zhuǎn):Home End
關(guān)于刪除復(fù)制和粘貼:(命令模式下)
刪除
|
x或Del
|
刪除光標(biāo)處的單個(gè)字符
|
dd
|
刪除當(dāng)前光標(biāo)所在行
|
#dd
|
刪除從光標(biāo)處開(kāi)始的#行內(nèi)容
|
d^
|
刪除當(dāng)前光標(biāo)之前到行首的所有字符
|
d$
|
刪除當(dāng)前光標(biāo)處到行尾的所有字符
|
復(fù)制
|
yy
|
復(fù)制當(dāng)前行整行的內(nèi)容到剪貼板
|
#yy
|
復(fù)制從光標(biāo)處開(kāi)始的#行內(nèi)容
|
粘貼
|
p
|
將緩沖區(qū)中的內(nèi)容粘貼到光標(biāo)位置處之后
|
P
|
粘貼到光標(biāo)位置處之前
|
在文件內(nèi)容中查找:
操作鍵
|
功能
|
/word
|
從上而下在文件中查找字符串“word”
|
?word
|
從下而上在文件中查找字符串“word”
|
n
|
定位下一個(gè)匹配的被查找字符串
|
N
|
定位上一個(gè)匹配的被查找字符串
|
撤銷(xiāo)編輯及保存退出:
u
|
按一次取消最近的一次操作
多次重復(fù)按u鍵,恢復(fù)已進(jìn)行的多步操作
|
U
|
用于取消對(duì)當(dāng)前行所做的所有編輯
|
ZZ
|
保存當(dāng)前的文件內(nèi)容并退出vi編輯器
|
保存文件及退出vi編輯器:(末行模式下)
保存文件
|
|
|
:w /root/newfile
|
另存為其它文件
|
退出vi
|
:q
|
未修改退出
|
:q!
|
放棄對(duì)文件內(nèi)容的修改,并退出vi
|
保存文件退出vi
|
:wq
|
|
文件內(nèi)容替換:(末行模式下)
:s /old/new
|
將當(dāng)前行中查找到的第一個(gè)字符“old” 串替換為“new”
|
:s /old/new/g
|
將當(dāng)前行中查找到的所有字符串“old” 替換為“new”
|
:#,# s/old/new/g
|
在行號(hào)“#,#”范圍內(nèi)替換所有的字符串“old”為“new”
|
:% s/old/new/g
|
在整個(gè)文件范圍內(nèi)替換所有的字符串“old”為“new”
|
:s /old/new/c
|
在替換命令末尾加入c命令,將對(duì)每個(gè)替換動(dòng)作提示用戶(hù)進(jìn)行確認(rèn)
|
要想熟練操作Vim編輯器,首先得掌握這些快捷鍵,這些都是最基本的。
如有錯(cuò)誤,歡迎指正,3Q!