1. 程式人生 > >Centos常用命令之:文件與目錄管理

Centos常用命令之:文件與目錄管理

一個 每一個 -- 目錄 clas rect 發現 linux中 使用

在centos中常用的文件與目錄操作命令有:

◇chmod:修改文件或目錄的權限

◇mkdir:新建目錄
◇rmdir:刪除目錄
◇rm:刪除目錄或文件
◇cp:復制目錄或文件
◇mv:移動目錄或文件

下面就一個個的來詳細解釋。

chmod,chown,chgrp

chmod:用來修改文件或目錄的權限。

前面我們說過,文件或目錄的權限分為,擁有者,同群組,和其他人三種的權限,然後每一個又分別可以控制,讀,寫,執行的權限。

其中擁有者,同群組,其他人我們分別用u,g,o來代替,a代表ugo三個組。

修改方式:

chmod [選項] [ugoa] [+-=] [rwx] 文件或目錄

選項:-R:代表遞歸

+:代表追加權限

-:代表取消權限

=:代表修改權限為

r:代表讀權限

w:代表寫權限

x:代表執行權限

使用例:

[[email protected] stu]$ ll
總用量 0
-rw-rw-r--. 1 fuwh fuwh 0 7月  23 17:51 test
[[email protected] stu]$ chmod u+x,g-w,o=rw test
[[email protected] stu]$ ll
總用量 0
-rwxr--rw-. 1 fuwh fuwh 0 7月  23 17:51 test
[[email protected]
/* */ stu]$

可以發現這種方式,不是那麽的方便,那麽在linux中,對於權限的描述,還可以使用數字來描述。

r:4

w:2

x:1

那麽,7就代表rwx,6就代表rw,5就代表rx,3就代表wx

使用例:

[[email protected] stu]$ ll
總用量 0
-rwxr--rw-. 1 fuwh fuwh 0 7月  23 17:51 test
[[email protected] stu]$ chmod 456 test
[[email protected] stu]$ ll
總用量 0
-r--r-xrw-. 1 fuwh fuwh 0 7月  23 17:51 test
[[email protected] stu]$

chown:

這個命令用來修改文件或目錄的所有者

使用方式:

chown [-R] 擁有者[:用戶組] 文件或目錄

-R:表示在修改目錄的擁有者的時候,遞歸修改

chgrp:

這個命令用來修改文件或目錄的用戶組

要修改用戶組,則用戶組必須在/etc/group中存在才行。

使用方式:

chgrp [-R] 用戶組 文件或目錄

◇mkdir

這個命令主要是用來創建目錄的。

常用選項:

-m:創建文件安的時候順便指定權限

-p:遞歸創建目錄

使用例子:

[[email protected] stu]$ ll
總用量 0
[[email protected] stu]$ mkdir -m 777 test/mkdir01
mkdir: 無法創建目錄"test/mkdir01": 沒有那個文件或目錄
[[email protected] stu]$ mkdir -pm 777 test/mkdir01
[[email protected] stu]$ ll test
總用量 4
drwxrwxrwx. 2 fuwh fuwh 4096 7月  23 17:10 mkdir01
[[email protected] stu]$

rmdir

這個命令主要用來刪除文件夾,但是這個命令有個缺陷就是,只能刪除非空的文件夾,裏面不論是有空的文件夾還是文件,都不能刪除。所以用的比較少,了解即可。

rmdir 目錄名稱

rm

這個命令可以用來刪除目錄或者文件夾,非常的方便;這個命令是一個非常危險的命令,使用不當,可能會刪除整個主機的文件(因為可以使用‘*’這種通配符)。

常用選項:

-i:刪除前會有確認信息

-f:強制刪除

-r:遞歸刪除

使用例子:

[[email protected] stu]$ tree
.
mqq test
    tqq test1
    mqq xx

2 directories, 1 file
[[email protected] stu]$ rm -rf test
[[email protected] stu]$ ll
總用量 0
[[email protected] stu]$

cp

這個命令用來復制文件或者目錄。

使用方式:

cp [選項] [源文件] [目標文件]

常用選項:

-i:如果存在,則會詢問是否覆蓋

-p:連同屬性一起復制過去

-r:遞歸復制,用於目錄的復制行為

-s:復制為符號連接檔

-f:強制復制

-d:如果是連接檔,則復制連接檔屬性,而不是文件本身

-a:相當於-pdr

使用實例:

[[email protected] stu]$ tree
∟ test01
| ∟test01 ∟ test02
2 directories, 1 file [[email protected] stu]$ cp test01/test01 test02/test02 [[email protected] stu]$ cp -p test01/test01 test02/test03 [[email protected] stu]$ cp -r test01 test02/test04 [[email protected] stu]$ ll 總用量 8 drwxrwxr-x. 2 fuwh fuwh 4096 7月 23 17:30 test01 drwxrwxr-x. 3 fuwh fuwh 4096 7月 23 17:33 test02 [[email protected] stu]$ cd test02 [[email protected] test02]$ ll 總用量 4 -rw-rw-r--. 1 fuwh fuwh 0 7月 23 17:32 test02 -rw-rw-r--. 1 fuwh fuwh 0 7月 23 17:30 test03 drwxrwxr-x. 2 fuwh fuwh 4096 7月 23 17:33 test04 [[email protected] test02]$

mv

這個命令主要用來移動文件或者文件夾,還有一個就是重命名的功能。

使用方式:

mv [選項] 源文件 目標文件

常用選項:

-f:表示強制的意思,如果目標文件已經存在,則不會詢問,直接覆蓋

-i:如果目標文件存在,則會詢問是否覆蓋

-u:如果目標文件存在,且源文件比較新,則會對目標文件進行更新

使用實例:

[[email protected] stu]$ tree
.
∟ test01
  ∟t1
∟ test02

2 directories, 1 file
[[email protected] stu]$ mv test01/t1 test02/t2
[[email protected] stu]$ tree
.
∟ test01
∟ test02
     ∟ t2

2 directories, 1 file
[[email protected] stu]$

Centos常用命令之:文件與目錄管理