1. 程式人生 > 實用技巧 >LINUX核心命令實戰總結三——檔案和目錄操作命令二

LINUX核心命令實戰總結三——檔案和目錄操作命令二

1.1 ln:硬連結與軟連結

【功能說明】

ln 命令可以理解為英文單詞link的縮寫,其功能是建立檔案間的連結,連結包括硬連結和軟連結。

【語法格式】

  ln         [OPTION]...     [source]       [target]
  ln         [選項]...         [原始檔]       [目標檔案或目錄]

【選項說明】

命令ln的引數選項及說明

引數選項

解釋說明(帶*的為重點)

無引數

建立硬連結(*

-s

建立軟連線(符號連結)(*

【硬連結使用範例】

硬連結是通過索引節點來進行連結。在

Linux檔案系統中,多個檔名指向同。一個索引節點是正常且允許的,這種情況下的檔案就稱為硬連結。

[root@web01 ~]# ln /etc/hosts hard_link
[root@web01 ~]# ls -i /etc/hosts hard_link
1310748 /etc/hosts  1310748 hard_link
[root@web01 ~]# rm -f /etc/hosts
[root@web01 ~]# cat /etc/hosts
cat: /etc/hosts: 沒有那個檔案或目錄
[root@web01 ~]# ln hard_link /etc/hosts
[root@web01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.28.11.232 server 10.28.11.234 client 10.28.11.235 web01 10.28.11.236 web02 [root@web01 ~]# ls -i /etc/hosts hard_link 1310748 /etc/hosts 1310748
hard_link
View Code

【硬連結知識小總結】

1、具有相同的inode 節點號的多個檔案互為硬連結

2、刪除硬連結檔案或者刪除原始檔任意之一,檔案實體並未被刪除。

3、只有刪除了原檔案以及原始檔所有對應的硬連結檔案,檔案實體才會被刪除。

4、當所有的硬連結檔案及原始檔被刪除之後,再存放新的資料時會佔用這個檔案的空間,或者磁碟fsck檢查的時候,刪除的資料也會被系統回收。

5、硬連結檔案就是檔案的另一個入口

6、可以通過給檔案設定硬連結檔案,來防止重要檔案被誤刪

7、執行命令ln 原始檔 硬連結檔案”,即可完成硬連結的建立

8、硬連結檔案可以用rm命令刪除

9、對於靜態檔案(沒有程序正在呼叫的檔案)來講,當對應硬連結數為0時,檔案就會被刪除,

【軟連線語法格式】

 ln      -s    [OPTION]...     [source]       [target]
 ln      -s    [選項]...         [原始檔]       [目標檔案或目錄]

執行ln命令建立軟連結

[root@web01 ~]# ln -s /etc/hosts soft_link
[root@web01 ~]# ll -hi|grep /etc/hosts
1703982 lrwxrwxrwx  1 root root   10 1月  19 12:12 soft_link -> /etc/hosts

【軟連結知識小結】

1、軟連結類似於Windows的快捷方式

2、軟連結類似於一個文字檔案,裡面存放的是原始檔的路徑,指向源文字實體

3、即使刪除了原始檔,軟連結檔案也還是依然存在,但是無法訪問指向的原始檔路徑內容了。

4、失效的時候一般是白字紅底閃爍提示

5、執行命令ln -s 原始檔 軟連結檔案”, 即可完成建立軟連結

6、軟連結和原始檔是不同型別的檔案,也是不同的檔案,inode號也不相同

7、刪除軟連線檔案可以使用rm命令

1.2 readlink:檢視符號連結檔案的內容

【功能說明】

Readlink命令,它能夠幫助我們檢視符號連結檔案的真是情況。

【語法格式】

readlink     [OPTION]...     [FILE]
readlink     [選項]...         [檔案]

【選項說明】

Readlink命令的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-f

一直跟隨符號連結,直到非符號連結的檔案位置,但要保證最後必須存在一個非符號連結檔案

【使用範例】

[root@web01 ~]# ll -h /usr/bin/awk
lrwxrwxrwx. 1 root root 14 7月   5 2016 /usr/bin/awk -> ../../bin/gawk
[root@web01 ~]# readlink /usr/bin/awk
../../bin/gawk
[root@web01 ~]# readlink  -f /usr/bin/awk
/bin/gawk

1.3 find:查詢目錄下的檔案

【功能說明】

Find 命令用於查詢目錄的檔案,同時也可以呼叫其他命令執行相應的操作

【語法格式】

find     [-H]     [-L]     [-P]     [-D debugopts]     [-Olevel]     [path...]     [expression]
find         [選項]     [-D debugopts]     [-Olevel]     [路徑]     [操作語]

【選項說明】

引數選項

解釋說明(帶*的為重點)

pathname

命令所查詢的目錄路徑,例如“.”來表示當前目錄,用“/”來表示系統根目錄(*

Options模組

-depth

從指定目錄下最深的子目錄開始查詢

-maxdepth levels

查詢的最大目錄級數,levels為自然數(*

-regextype type

改變正則表示式的模式,預設為emacs,還有posix-awkposix-basicposix-egrepposix-extended

Tests模組

-mtime[-n|n|+n]

按照檔案的修改時間來查詢檔案(這個引數最常用),具體如下(*

-n表示檔案更改時間距現在n天以內

+n表示檔案更改時間距現在n天以前

n是距現在第n

-atime[-n|n|+n]

按照檔案的訪問時間來查詢檔案,單位為天

-ctime[-n|n|+n]

按照檔案的狀態改變時間來查詢檔案,單位為天

-amin

按照檔案的訪問時間來查詢檔案,單位為分鐘

-cmin

按照檔案的狀態改變時間來查詢檔案,單位為分鐘

-mmin

按照檔案的修改時間來查詢檔案,單位為分鐘

-group

按照檔案所屬的組來查詢檔案

-name

按照檔名查詢檔案,只支援*、?、[]等特殊萬用字元(*

-newer

查詢更改時間比指定檔案新的檔案

-nogroup

查詢沒有有效使用者組的檔案,即該檔案所屬的組在/etc/groups中不存在

-nouser

查詢沒有有效屬主的檔案,即該檔案的屬主在/etc/passwd中不存在

-path paternt

指定路徑樣式,配合-prune引數排除指定目錄

-perm

按照檔案許可權來查詢檔案

-regex

接正則表示式

-iregex

接正則表示式,不區分大小寫

-size n[]

查詢檔案長度為n塊的檔案

-user

按照檔案屬主查詢檔案

-type

查詢某一型別的檔案:(*

b (塊裝置)

c (字元裝置檔案)

d (目錄)

P (管道檔案)

l (符號連結檔案)

f (普通檔案)

s socket 檔案)

D door

Action模組

-delete

將查找出的檔案刪除

-exec

對匹配的檔案執行該引數所給出的shelll命令(*

-ok

-exec作用相同,但在執行每個命令之前,都會讓使用者先確定是否執行

-prune

使用這一項可以使find命令不在當前指定的目錄中查詢

-print

將匹配的檔案輸出到標準輸出(預設功能,使用中可省略)

OPERATORS

Find支援邏輯運算子

!

取反(*

-a

取交集,全拼為and*

-o

取並集,全拼為or*

【使用範例】

查詢指定時間內修改過的檔案

[root@web01 ~]# find . -atime -2
.
./.bashrc
./dir2
./test1
./test1/d
./test1/e
./test1/b
./test1/a
./test1/g
View Code

-name指定關鍵字查詢

[root@web01 ~]# find /var/log -mtime +3 -name '*.log'
/var/log/anaconda.storage.log
/var/log/anaconda.log
/var/log/dracut.log
/var/log/anaconda.program.log
/var/log/anaconda.ifcfg.log
/var/log/anaconda.yum.log

利用!”反向查詢

[root@web01 test]# find . ! -type d
./file2.txt
./dir5/yhh04
./dir5/file6.txt
./dir5/yhh02
[root@web01 test]# find .  -type d 
.
./dir2
./dir5
./dir5/sub1
./dir5/sub1/test
View Code

按照目錄或檔案的許可權來查詢檔案

[root@web01 test]# find /data/ -perm 755
/data/
/data/dir2
/data/dir3

按大小查詢檔案

[root@web01 test]# find . -size +1000c
.
./dir2
./dir5
./dir5/sub1
./dir5/sub1/test
./dir5/dir3
./dir4
./dir4/sub1
./dir4/sub1/test
View Code

查詢檔案時希望忽略某個目錄

[root@web01 test]# find /data -path "/data/dir3" -prune -o -print 
/data
/data/dir2
/data/file2.txt
/data/file1.txt
View Code

忽略多個目錄

[root@web01 test]# find /data \( -path /data/dir2 -o -path /data/dir3  \) -prune -o -print
/data
/data/file2.txt
/data/file1.txt
View Code

使用usernouser選項

[root@web01 test]# chown nobody:nobody file2.txt
[root@web01 test]# ll -h file2.txt
-rw-r--r-- 1 nobody nobody 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -user nobody
./file2.txt
[root@web01 test]# chown 555 file2.txt 
[root@web01 test]# ll -h file2.txt 
-rw-r--r-- 1 555 nobody 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -nouser
./file2.txt
View Code

使用groupnogroup選項

[root@web01 test]# find . -group nobody
./file2.txt
[root@web01 test]# chown .555 file2.txt
[root@web01 test]# ll -h file2.txt
-rw-r--r-- 1 555 555 0 1月  18 15:48 file2.txt
[root@web01 test]# find . -nogroup
./file2.txt
View Code

查詢比某個檔案新或舊的檔案

[root@web01 test]# find . -newer file2.txt   
.
./dir2
./dir5
./dir5/yhh04
./dir5/sub1
./dir5/sub1/test
./dir5/dir3
./dir5/yhh02
./dir5/yhh03
./dir5/yhh01
./dir5/yhh05
./dir5/yhh00
./.file4.txt
./dir4
./dir4/sub1
./dir4/sub1/test
[root@web01 test]# find . -newer file2.txt ! -newer file3.txt
View Code

邏輯操作符的使用

[root@web01 test]# find . -maxdepth 1 -type d  
.
./dir2
./dir5
./dir4
[root@web01 test]# find . -maxdepth 1 -type d ! -name "."
./dir2
./dir5
./dir4
[root@web01 test]# find . -maxdepth 1 -type d ! -name "." -o -name "dir2"
./dir2
View Code

Find正則表示式

[root@web01 ~]# find / -regex ".*find"
/usr/bin/oldfind
/usr/bin/find
/usr/bin/gst-typefind
/bin/find
[root@web01 ~]# find / -regex ".*/find"
/usr/bin/find
/bin/find
View Code

正則表示式的其他型別

root@web01 ~]# cd /data
[root@web01 data]# find . -regextype "posix-egrep" -name '*[0-9]'
./dir2
./dir3

命令ls -l 放在find命令的 -exec選項中執行

[root@web01 data]# find . -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file2.txt
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file1.txt

在目錄中查詢到更改時間在n天以前的檔案,並刪除它們

[root@web01 data]# find . -type f -mtime +14 -exec rm {} \;

使用exec的安全模式-ok

[root@web01 data]# find /var/log -name "*.log" -mtime +5 -ok rm {} \;
< rm ... /var/log/anaconda.storage.log > ? n
< rm ... /var/log/anaconda.log > ? n
< rm ... /var/log/dracut.log > ? ^C

命令ls -l 命令放在find命令的xargs

[root@web01 data]# find . -type f|xargs ls -l
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file1.txt
-rw-r--r-- 1 root root 0 1月  19 09:20 ./file2.txt

使用xargs 執行mv 命令

[root@web01 data]# find . -name "*.txt"|xargs -i mv {} dir2/
[root@web01 data]# ls
dir2  dir3
[root@web01 data]# ls dir2/
file1.txt  file2.txt

命令find 結合xargs-p選項的使用示例

[root@web01 data]# find dir2/ -name "file*"|xargs -p rm -f
rm -f dir2/file2.txt dir2/file1.txt ?...y
View Code

【技巧性範例】

進入目錄/root目錄下data目錄,刪除file5.txt

[root@web01 data]# cd /data
[root@web01 data]# ls
dir2  dir3  file5.txt
[root@web01 data]# rm file5.txt 

[root@web01 ~]# find /data -type f -name "*file5.txt"|xargs rm -f

[root@web01 ~]# find /data -type f -name "*file5.txt" -exec rm {} \;

1.4 xargs:將標準輸入轉換成命令列引數

【功能說明】

命令xargs是向其他命令傳遞命令引數一個過濾器,能夠將管道或者標準輸入傳遞的資料傳遞換成xargs命令後跟隨的命令的命令列引數。

【語法格式】

xargs         [option]
xargs         [選項]

【選項說明】

命令xargs 引數選項及說明

引數選項

解釋說明(帶*的為重點)

-n

指定每行的最大數量n,可以將標準輸入的文字劃分為多行,每行n個引數,預設空格分離(*

-d

自定義分隔符

-I

指定一個符號替代前面結界,而不用-i引數預設的{}

-p

提示讓使用者確認是否執行後面的命令,y執行,n不執行

-0(數字0)

null代替空格作為分隔符,配合find命令的 -print()選項的輸出使用

【使用範例】

多行輸入變單行的例子

[root@web01 ~]# xargs < test.txt
1 2 3 4 5 6 7 8 9 10 11
[root@web01 ~]# cat test.txt    
1 2 3 4 5 6
7 8 9
10 11
[root@web01 ~]# xargs < test.txt
1 2 3 4 5 6 7 8 9 10 11
View Code

通過-n指定每行的輸出個數的例子

[root@web01 ~]# xargs  -n 3 < test.txt
1 2 3
4 5 6
7 8 9
10 11
View Code

自定義分隔符(使用-d功能)的例子

[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX
splitXsplitXsplitXsplitXsplitX
[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX|xargs -d X
split split split split split 
[root@web01 ~]# echo splitXsplitXsplitXsplitXsplitX|xargs -d X -n 2
split split
split split
split 
View Code

引數-I可以指定一個替換的字串

[root@web01 data]# find . -name "*.log"|xargs -i mv {} dir1/
[root@web01 data]# ls
dir2  dir3  file5.txt
[root@web01 data]# find . -name "file*"|xargs -I [] cp [] dir2
cp: "./dir2/file5.txt""dir2/file5.txt" 為同一檔案
View Code

結合find使用xargs的特殊案例

[root@web01 tmp]# touch "hello word.txt"
[root@web01 tmp]# ls
hello word.txt
[root@web01 tmp]# touch hello\ everyone.txt
[root@web01 tmp]# ls
hello everyone.txt  hello word.txt
[root@web01 tmp]# find . -type f -name "*.txt"|xargs rm
rm: 無法刪除"./hello": 沒有那個檔案或目錄
rm: 無法刪除"word.txt": 沒有那個檔案或目錄
rm: 無法刪除"./hello": 沒有那個檔案或目錄
rm: 無法刪除"everyone.txt": 沒有那個檔案或目錄
[root@web01 tmp]# find . -type f -name "*.txt" -print |xargs -0  rm -f 
[root@web01 tmp]# ls
hello everyone.txt  hello word.txt
[root@web01 tmp]# find . -type f -name "*.txt" -print0 |xargs -0  rm -f 
[root@web01 tmp]# ls
View Code

1.5 rename:重新命名檔案

【功能說明】

命令rename通過字串替換的方式批量修改檔名

【語法格式】

rename     from         to         file ...

【使用案例】

[root@web01 data]# touch yhh{00..05}.txt
[root@web01 data]# ls
dir2  dir3  file5.txt  yhh00.txt  yhh01.txt  yhh02.txt  yhh03.txt  yhh04.txt  yhh05.txt
[root@web01 data]# ls
dir2  dir3  file5.txt  yhh00.txt  yhh01.txt  yhh02.txt  yhh03.txt  yhh04.txt  yhh05.txt
[root@web01 data]# rename "hh0" " " *
[root@web01 data]# ls
dir2  dir3  file5.txt  y 0.txt  y 1.txt  y 2.txt  y 3.txt  y 4.txt  y 5.txt
View Code

批量修改副檔名案例

[root@web01 data]# ls
dir2  dir3  file5.txt  y 0.txt  y 1.txt  y 2.txt  y 3.txt  y 4.txt  y 5.txt
[root@web01 data]# touch yhh{06..10}.jpk
[root@web01 data]# ls
dir2  file5.txt  y 1.txt  y 3.txt  y 5.txt    yhh07.jpk  yhh09.jpk
dir3  y 0.txt    y 2.txt  y 4.txt  yhh06.jpk  yhh08.jpk  yhh10.jpk
[root@web01 data]# rename .jpk .txt *.jpk
[root@web01 data]# ls
dir2  file5.txt  y 1.txt  y 3.txt  y 5.txt    yhh07.txt  yhh09.txt
dir3  y 0.txt    y 2.txt  y 4.txt  yhh06.txt  yhh08.txt  yhh10.txt
View Code

1.6 basename:顯示檔名或目錄名

【功能說明】

命令basename 用於顯示去除路徑和檔案字尾部分的檔名或目錄名

【語法格式】

basename         [NAME]         [SUFFIX]
basename         [檔案或目錄]     [字尾]

【使用範例】

[root@web01 ~]# ls  /data/dir1/file1.txt     
/data/dir1/file1.txt
[root@web01 ~]# basename /data/dir1/file1.txt
file1.txt

1.7 dirname:顯示檔案或目錄路徑

【功能說明】

命令dirname 用於顯示檔案或目錄

【語法格式】

dirname         [NAME]
dirname         [檔案或目錄]

【使用範例】

顯示檔案或目錄路徑

[root@web01 ~]# dirname /data/dir1/file1.txt 
/data/dir1
[root@web01 ~]# cd /data/dir1
[root@web01 dir1]# dirname file1.txt

1.8 chattr:改變檔案的擴充套件屬性

【功能說明】

命令chattr用於改變檔案的擴充套件屬性。與chmod這個命令相比,chmod只是改變檔案的讀、寫、執行許可權,更底層的屬性由chattr來改變。

【語法格式】

 Chattr      [ option ]         [ mode ]         files...
 chattr       [ 選項 ]         [模式 ]          [檔案或目錄]...

【引數說明】

引數選項

解釋說明(帶*的為重點)

-R

遞迴更改目錄屬性

-V

顯示命令執行過程

mode

+

增加引數

-

移除引數

=

更新為指定引數

A

告訴系統不要修改這個檔案的最後訪問時間

a

只能向檔案中新增資料,而不能刪除,多用於伺服器日誌檔案安全(*

i

設定檔案不能被刪除,改名,寫入,或新增加內容。

【使用範例】

設定只能往檔案裡追加內容,但不能刪除檔案

[root@web01 dir1]# touch test
[root@web01 dir1]# ls
file1.txt  test
[root@web01 dir1]# lsattr test
-------------e- test
[root@web01 dir1]# chattr +a test
[root@web01 dir1]# lsattr test   
-----a-------e- test
[root@web01 dir1]# rm -f test
rm: 無法刪除"test": 不允許的操作
[root@web01 dir1]# echo 111 >>test
[root@web01 dir1]# cat test
111
[root@web01 dir1]# echo 222 > test
-bash: test: 不允許的操作
View Code

【安全優化例項】

歷史記錄.bash_history採用只增加不減少

[root@web01 ~]# chattr +a .bash_history
[root@web01 ~]# lsattr .bash_history
-----a-------e- .bash_history

鎖定一些檔案不能被修改

[root@web01 ~]# chattr +i /etc/inittab
[root@web01 ~]# lsattr /etc/inittab
----i--------e- /etc/inittab

1.9 lsattr:檢視檔案擴充套件屬性

【功能說明】

命令lsattr用於檢視檔案擴充套件屬性

【語法格式】

lsattr         [ options]         [ files...  ]
lsattr          [選項]         [ 檔案或目錄 ]

【選項說明】

引數選項

解釋說明(帶*的為重點)

-R

遞迴檢視目錄的擴充套件性

-a

顯示所有檔案包括隱藏檔案的擴充套件屬性

-d

顯示目錄的擴充套件屬性

【使用範例】

檢視檔案的擴充套件屬性

[root@web01 ~]# lsattr aa 
-------------e- aa

檢視目錄擴充套件屬性

[root@web01 ~]# ll -d dir2
d-wx-wx-wx 2 root root 4096 1月  18 12:18 dir2
[root@web01 ~]# lsattr -d dir2
-------------e- dir2

1.10 file:顯示檔案的型別

【功能說明】

命令file用於顯示檔案的型別

【語法格式】

 file         [options]        [file]
 file         [選項]        [檔案或目錄]

【選項說明】

命令file的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-b

輸出資訊使用精簡格式,不輸出檔名

【使用範例】

[root@web01 ~]# file aa
aa: empty
[root@web01 ~]# echo 111 >> aa
[root@web01 ~]# file aa       
aa: ASCII text

1.11 md5sum:計算和校驗檔案的md5的值

【功能說明】

它是一種不可逆的件演算法

【語法格式】

md5sum         [OPTION]...             [FILE]...
md5sum         [選項]...             [檔案或目錄]...

【選項說明】

引數選項

解釋說明(帶*的為重點)

-b

二進位制模式讀取檔案

-c

從指定檔案讀取md5校驗值,並進行校驗

-t

文字模式讀取檔案,這是預設模式

--quiet

校驗檔案使用的引數,驗證通過不輸出OK

--status

校驗檔案使用的引數,不輸出任何資訊,可以通過命令的返回值來判斷

【使用範例】

生成一個檔案的md5

[root@web01 ~]# md5sum aa
1181c1834012245d785120e3505ed169  aa

校驗是否發生改變

[root@web01 ~]# md5sum aa
1181c1834012245d785120e3505ed169  aa
[root@web01 ~]# md5sum aa > md5.log
[root@web01 ~]# cat md5.log
1181c1834012245d785120e3505ed169  aa
[root@web01 ~]# md5sum -c md5.log
aa: 確定
[root@web01 ~]# echo 222 >> aa
[root@web01 ~]# md5sum -c md5.log
aa: 失敗
md5sum: 警告:1/1 生成的校驗和不匹配

1.12 chown:改變檔案或目錄的使用者和使用者組

【功能說明】

命令chown 用於改變檔案或目錄的使用者和使用者組

【語法格式】

chown         [OPTION]...         [OWNER][:[GROUP]]         FILE...
chown         [選項]...              [使用者][:[使用者組]]        《檔案或目錄》...

常用格式:

chown        使用者            檔案或目錄        #僅僅授權使用者
chown        :組          檔案或目錄        #僅僅授權組
chown        使用者:組        檔案或目錄        #表示授權使用者和組

【使用說明】

命令chown 的引數選項及說明

引數選項

解釋說明(帶*的為重點)

-R

遞迴更改目錄的使用者和使用者組(*

【使用範例】

更改檔案所屬的使用者屬性

[root@web01 data]# touch file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chown 777 file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 777 root 0 1月  20 19:41 file1.txt

更改檔案所屬組(使用者組)屬性

[root@web01 data]# chown .yhh file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 777 yhh 0 1月  20 19:41 file1.txt

同時更改檔案所屬的使用者和組屬性

[root@web01 data]# chown root:root file1.txt
[root@web01 data]# ll file1.txt
-rw-r--r-- 1 root root 0 1月  20 19:41 file1.txt

遞迴更改目錄及目錄下所有子目錄及檔案的使用者和使用者組屬性

[root@web01 data]# useradd yhh -g yhh    
[root@web01 data]# chown -R  yhh.yhh dir1/
chown: 正在更改"dir1/test" 的所有者: 不允許的操作
[root@web01 data]# ll  dir1  
總用量 4
-rw-r--r-- 1 yhh  yhh  0 1月  20 13:16 file1.txt
-rw-r--r-- 1 root root 4 1月  20 13:39 test

1.13 chmod:改變檔案或目錄許可權

【功能說明】

命令chmod用來改變目錄或檔案許可權命令的。

【語法格式】

chmod         [OPTION]...             [MODE]...             FILE...
chmod         [選項]...             [模式]...             《檔案或目錄》...

【選項說明】

引數選項

解釋說明(帶*的為重點)

-R

遞迴更改目錄的使用者和使用者組(*

【使用範例】

[root@web01 data]# ll file1.txt
---------- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod u+x file1.txt
[root@web01 data]# ll file1.txt
---x------ 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod g+w file1.txt
[root@web01 data]# ll file1.txt
---x-w---- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod o+r file1.txt
[root@web01 data]# ll file1.txt
---x-w-r-- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod u=rwx,g=rw,o=r file1.txt
[root@web01 data]# ll file1.txt
-rwxrw-r-- 1 root root 0 1月  20 19:41 file1.txt
View Code

檔案的數字許可權授權案例

[root@web01 data]# chmod 000 file1.txt
[root@web01 data]# ll file1.txt
---------- 1 root root 0 1月  20 19:41 file1.txt
[root@web01 data]# chmod 753 file1.txt
[root@web01 data]# ll file1.txt
-rwxr-x-wx 1 root root 0 1月  20 19:41 file1.txt
View Code

使用-R 引數遞迴授權許可權案例

[root@web01 data]# ll dir2/
總用量 0
-rw-r--r-- 1 root root 0 1月  19 17:15 file5.txt
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh00
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh01
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh02
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh03
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh04
-rw-r--r-- 1 root root 0 1月  20 20:33 yhh05
[root@web01 data]# chmod -R  777 dir2/
[root@web01 data]# ll dir2/
總用量 0
-rwxrwxrwx 1 root root 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh05
View Code

1.14 chgrp:更改檔案使用者組

【功能說明】

命令chgrp只用於更改檔案的使用者,功能被chown取代了,瞭解一下即可

【語法格式】

 chgrp         [OPTION]...         GROUP         FILE...
 chgrp         [選項]...             使用者組        <檔案或目錄>...

【選項說明】

引數選項

解釋說明(帶*的為重點)

-R

遞迴更改目錄的使用者和使用者組(*

【使用範例】

[root@web01 data]# ll file5.txt
-rw-r--r-- 1 root root 0 1月  19 16:32 file5.txt
[root@web01 data]# chgrp yhh file5.txt
[root@web01 data]# ll file5.txt
-rw-r--r-- 1 root yhh 0 1月  19 16:32 file5.txt
View Code

遞迴修改檔案

[root@web01 data]# ll dir2
總用量 0
-rwxrwxrwx 1 root root 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root root 0 1月  20 20:33 yhh05
[root@web01 data]# chgrp -R yhh dir2/
[root@web01 data]# ll dir2
總用量 0
-rwxrwxrwx 1 root yhh 0 1月  19 17:15 file5.txt
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh00
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh01
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh02
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh03
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh04
-rwxrwxrwx 1 root yhh 0 1月  20 20:33 yhh05
View Code

1.15 umask:顯示或設定許可權密碼

【功能說明】

命令umask 通過八進位制的數值來定義使用者建立或目錄的預設許可權

【語法格式】

umask         [option]   [mode]
umask         [選項]    [模式]

引數選項

解釋說明(帶*的為重點)

-P

輸出的許可權掩碼可直接作為命令執行

-S

以字元方式輸出預設掩碼

【許可權計算】

假設umask的值為:022

6    6    6            #檔案的起始許可權
0    2    2 -            #umask的值
-------------------
6    4    4

假設umask的值為:045(其他使用者組位奇數)

6    6    6            #檔案的起始許可權
0    4    5 -            #umask的值
-------------------
6    2    1
0    0     1 +
-----------------------
6    2    2             #真是檔案許可權

目錄許可權計算(沒有奇偶之分)

7    7    7            #檔案的起始許可權
0    2    2 -            #umask的值
-------------------
7    5    5

【使用範例】

系統umask預設值

[root@web01 ~]# umask
0022
[root@web01 ~]# su yhh
[yhh@web01 root]$ umask
0002

引數-p-S的作用

[root@web01 ~]# umask -p
umask 0022
[root@web01 ~]# umask -S
u=rwx,g=rx,o=rx

驗證修改umaks值對檔案許可權的影響

[root@web01 ~]# umask
0022
[root@web01 ~]# touch file1;ll -h file1
-rw-r--r-- 1 root root 0 1月  20 21:22 file1
[root@web01 ~]# umask 044
[root@web01 ~]# touch file2;ll -h file2
-rw--w--w- 1 root root 0 1月  20 21:22 file2
[root@web01 ~]# umask 034
[root@web01 ~]# touch file3;ll -h file3
-rw-r---w- 1 root root 0 1月  20 21:23 file3
View Code

驗證修改umask永久生效

[root@web01 ~]# umask
0022
drwxr-xr-x 3 root root 4096 1月  18 12:16 yan
[root@web01 ~]# mkdir yanhuihuang;ll -d yanhuihuang
drwxr-xr-x 2 root root 4096 1月  20 21:25 yanhuihuang
[root@web01 ~]# umask 044
[root@web01 ~]# mkdir yanhuihuang1;ll -d yanhuihuang1
drwx-wx-wx 2 root root 4096 1月  20 21:25 yanhuihuang1
[root@web01 ~]# umask 055
[root@web01 ~]# mkdir yanhuihuang2;ll -d yanhuihuang2 
drwx-w--w- 2 root root 4096 1月  20 21:25 yanhuihuang2
View Code

修改配置檔案使得umask永久生效

[root@web01 ~]# sed -n '61,69p' /etc/bashrc
    # By default, we want umask to get set. This sets it for non-login shell.
    # Current threshold for system reserved uid/gids is 200
    # You could check uidgid reservation validity in
    # /usr/share/doc/setup-*/uidgid file
    if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
       umask 002
    else
       umask 022
Fi
[root@web01 ~]# sed -n '57,64p' /etc/profile
# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
View Code

1.16 從畏手畏腳到力挽狂瀾的心法

執著:屢敗屢戰,不死不休。

專注:只有它,無她

自信:我就是未來的linux大牛

心態:空杯心態,基礎不牢,地動山搖