歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux操作系統學習筆記文件/目錄/VIM(1)

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

日期:2017/2/25 10:32:48   编辑:Linux教程

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

 文件和目錄管理,剛開始學這塊的時候感覺內容很多很雜,但是學完進行總結後,發現其實很有條理的而且沒什麼難度,只是熟練掌握這些常用的命令就行了。至於Vim編輯器,不得不說,用了這個編輯器之後,感覺Windows的notepad很沒有技術含量了。

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

文件操作命令:touch、file、which、find、cp、rm、mv、ln

文件內容操作命令:cat、more、less,head、tail,wc、grep

目錄操作命令:pwd、cd、ls、mkdir、du

歸檔及壓縮命令:gzip、bzip2、tar

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

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

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

[linuxidc@localhost dirtest]$ cd dirtest1    ==>進入到剛才建的子目錄
[linuxidc@localhost dirtest1]$ touch testfile1   ==>在子目錄中創建一個新文件
[linuxidc@localhost dirtest1]$ echo haha >> testfile1
[linuxidc@localhost dirtest1]$ cd .. ==>返回到上一目錄
[linuxidc@localhost dirtest]$ ls 
dirtest1  testfile
[linuxidc@localhost dirtest]$ cp testfile ./dirtest1/ ==>把文件testfile復制到子目錄dirtest1下
[linuxidc@localhost dirtest]$ cd dirtest1/   ==>進入到子目錄
[linuxidc@localhost dirtest1]$ ls   ==>查看子目錄下多了一個剛才復制過來的文件
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文件已經被刪除
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文件已經被移動過來了
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 文件作用在文件與目錄的唯一區別就是是否帶有-r選項,因為刪除目錄時,目錄裡面可能嵌套有文件和目錄,所以必須要有-r選項,cp和rm的格式都是: cp/rm 原文件 目標文件(注意這裡的路徑問題)

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

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

[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]$ 

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

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

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

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

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

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

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

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

Copyright © Linux教程網 All Rights Reserved