华南俳烁实业有限公司

考試首頁 | 考試用書 | 培訓課程 | 模擬考場 | 考試論壇  
  當前位置:操作系統(tǒng) > Linux > 文章內(nèi)容
  

Linux操作系統(tǒng)學習筆記文件/目錄/VIM(1)

 [ 2016年6月6日 ] 【

文件和目錄管理 及 VI編輯器的使用

    文件和目錄管理,剛開始學這塊的時候感覺內(nèi)容很多很雜,但是學完進行總結(jié)后,發(fā)現(xiàn)其實很有條理的而且沒什么難度,只是熟練掌握這些常用的命令就行了。至于Vim編輯器,不得不說,用了這個編輯器之后,感覺Windows的notepad很沒有技術(shù)含量了。

  先簡單總結(jié)一下文件和目錄常用到的命令,簡單的用法就略過。

     文件操作命令: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

[linuxidc@localhost ~]$ pwd  ==>顯示當前目錄
/home/linuxidc
[jzhou@localhost ~]$ mkdir dirtest   ==>創(chuàng)建一個目錄
[linuxidc@localhost ~]$ cd dirtest      ==>進入這個目錄
[linuxidc@localhost dirtest]$ touch testfile    ==>創(chuàng)建一個文件
[linuxidc@localhost dirtest]$ mkdir dirtest1    ==>創(chuàng)建子目錄
[linuxidc@localhost dirtest]$ ls    ==>列出當前目錄內(nèi)容
dirtest1  testfile
[linuxidc@localhost dirtest]$ echo hello linux >>testfile  ==>追加內(nèi)容到文件
[linuxidc@localhost dirtest]$ cat testfile    ==>顯示文件內(nèi)容
hello linux
[linuxidc@localhost dirtest]$ file testfile  ==>查看文件類型
testfile: ASCII text
[linuxidc@localhost dirtest]$ du -sh testfile  ==>顯示文件所占空間
8.0K    testfile
[linuxidc@localhost dirtest]$ wc testfile  ==>統(tǒng)計文件行數(shù)、字數(shù)、字符數(shù)
 1  2 12 testfile
[linuxidc@localhost dirtest]$ echo haha,I love Linux >> testfile   ==>追加內(nèi)容
[linuxidc@localhost dirtest]$ echo no,no ,I hate C plus plus >> testfile
[linuxidc@localhost dirtest]$ echo OK,the end >> testfile
[linuxidc@localhost dirtest]$ cat testfile   ==>查看內(nèi)容
hello linux
haha,I love Linux
no,no ,I hate C plus plus
OK,the end
[linuxidc@localhost dirtest]$ head -2 testfile  ==>查看文件前兩行內(nèi)容
hello linux
haha,I love Linux
[linuxidc@localhost dirtest]$ tail -2 testfile  ==>查看文件最后兩行內(nèi)容
no,no ,I hate C plus plus
OK,the end
[linuxidc@localhost dirtest]$ cat testfile | grep "Linux"  查找特定關(guān)鍵字
haha,I love Linux
[linuxidc@localhost dirtest]$ 

以上只是展示部分命令的簡單用法,很多選項沒有加入,head和tail命令默認是顯示前10行和后10行記錄,du是查看目錄或文件所占的空間,通常比實際大小要大,且通常為4的整數(shù)倍。

more和less命令也是查看文件內(nèi)容的方法,不過less已經(jīng)漸漸取代 more了,因為more的所有功能less都具有,而且less可以向上翻頁查看,more則不可以,cat是直接將文件內(nèi)容一屏顯示出來,不管多長,所有如果文件很長時,則使用less命令,同樣,也是按q鍵退出。

[linuxidc@localhost dirtest]$ cd dirtest1    ==>進入到剛才建的子目錄
[linuxidc@localhost dirtest1]$ touch testfile1   ==>在子目錄中創(chuàng)建一個新文件
[linuxidc@localhost dirtest1]$ echo haha >> testfile1
[linuxidc@localhost dirtest1]$ cd .. ==>返回到上一目錄
[linuxidc@localhost dirtest]$ ls 
dirtest1  testfile
[linuxidc@localhost dirtest]$ cp testfile ./dirtest1/ ==>把文件testfile復(fù)制到子目錄dirtest1下
[linuxidc@localhost dirtest]$ cd dirtest1/   ==>進入到子目錄
[linuxidc@localhost dirtest1]$ ls   ==>查看子目錄下多了一個剛才復(fù)制過來的文件
testfile  testfile1
[linuxidc@localhost dirtest1]$ cd ..
[linuxidc@localhost dirtest]$ ls
dirtest1  testfile
[linuxidc@localhost dirtest]$ rm -f testfile ==>強制刪除dirtest目錄下的testfile文件
[linuxidc@localhost dirtest]$ ls  ==>testfile文件已經(jīng)被刪除
dirtest1
[linuxidc@localhost dirtest]$ cd ./dirtest1/   ==>進入到子目錄
[linuxidc@localhost dirtest1]$ mv testfile ./testfile  ==>這里我嘗試移動的目標目錄錯誤
testfile   testfile1  
[linuxidc@localhost dirtest1]$ pwd   ==>所以我要查看當前目錄,以使用絕對路徑
/home/linuxidc/dirtest/dirtest1
[linuxidc@localhost dirtest1]$ mv testfile /home/linuxidc/dirtest/==>將testfile文件移到dirtest目錄下
[linuxidc@localhost dirtest1]$ cd ..
[linuxidc@localhost dirtest]$ ls   ==>很好,testfile文件已經(jīng)被移動過來了
dirtest1  testfile
[linuxidc@localhost dirtest]$ ln -s testfile linkfile  ==>建立軟鏈接
[linuxidc@localhost dirtest]$ ls -l   ==>注意下面軟鏈接文件的顯示方式
總計 20
drwxrwxr-x 2 linuxidc jzhou 4096 03-05 22:43 dirtest1
lrwxrwxrwx 1 linuxidc jzhou    8 03-05 22:45 linkfile -> testfile
-rw-rw-r-- 1 linuxidc jzhou   67 03-05 22:40 testfile
[linuxidc@localhost dirtest]$ 

rm 文件作用在文件與目錄的唯一區(qū)別就是是否帶有-r選項,因為刪除目錄時,目錄里面可能嵌套有文件和目錄,所以必須要有-r選項,cp和rm的格式都是: cp/rm  原文件   目標文件(注意這里的路徑問題)

ln鏈接文件:分為軟鏈接和硬鏈接,軟鏈接又稱符號鏈接,即帶有-s選項。軟鏈接即相當于windows下的快捷方式,若原文件損壞,則快捷方式無效,而硬鏈接則相當于對原文件的一個拷貝,通常情況,硬鏈接用的很少。所以建立鏈接文件時,通常加-s選項即建立軟鏈接。鏈接文件的文件類型位為:l,后續(xù)筆記文件權(quán)限中會介紹這個位。

  另外要注意的是:不能為目錄建立硬鏈接文件,而且硬鏈接與原始文件必須位于同一分區(qū)(文件系統(tǒng))中。

[linuxidc@localhost ~]$ cd dirtest/
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile
[linuxidc@localhost dirtest]$ tar cf test.tar dirtest1 testfile  ==>歸檔目錄和文件
[linuxidc@localhost dirtest]$ ls  ==>多了一個剛新建的歸檔文件test.tar
dirtest1  linkfile  testfile  test.tar
[linuxidc@localhost dirtest]$ rm -rf dirtest1 testfile   ==>刪除原文件,方便后面確認文件是否歸檔
[linuxidc@localhost dirtest]$ ls
linkfile  test.tar
[linuxidc@localhost dirtest]$ pwd  ==>查看一下當前目錄,后面要解歸檔在這個目錄
/home/linuxidc/dirtest
[linuxidc@localhost dirtest]$ tar xf test.tar -C /home/linuxidc/dirtest/   ==>解開歸檔,testfile文件釋放了
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  test.tar
[linuxidc@localhost dirtest]$ rm -f test.tar  ==>刪除這個歸檔包,助于后面測試
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile
[linuxidc@localhost dirtest]$ gzip -9 testfile   ==>將這個文件以gz格式壓縮
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile.gz ==>這個就是壓縮后自動生成的文件名
[linuxidc@localhost dirtest]$ gzip -d testfile.gz   ==>將剛壓縮的包解開
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile    ==>看,testfile被解壓出來了
[linuxidc@localhost dirtest]$ bzip2 -9 testfile    ==>將這個文件以bz2格式壓縮
[linuxidc@localhost dirtest]$ ls 
dirtest1  linkfile  testfile.bz2  ==>看,這個bz2就是剛生成的
[linuxidc@localhost dirtest]$ bzip2 -d testfile.bz2   ==>解開這個壓縮包
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  ==>看,它被釋放出來了
[linuxidc@localhost dirtest]$ tar jcf test.tar.bz2 testfile   ==>這個是bz2格式歸檔壓縮,注意選項是j
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  test.tar.bz2
[linuxidc@localhost dirtest]$ rm -r testfile
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  test.tar.bz2
[linuxidc@localhost dirtest]$ tar jxf test.tar.bz2 -C /home/linuxidc/dirtest/  ==>解開歸檔壓縮
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  test.tar.bz2
[linuxidc@localhost dirtest]$ tar zcf test.tar.gz dirtest1   ==>這個是gz格式歸檔壓縮,注意選項是z
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  test.tar.bz2  test.tar.gz
[linuxidc@localhost dirtest]$ rm -rf dirtest1
[linuxidc@localhost dirtest]$ ls
linkfile  testfile  test.tar.bz2  test.tar.gz
[linuxidc@localhost dirtest]$ tar zxf test.tar.gz -C /home/linuxidc/dirtest/   ==>解開歸檔壓縮
[linuxidc@localhost dirtest]$ ls
dirtest1  linkfile  testfile  test.tar.bz2  test.tar.gz
[linuxidc@localhost dirtest]$ 

上面命令顯示格式不太友好,因為在真實環(huán)境下,若刪除原文件,軟鏈接文件會處于不可用狀態(tài)背景會變成紅底。不過這個不影響理解呵呵。

  注意歸檔只是將文件或者目錄打在一個包里,并不進行壓縮,而gzip和bzip2是進行壓縮,上述最后幾行命令是將二者結(jié)合起來使用的,即先歸檔后壓縮。

  tar和gzip  bzip2的命令格式如下: 

                 tar  [選項]...  歸檔文件名  源文件或目錄    ==》制作歸檔文件

              tar  [選項]...  歸檔文件名  [-C 目標目錄]   ==》解開歸檔文件

              gzip/bzip2 [-9] 文件名或目錄                 ==》制作壓縮文件

              gzip/bzip2  -d .gz/.bz2格式的壓縮文件    ==》解開壓縮文件

     對于上述命令,只是舉出最簡單的用法,至于要實現(xiàn)更強大的功能,使用時那就要去查每個命令帶有哪些選項,或者直接找man命令幫助,那些選項太多,所以我認為只要知道有某個命令,至于具體用法用到時再去查而沒必要記住所有的選項含義。

本文糾錯】【告訴好友】【打印此文】【返回頂部
將考試網(wǎng)添加到收藏夾 | 每次上網(wǎng)自動訪問考試網(wǎng) | 復(fù)制本頁地址,傳給QQ/MSN上的好友 | 申請鏈接 | 意見留言 TOP
關(guān)于本站  網(wǎng)站聲明  廣告服務(wù)  聯(lián)系方式  站內(nèi)導航  考試論壇
Copyright © 2007-2013 中華考試網(wǎng)(Examw.com) All Rights Reserved
大余县| 大同市| 台南市| 招远市| 舞阳县| 信宜市| 霍林郭勒市| 湖州市| 鲜城| 恩施市| 饶阳县| 清远市| 旬阳县| 宣城市| 苍溪县| 专栏| 寻甸| 锡林郭勒盟| 宁陵县| 绵阳市| 玛曲县| 务川| 左云县| 柳林县| 焉耆| 逊克县| 阿巴嘎旗| 松原市| 大洼县| 三都| 思茅市| 滕州市| 吴忠市| 清丰县| 永宁县| 会昌县| 合江县| 葫芦岛市| 新兴县| 建水县| 松潘县|