Spark修煉之道(基礎篇)——Linux大資料開發基礎:第二節:Linux檔案系統、目錄(一)
本節主要內容
- 如何獲取幫助文件
- Linux檔案系統簡介
- 目錄操作
- 訪問許可權
1. 如何獲取幫助文件
在實際工作過程當中,經常會忘記命令的使用方式,例如ls命令後面可以跟哪些引數,此時可以使用man命令來檢視其使用方式,例如
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//man命令獲取命令幫助手冊</span> xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:~$ man <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
可以使用鍵盤上的 來顯示下一行或上一行命令,也可以使用 進行上一頁或下一頁(屏)命令的檢視,另外 空格鍵也可以用來顯示下一屏的命令。想退出命令檢視,直接按q鍵退出即可,也可以h鍵顯示less命令列表(man命令通過less命令輸出結果)
2. Linux檔案系統簡介
(一) 檔案和目錄
本節從使用者的角度來介紹Linux檔案系統,Linux根據檔案形式將檔案分為目錄和普通檔案,如下圖:
目錄或檔案的名稱長度不超過255個字元,檔案或目錄名可由下列字元構成:
- Uppercase letters (A–Z)
- Lowercase letters (a–z)
- Numbers (0–9)
- Underscore ( _ )
- Period(.)
- Comma(,)
檔案或目錄名區分大小寫,屬於不同的檔案或目錄
(二) 副檔名與不可見檔名
與Window作業系統有很大不同的是,Linux檔案對副檔名沒有強制要求,例如假設編寫了一個c語言原始檔,你可以將其命名為complier.c,也可以是其它如complier、complier.ccc等檔名,但不推薦這麼做,因為如果能將副檔名與特定的檔案進行關聯的話,有利於理解檔案內容,目前約定成俗的linux副檔名如下表:
帶副檔名的檔名 | 副檔名的含義 |
---|---|
max.c | C語言原始檔 |
max.o | 編碼後的目的碼檔案 |
max | max.c對應的可執行檔案 |
memo.txt | 文字檔案 |
memo.pdf | pdf檔案,必須在GUI介面上使用xpdf或kpdf才能檢視 |
memo.ps | PostScript檔案,必須在GUI介面上使用ghostscript或kpdf才能檢視 |
memo.z | 經壓縮程式壓縮後的檔案,可使用uncompress或gunzip解壓 |
memo.gz | 經gzip壓縮程式壓縮後的檔案,可使用gunzip解壓 |
memo.tar.gz或memo.tgz | 經gzip壓縮後的tar歸檔檔案,可使用gunzip解壓 |
memo.bz2 | 經bzip2壓縮後的檔案,可使用bunzip2解壓 |
memo.html | html檔案,使用GUI環境的firefox檢視 |
memo.jpg等 | 影象檔案,使用GUI環境的照片檢視器開啟 |
在前一講中我們看到,linux中還存在大量的隱藏檔案,採用ls -a 命令可以顯示,想定義隱藏檔案,只要檔名或目錄以.開始即可
(三) 絕對路徑與相對路徑
在Linux中絕對路徑與相對路徑是一個很重要的概念,下圖給出了什麼是絕對路徑
所有以根目錄”/”作為開始的都是絕對路徑,其它的均為相對路徑
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//絕對路徑訪問</span> xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:~/Public$ cd /home/ xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home$ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> xtwy <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//相對路徑訪問</span> xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home$ cd xtwy/ </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
3. 目錄操作
(一) 建立目錄 mkdir
為演示方便,使用下列目錄結構進行演示:
1 絕對路徑建立方式
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用絕對路徑建立</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir /home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span> xtwy root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
2 相對路徑建立方式
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//使用相對路徑進行建立</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/names root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/temp root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# mkdir <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span>/literature root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home# cd <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span># mkdir demo root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">max</span># <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> demo literature names temp</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
有時不想層層目錄建立,此時可以在mkdir 後面加上引數 -p(parents),將父子目錄一起建立
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># mkdir -p literature/promo</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span> demo literature names temp root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd literature/</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/max/literature</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span> promo </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>
(二) 更改目錄 cd
工作目錄與主目錄的區別
使用者每次登入後的預設目錄就是主目錄,與系統會話期間保持不變,主目錄用~表示
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/root</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>cd ~ xtwy<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:~</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$ </span>pwd /home/xtwy </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>
工作目錄又稱當前目錄,cd命令執行完成後的目錄就是工作目錄,它是可以隨意改變的。
<code class="hljs java has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//.表示當前目錄即工作目錄</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//..表示當前目錄的上一級目錄</span> xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:~$ cd . xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:~$ cd .. xtwy<span class="hljs-annotation" style="color: rgb(155, 133, 157); box-sizing: border-box;">@ubuntu</span>:/home$ </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>
(三) 刪除目錄 rmdir
rmdir是remove directory的簡稱,用於刪除目錄,它先刪除目錄下的所有檔案,然後再刪除該目錄,但當目錄下還有子目錄時,該命令不能執行,需要使用rm命令,例如
<code class="hljs glsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//刪除temp目錄,先刪除目錄下的檔案</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//再刪除temp目錄自身</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rmdir temp/</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rmdir literature/</span> rmdir: failed to remove `literature/': Directory <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">not</span> empty root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># rm -r literature/</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> demo names</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li></ul>
其中rm -r中的r指的是遞迴的刪除目錄及目錄中的檔案,因此它具有很強的破壞力,要謹慎使用。
(四) 移動目錄 mv
<code class="hljs coffeescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>將目錄demo移到<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy/</span>目錄下 root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/max# mv demo/</span> <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy/</span> root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/max# cd /home/xtwy/</span> root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span> demo Documents examples.desktop Pictures Templates Desktop Downloads Music Public Videos root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rmdir demo</span> <span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>原來目錄的demo目錄已經不存在了 root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy# cd /home/max/</span> root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/max<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span> names </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
(五) 複製目錄 cp
前面用mv命令移動目錄,有時候需要對目錄進行拷貝,使用方式如下:
<code class="hljs glsl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//先建立一個演示目錄,用-p,父目錄如果不存在將會被建立</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mkdir -p literature/demo</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//由於literature還包括子目錄,此時拷貝不成功</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cp literature/ /home/xtwy/</span> cp: omitting directory `literature/' <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果包括子目錄的話,則加上-r引數,表示遞迴地拷貝</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cp -r literature/ /home/xtwy/</span> root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd /homt</span> bash: cd: /homt: No such file or directory root@ubuntu:/home/<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">max</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd /home/xtwy/</span> root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> Desktop Downloads literature Pictures Templates Documents examples.desktop Music Public Videos root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd literature/</span> root@ubuntu:/home/xtwy/literature<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> demo </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
4. 檔案操作
(一) 建立檔案
直接通過命令列的方式建立檔案的方式有多種,常用方式如下:
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">//通過echo命令,將輸出的命令重定向到檔案 root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "hello linux" > hello.txt</span> root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> Desktop Downloads hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> Music Public Videos Documents examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span> literature Pictures Templates //touch命令,如何檔案不存在,會建立檔案 root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># touch hell1.txt</span> root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> Desktop Downloads hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> literature Pictures Templates Documents examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span> hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> Music Public Videos </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li></ul>
(二) 顯示檔案內容
cate命令可以顯示檔案內容,它的全稱是catenate,意思是將單詞一個接一個地連線起來
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat hello.txt </span> hello linux </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
cat命令會將檔案中所有的內容全部一次性顯示出現,例如
<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat /etc/profile</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># /etc/profile: system-wide .profile file for the Bourne shell (sh(1))</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -d /etc/profile.d ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> i <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> /etc/profile.d/*.sh; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">do</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ -r <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span> . <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">$i</span> fi done unset i ...... </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>
有時候我們希望能夠分屏檢視檔案內容,此時可以使用less或more分頁程式,less和more的使用方式相差不大,通過空格鍵顯示下一屏資訊,它們之間的差別在於less在檔案末尾會顯示END訊息,而more直接返回shell終端,例如:
less命令
more命令
(三) cp命令複製檔案
<code class="hljs perl has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span> Desktop Downloads hell1.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos //複製檔案 root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cp hell1.txt literature/demo</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd literature/demo</span> //cd -返回上一次執行的工作目錄 root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/home/xtwy</span><span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">/literature/demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd -</span> /home/xtwy </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>
需要注意的是cp命令在複製時,如果目標目錄中已存在該檔案,系統不會給出警告,而是直接覆蓋,因此它可能存在銷燬檔案的風險,為解決這個問題可以使用-i引數讓系統給出警告,例如:
<code class="hljs asciidoc has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@ubuntu:/home/xtwy# cp -i hell1.txt literature/demo cp: overwrite <span class="hljs-smartquote" style="box-sizing: border-box;">`literature/demo/hell1.txt'</span>? </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li></ul>
(三) mv命令移動或重新命名檔案
<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">//在同一目錄時,相當於檔案重新命名,執行完成後hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>不存在 root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mv hell1.txt hell2.txt</span> root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> Desktop Downloads hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> literature Pictures Templates Documents examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span> hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> Music Public Videos //移動hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>到literature/demo root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># mv hell2.txt literature/demo</span> root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd literature/demo/</span> root@ubuntu:/home/xtwy/literature/demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> hell1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> root@ubuntu:/home/xtwy/literature/demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd -</span> /home/xtwy //源目錄hell2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>已不存在 root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span> Desktop Downloads hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span> Music Public Videos Documents examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span> literature Pictures Templates </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
(四)顯示檔案頭部或尾部
顯示檔案頭部內容用head命令,尾部用tail命令,預設顯示行數為10
<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root@ubuntu:/home/xtwy# head ~/.bashrc <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ~/.bashrc: executed by bash(1) for non-login shells.</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># for examples</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># If not running interactively, don't do anything</span> [ -z <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"$PS1"</span> ] && <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># don't put duplicate lines in the history. See bash(1) for more options</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ... or force ignoredups and ignorespace</span> HISTCONTROL=ignoredups:ignorespace root@ubuntu:/home/xtwy# tail ~/.bashrc if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># enable programmable completion features (you don't need to enable</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># this, if it's already enabled in /etc/bash.bashrc and /etc/profile</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># sources /etc/bash.bashrc).</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># . /etc/bash_completion</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#fi</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li></ul>
head及tail的預設行數是可以修改的,例如:
<code class="hljs vala has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//僅顯示前兩行</span> root@ubuntu:/home/xtwy# head -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> ~/.bashrc <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ~/.bashrc: executed by bash(1) for non-login shells.</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)</span> </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li></ul>
tail命令在檢視日誌檔案內容增長時可能經常會使用,例如在hadoop啟動之後,會產生許多日誌,但出現問題時,可以採用tail命令動態地監測日誌檔案內容的增長,檢視問題出在哪個地方。
<code class="hljs ruleslanguage has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始顯示情況</span> root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># tail </span>-f hello.txt hello linux <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//向檔案中追加內容</span> root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># echo </span><span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hello linux linux"</span> >> hello.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//追加後的輸出情況</span> root@ubuntu:/home/xtwy<span class="hljs-array" style="box-sizing: border-box;"># tail </span>-f hello.txt hello linux hello linux linux </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>
(五)其它常見檔案操作命令
下面的命令都不會改變檔案內容
<code class="hljs mel has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# cp hello.txt hello1.txt root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> Desktop Downloads hello1.txt literature Pictures Templates Documents examples.desktop hello.txt Music Public Videos <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//根據檔案內容排序</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sort</span> hello1.txt hello linux hello linux linux <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//逆序輸出</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">sort</span> -r hello1.txt hello linux linux hello linux <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//diff進行內容比較</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff hello1.txt hello.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//向檔案中追加內容</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# echo <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"hello linux linux"</span> >> hello.txt <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//內容比較</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff hello1.txt hello.txt <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>a3 > hello linux linux <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//格式化輸出</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-u引數將檔案分成多塊</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//比較的兩個檔案分別用-、+表示</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//本例中 -表示hello1.txt,+表示hello.txt</span> root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy# diff -u hello1.txt hello.txt --- hello1.txt <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">28</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44.071202558</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0700</span> +++ hello.txt <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">08</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">22</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">17</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">29</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">49.131181281</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0700</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//@@xxx@@用於標識行起始編號、行數</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//-1,2表示 hello1.txt檔案起始編號為1,行數為2</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//+1,3表示 hello.txt檔案起始編號為1,行數為3</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@@</span> -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> +<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@@</span> hello linux hello linux linux +hello linux linux </code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li></ul>
內容來源: http://blog.csdn.net/lovehuangjiaju/article/details/47856591
相關文章
- Linux 基礎-檔案及目錄管理Linux
- Linux系統檔案系統及檔案基礎篇Linux
- 【Linux基礎】Linux目錄Linux
- Linux基礎學習——檔案與目錄管理Linux
- Linux基礎學習——檔案基礎Linux
- [Python]學習基礎篇:檔案和目錄Python
- linux系統基礎Linux
- 【Linux基礎入門】Linux磁碟與檔案系統管理命令!Linux
- Linux支援的檔案系統有哪些?Linux基礎課程Linux
- Linux檔案系統、目錄Linux
- Linux基礎之檔案管理Linux
- 【大資料】Spark RDD基礎大資料Spark
- 零基礎入門前端的修煉之道前端
- 『學了就忘』Linux基礎 — 15、瞭解Linux系統的目錄結構Linux
- Linux作業系統有什麼檔案型別?Linux基礎教程Linux作業系統型別
- Linux基礎命令---mirror獲取ftp目錄LinuxFTP
- 檔案系統基礎
- 大資料學習之Linux基礎大資料Linux
- Linux基礎命令---lp列印檔案Linux
- Linux基礎命令---lpr列印檔案Linux
- Linux作業系統基礎Linux作業系統
- Linux系統基礎學習Linux
- Linux檔案系統目錄結構Linux
- Linux基礎學習系列——Linux檔案目錄操作命令Linux
- 系列教程--Linux基礎--05--Linux 檔案管理Linux
- Linux常見目錄結構有哪些?Linux基礎入門Linux
- Linux系統程式設計基礎Linux程式設計
- Linux系統的目錄及重要檔案Linux
- 【Linux基礎知識】Linux目錄管理相關命令有什麼Linux
- Nginx基礎篇--Linux下操作NginxLinux
- 面試題:Linux 系統基礎提問 (一)面試題Linux
- Linux基礎命令---驗證組檔案grpckLinuxRPC
- Linux 基礎教程 38-檔案下載Linux
- Linux基礎命令---htpasswd建立密碼檔案Linux密碼
- Linux基礎命令---mget獲取ftp檔案LinuxFTP
- Linux基礎命令---get獲取ftp檔案LinuxFTP
- Linux基礎命令---mput上傳ftp檔案LinuxFTP
- Linux基礎之瀏覽和建立檔案Linux
- linux基礎Linux