Linux命令之chown

來源:互聯網
上載者:User

標籤:

chown 變更檔全部者和組

文法:

     chown [OPTION]  [OWNER][:[GROUP]] FILE

     chown [OPTION] --reference=RFILE  FILE

描寫敘述:

      chown 改變指定檔案的使用者和/或組的擁有者。

  •  假設只給定owner(username活在使用者ID),運行此語句後,只改變選定檔案的擁有者,檔案的組是沒有改變的。

     比如:

           [email protected]:~/mycode/TEST$ ls -l
          總用量 0

          -rw-rw-r-- 1 liujl liujl 0 2012-06-20 09:31 mytest.txt

     運行chown 後:

          [email protected]:~/mycode/TEST$ sudo chown juanzhang mytest.txt
          [sudo] password for liujl:
          [email protected]:~/mycode/TEST$ ls -l
          總用量 0
          -rw-rw-r-- 1 juanzhang liujl 0 2012-06-20 09:31 mytest.txt

  •  假設owner後跟著“:”和組名(或者組ID),注意在“:”兩邊不要有空格,運行完此條語句後,使用者和組擁有者都會發生改變,比如:

         [email protected]:~/mycode/TEST$ ls -l
          總用量 0

          -rw-rw-r-- 1 liujl liujl 0 2012-06-20 09:31 mytest.txt

         運行chown後:

          [email protected]:~/mycode/TEST$ sudo chown liujl:liujl mytest.txt
          [sudo] password for liujl:
          [email protected]:~/mycode/TEST$ ls -l
          總用量 0
          -rw-rw-r-- 1 liujl liujl 0 2012-06-20 09:31 mytest.txt
         

  •  假設僅有“:”,但後面沒有組名,系統會改變檔案的擁有者及此擁有組所在的組。

          比如:

         [email protected]:~/mycode/TEST$ ls -l
        總用量 0
         -rw-rw-r-- 1 liujl liujl 0 2012-06-20 09:31 mytest.txt

          [email protected]:~/mycode/TEST$ sudo chown juanzhang: mytest.txt
         [sudo] password for liujl:
         [email protected]:~/mycode/TEST$ ls -l
         總用量 0
          -rw-rw-r-- 1 juanzhang juanzhang 0 2012-06-20 09:31 mytest.txt

  •  假設“:”和組名給定,可是擁有者忽略,僅有組名被改動;在這樣的情況下,chown的功能和chgrp的功能相似。

          [email protected]:~/mycode/TEST$ ls -l
          總用量 0
          -rw-rw-r-- 1 juanzhang juanzhang 0 2012-06-20 09:31 mytest.txt
           [email protected]:~/mycode/TEST$ sudo chown :liujl mytest.txt
           [email protected]:~/mycode/TEST$ ls -l
           總用量 0
           -rw-rw-r-- 1 juanzhang liujl 0 2012-06-20 09:31 mytest.txt

  •  假設僅“:”給定或者整個運算元為空白,檔案擁有者或者組名都不會改變。

           [email protected]:~/mycode/TEST$ ls -l
           總用量 0
           -rw-rw-r-- 1 juanzhang liujl 0 2012-06-20 09:31 mytest.txt
           [email protected]:~/mycode/TEST$ chown : mytest.txt
           [email protected]:~/mycode/TEST$ ls -l
           總用量 0
           -rw-rw-r-- 1 juanzhang liujl 0 2012-06-20 09:31 mytest.txt
            整個運算元為空白,會報錯:

           [email protected]:~/mycode/TEST$ chown mytest.txt
           chown: "mytest.txt" 後缺少運算元

參數:

         -R :進行遞迴的持續更改,即將同子檔案夾下的全部檔案、檔案夾都更新問這個使用者組。通經常使用在更改某一檔案夾的情況。

 應用場合:

        此命令用的最多的地方是,當我們使用mv或者cp拷貝給其它人的時候,這些檔案的擁有者和組名沒有改變,所以須要改動。

       範例:

       [email protected]:/usr$ ls -l|grep mytest2.txt
       -rw-r--r--   1 root root     0 2012-06-20 13:50 mytest2.txt
       [email protected]:/usr$ sudo mv mytest2.txt  /home/liujl/mycode/TEST/
       [email protected]:~/mycode/TEST$ ls -l
      總用量 0
       -rw-r--r-- 1 root      root  0 2012-06-20 13:50 mytest2.txt
      如今mytest2.txt已經mv到liujl使用者下了,可是當改動的時候是不同意的,從上句能夠看出,還是屬於root root ,這就須要chown了。

richerg85的專欄//記錄自己工作學習中的點點滴滴,希望有一天會變強大//

Linux命令之chown

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.