Linux大資料開發基礎:第四節:Linux檔案系統(二)

紅豆和綠豆發表於2016-01-04

http://blog.csdn.net/lovehuangjiaju/article/details/47905791

本節主要內容

  1. 訪問控制列表ACL
  2. 連結
  3. 壓縮和歸檔檔案
  4. 其他日常管理實用程式

1. 訪問控制列表ACL

在實際使用使用過程中,可能linux系統自身許可權控制不能滿足要求,還需要進一步細化,此時可以用ACL( Access Control List )進行,它主要提供 owner,group,others 的 read,write,execute 許可權之外的細部許可權配置。它可以針對單一使用者,單一檔案或目錄來進行 r,w,x 的許可權規範,對於需要特殊許可權的使用狀況非常有幫助。 
Ubuntu作業系統要使用ACL,需要手動去安裝,安裝方法如下: 
使用apt-get install acl命令進行ACL的安裝 
這裡寫圖片描述

(1)獲取檔案的acl

採用getfacl來獲取檔案的ACL詳細資訊,當檔案不包含acl時,所顯示的資訊與ls -l命令相同

<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;"># ls -l hello1.txt</span>
-rw---x--x <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xtwy xtwy <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">47</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<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;">23</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">43</span> hello1.txt

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;"># getfacl hello1.txt </span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># file: hello1.txt</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># owner: xtwy</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># group: xtwy</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">user:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:rw-</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">group:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:--x</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">other:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:--x</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></ul>

(2)設定檔案的acl

採用setfacl命令進行檔案精細許可權的設定,命令格式如下:

<code class="hljs css 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-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">setfacl</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">--modify</span>(<span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">-m</span>) <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">ugo</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:name</span><span class="hljs-pseudo" style="color: rgb(0, 0, 0); box-sizing: border-box;">:permissions</span> <span class="hljs-tag" style="color: rgb(0, 0, 0); box-sizing: border-box;">file-list</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></ul>

其中ugo分別是u或g或o,分別對應設定使用者、組及其他使用者對檔案的訪問許可權。name為使用者或組名,如果是o則無需指定,permissions是為待設定的許可權,file-list,使用例如下:

<code class="hljs cs 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;">//下列命令給組xtwy新增讀許可權,給組root新增讀寫執行許可權</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//給o也新增讀寫、執行許可權</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># setfacl -m g:xtwy:r,g:root:rwx,o::rwx hello1.txt</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//顯示acl資訊</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># getfacl hello1.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># file: hello1.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># owner: xtwy</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># group: xtwy</span>
user::rw-
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>::--x
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>:root:rwx
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">group</span>:xtwy:r--
mask::rwx
other::rwx
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//ls -l顯示的許可權資訊</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -l hello1.txt</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//檔案許可權部分多了個+號</span>
-rw-rwxrwx+ <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> xtwy xtwy <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">47</span> <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;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">43</span> hello1.txt

</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></ul>

(3)獲取目錄或檔案的acl

<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;"># mkdir acltest</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/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -ld acltest/</span>
drwxr-xr-x <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> acltest/

<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>獲取目錄的acl,與檔案操作一致
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;"># getfacl acltest/</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># file: acltest/</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># owner: root</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># group: root</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">user:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:rwx</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">group:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:r-x</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">other:</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:r-x</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></ul>

給目錄新增acl

<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;">root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># groupadd groupacl</span>
//採用-d命令,這是與普通檔案之間的區別
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># setfacl -d -m g:groupacl:r-x acltest/</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># getfacl acltest/</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># file: acltest/</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># owner: root</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># group: root</span>
<span class="hljs-label" style="box-sizing: border-box;">user:</span>:rwx
<span class="hljs-label" style="box-sizing: border-box;">group:</span>:r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>
<span class="hljs-label" style="box-sizing: border-box;">other:</span>:r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>
<span class="hljs-label" style="box-sizing: border-box;">default:</span>user::rwx
<span class="hljs-label" style="box-sizing: border-box;">default:</span>group::r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>
<span class="hljs-label" style="box-sizing: border-box;">default:</span>group:groupacl:r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>
<span class="hljs-label" style="box-sizing: border-box;">default:</span>mask::r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>
<span class="hljs-label" style="box-sizing: border-box;">default:</span>other::r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>

//ls -<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ld</span> 命令顯示,目錄許可權多了個+號
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -ld acltest/</span>
drwxr-xr-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>+ <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4096</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> acltest/



//在設定好的acl目錄中建立檔案與沒有設定acl的目錄中建立檔案,有以下區別

oot@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "file acl in acltst" > test1.txt</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -l test1.txt </span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">58</span> test1<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;"># getfacl test1.txt </span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># file: test1.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># owner: root</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># group: root</span>
<span class="hljs-label" style="box-sizing: border-box;">user:</span>:rw-
<span class="hljs-label" style="box-sizing: border-box;">group:</span>:r--
<span class="hljs-label" style="box-sizing: border-box;">other:</span>:r--


root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cd acltest/</span>
root@ubuntu:/home/xtwy/acltest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "file acl in acltst" > test1.txt</span>
//許可權後面也有+
root@ubuntu:/home/xtwy/acltest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -l test1.txt </span>
-rw-r--r--+ <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">59</span> test1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
//顯示建立檔案的acl資訊
root@ubuntu:/home/xtwy/acltest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># getfacl test1.txt </span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># file: test1.txt</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># owner: root</span>
<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># group: root</span>
<span class="hljs-label" style="box-sizing: border-box;">user:</span>:rw-
<span class="hljs-label" style="box-sizing: border-box;">group:</span>:r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>          <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#effective:r--</span>
<span class="hljs-label" style="box-sizing: border-box;">group:</span>groupacl:r-<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">x</span>      <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#effective:r--</span>
<span class="hljs-label" style="box-sizing: border-box;">mask:</span>:r--
<span class="hljs-label" style="box-sizing: border-box;">other:</span>:r--</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><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li></ul>

上面的程式碼說明,如果在acl目錄中沒有指定檔案的acl資訊,則會使用目錄對應預設acl。

2. 連結

(1) 硬連結

linux中的硬連結另一個檔案的形式出現在檔案結構中,如果硬連結與被連結檔案在同一個目錄中,則名字必須不一樣,不在同一個目錄的話,可以沒有此要求

<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;">root@ubuntu:/home/xtwy/acltest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
test1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
//利用ln命令建立一個test1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>檔案的硬連結
root@ubuntu:/home/xtwy/acltest<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ln test1.txt /home/xtwy/test1link</span>
root@ubuntu:/home/xtwy/acltest<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>
acltest  Documents         hello1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>  Music     Templates  Videos
Desktop  Downloads         hello<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>   Pictures  test1link
dir      examples<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.desktop</span>  literature  Public    test1<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.txt</span>
//利用cat命令檢視test1link檔案的內容,它其實是連結到test1<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;"># cat test1link</span>
file acl <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span> acltst
</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></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;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//建立完硬連結後,連結數+1</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy/acltest# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> -l
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r--+ <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">59</span> test1.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/acltest# ln test1.txt /home/xtwy/test1link2
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//連結數+1</span>
root<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">@ubuntu</span>:/home/xtwy/acltest# <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ls</span> -l
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r--+ <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">19</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">00</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">59</span> test1.txt

</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>

硬連結以檔案形式存在,但它實質上指向的是同一個檔案,只不過是建立了指向目標檔案的指標,即ln命令不會建立檔案的副本,即它與cp命令有著本質的區別,如下圖所示: 
這裡寫圖片描述

(2) 軟連結

linux作業系統除了支援硬連結之外,還支援符號連結(symbolic link)或軟連結(soft link)。硬連結是直接指向檔案的指標,它除了可以建立檔案的符號連結之外,還可以建立目錄的符號連結。先看幾個例子:

<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;">//不能建立目錄的硬連結</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ln Music softlink_Music</span>
ln: `Music': hard link <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">not</span> allowed <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> directory

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//建立目錄的符號連結</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ln --symbolic Music softlink_Music</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//連結數並沒有增加,還是1</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -l softlink_Music</span>
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">01</span> softlink_Music -> Music


<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//建立普通檔案的符號連結</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># echo "soft link test" >> soft.txt</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ln -s soft</span>
softlink_Music/ soft.txt        
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ln -s soft.txt softlink_txtfile</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//通過下面的命令可以看到, 符號連結檔案的大小為8</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//它與原始檔案大小不一樣,其大小是目標檔案路徑名中的字元數</span>
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls -l softlink_txtfile </span>
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span> softlink_txtfile -> soft.txt
root@ubuntu:/home/xtwy<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># cat softlink_txtfile </span>
soft link test
</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>

使用cd命令切換到目錄的符號連線時,路徑名中顯示的是符號連結名稱

<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;">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;"># cd softlink_Music</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>路徑名顯示是符號連結
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd ..</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/xtwy# cd Music/</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># touch music1.mp3</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
music1.mp3
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">28</span> music1.mp3
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd ../softlink_Music</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>目錄內容與Music目錄一致
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
music1.mp3
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># </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></ul>

符號連結可以指向一個不存在的檔案,例如

<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>刪除Music目錄
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rm -r ../Music</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>刪除後內容為空
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls </span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/softlink_Music<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd ..</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</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 -l softlink_Music </span>
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">01</span> softlink_Music<span class="hljs-function" style="box-sizing: border-box;"> -></span> Music
</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></ul>

這裡寫圖片描述

硬連結由於採用的是指標的方式,如果檔案刪除,它將一直指向已刪除的檔案,而軟連結總是指向新建立的檔案

<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>生成test檔案
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo "hard and soft link test" > test.txt</span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>生成硬連結
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ln test.txt hardlink_test.txt</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> hardlink_test.txt
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> test.txt
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat hardlink_test.txt </span>
hard <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> soft link test
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>建立符號連結
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ln -s test.txt softlink_test.txt</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> hardlink_test.txt
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">46</span> softlink_test.txt<span class="hljs-function" style="box-sizing: border-box;"> -></span> test.txt
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> test.txt
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat softlink_test.txt </span>
hard <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> soft link test
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>刪除test.txt檔案
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rm test.txt</span>
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> hardlink_test.txt
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">46</span> softlink_test.txt<span class="hljs-function" style="box-sizing: border-box;"> -></span> test.txt
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>hardlink_test.txt仍然指向刪除的檔案,只有當該連結也被刪除的時候
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>檔案才被刪除
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat hardlink_test.txt </span>
hard <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> soft link test
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>符號連結已經失效
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat softlink_test.txt </span>
<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">cat</span>: softlink_test.<span class="hljs-attribute" style="box-sizing: border-box; color: rgb(0, 136, 0);">txt</span>: No such file <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">or</span> directory
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/hard_symbolic_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># </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><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></ul>

這裡寫圖片描述

建立新的test.txt檔案之後,符號連結指向新檔案,硬連結仍然指向被刪除的檔案

<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/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># echo "new test file" > test.txt</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/xtwy/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">24</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">45</span> hardlink_test.txt
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">46</span> softlink_test.txt -> test.txt
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> test.txt
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/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat hardlink_test.txt </span>
hard <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">and</span> soft link test
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/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cat softlink_test.txt </span>
new test file
</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>

這裡寫圖片描述

刪除符號連結或硬連結與刪除其它檔案是一樣的,例如:

<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/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rm hardlink_test.txt </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/xtwy/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
lrwxrwxrwx <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">8</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">46</span> softlink_test.txt -> test.txt
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> test.txt
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/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rm softlink_test.txt </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/xtwy/hard_symbolic_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">14</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">05</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">50</span> test.txt
</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>

3. 壓縮和歸檔檔案

如果檔案較大的話,佔用大量的磁碟空間或網路流量,常常會對檔案進行壓縮儲存或網路傳送,本節將介紹幾種常用的檔案壓縮和歸檔方法

(1) bzip2壓縮

<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;"># mkdir compresse_demo</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/xtwy</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cd compresse_demo/</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/xtwy/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># cp /etc/profile .</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/xtwy/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
profile
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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span> profile
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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bizip profile </span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">bizip:</span> command <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">not</span> found
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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bzip2 -v profile </span>
  <span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">profile:</span>  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.608</span><span class="hljs-symbol" style="color: rgb(0, 102, 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;">4.974</span> bits/byte, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">37.83</span>% saved, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">309</span> out.
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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">309</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-08-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">:</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span> profile.bz2
</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></ul>

這裡寫圖片描述

(2) bz2檔案解壓縮

<code class="hljs bash 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;">//bunzip2 解壓縮
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bunzip2 profile.bz2 </span>
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls -l</span>
total <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>
-rw-r--r-- <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> root root <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span> profile


root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bzip2 -v profile </span>
  profile:  <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.608</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;">4.974</span> bits/byte, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">37.83</span>% saved, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">in</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">309</span> out.
//bzcat檢視檔案壓縮檔案內容,原檔案不會改變
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># bzcat profile.bz2 | tail -10</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> [ <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"`id -u`"</span> <span class="hljs-operator" style="box-sizing: border-box;">-eq</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> ]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">then</span>
      PS1=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'# '</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>
      PS1=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'$ '</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span>
  <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">fi</span>

umask <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">022</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>

(3) tar打包檔案

採用tar -cvf命令進行目錄或檔案的打包, c表示create,v表示verbose,顯示詳細資訊,f表示從一個檔案進行讀寫,打包好的檔案以tar作為副檔名結尾

<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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># tar -cvf test.tar profile </span>
profile
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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls </span>
profile  test.tar
</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>

(4) tar解包檔案

<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;">root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># rm profile </span>
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>按表棖方式顯示打包檔案內容,t表示表格
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># tar -tvf test.tar</span>
-rw-r--r-- root/root       <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">497</span> <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;">23</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">06</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span> profile
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># ls</span>
test.tar
<span class="hljs-regexp" style="color: rgb(0, 136, 0); box-sizing: border-box;">//</span>解包檔案,使用-x(extract)
root<span class="hljs-property" style="box-sizing: border-box;">@ubuntu</span>:/home/xtwy/compresse_demo<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># tar -xvf test.tar </span>
profile
</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>

(5) gzip壓縮與解壓縮檔案

<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;">//壓縮檔案,壓縮後的檔案以<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span>結尾
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># gzip profile </span>
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
profile<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</span>  test<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span>
//解壓縮檔案
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># gunzip profile.gz </span>
root@ubuntu:/home/xtwy/compresse_demo<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;"># ls</span>
profile  test<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</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></ul>

(6) tar.gz檔案解壓縮

在網路上傳輸的檔案通常都是經過gzip壓縮後,再由tar歸檔後的檔案,如下圖所示: 
這裡寫圖片描述 
使用tar -zxvf命令進行解壓縮

<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;">tar -zxvf hadoop-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2.7</span><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">.1</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.tar</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.gz</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></ul>

4. 其他日常管理實用程式

(1) whereis命令

在標準路徑下搜尋與名稱相關的檔案,whereis將所有搜尋到的檔案都顯示

<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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># whereis tar</span>
<span class="hljs-symbol" style="color: rgb(0, 102, 102); box-sizing: border-box;">tar:</span> /bin/tar /usr/<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">include</span>/tar.h /usr/share/man/man1/tar.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>.gz /usr/share/man/man5/tar.<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>.gz
</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>

(2) which命令

which在設定的搜尋路徑下進行目錄搜尋,只顯示搜尋到的第一個檔案

<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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># which tar</span>
/bin/tar
</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>

(3) locate命令

locate命令用於在本地檔案系統上進行檔案搜尋,下面的例子給出了與xtwy相關的所有系統檔案,包括隱藏檔案:

<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/compresse_demo</span><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># locate xtwy</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></ul>

這裡寫圖片描述

相關文章