Linux中檔案、目錄的預設許可權umask命令、查詢命令、壓縮命令
主要內容:
1.檔案和目錄的許可權
2.檔案和目錄的預設許可權和umask
3.Linux查詢及壓縮
1.檔案和目錄的許可權
許可權對於檔案:
r(read):可以獲取檔案的內容
w(write):可以編輯、新增或修改檔案的內容(不包括刪除該檔案)
x(excute):該檔案具有被系統執行的許可權。
許可權對於目錄:
r(read contents in directory):表示具有讀取目錄結構列表的許可權,即可使用ls命令將該目錄的內容列表顯示出來
w(modify contents of directory):
也就是在當前目錄下,可以有如下許可權:
新建新的檔案或目錄;
刪除已有目錄或檔案;
將已有目錄或檔案重新命名;
轉移該目錄內的檔案或目錄的位置;
x(access directory):目錄的可執行許可權代表使用者是否能夠進入該目錄,也就是cd進入該目錄的許可權。
總結一下:
x cd
rx cd ls
wx cd touch rm vi mv
2.檔案和目錄的預設許可權和umask
[[email protected] ~]# umask //root使用者的umask值uma
0022 //與一般許可權有關的是後邊3個數字
[[email protected] ~]$ umask //dmtsai使用者的umask值
0002
解釋:
若使用者建立“檔案”則預設沒有可執行(x)許可權,即只有r、w這兩個許可權,也就是說最大為666,預設許可權為如:-rw-rw-rw-
若使用者建立“目錄”,則由於x與是否進入此目錄有關,因此預設為許可權均開放,極為777,預設許可權為:drwxrwxrwxuma
umask的分數是指“預設值需要減掉的許可權”。因為r,w,x分別是4,2,1,也就是說,當要拿掉寫的許可權,就是輸入2,而如果拿掉讀的許可權,也就是4,那麼拿掉讀寫的許可權,也就是6,而要拿掉執行和寫入的許可權,也就是3。
新建檔案時(假設umask為022):
(-rw-rw-rw-)-(-----w--w-)===>-rw-r--r—
新建資料夾時(假設umask為022):
(drwxrwxrwx)-(d----w--w-)===>drwxr-xr-x
[[email protected] teach]# umask
0022
[[email protected] teach]# touch file
[[email protected] teach]# mkdir dir
[[email protected] teach]# ll
total 0
drwxr-xr-x 2 root root 6 Nov 8 15:24 dir //(d rwx rwx rwx)-(d --- -w- -w-)===>d rwx r-x r-x
-rw-r--r-- 1 root root 0 Nov 8 15:24 file //(- rw- rw- rw-)-(- --- -w- -w-)===>- rw- r-- r--
3.Linux查詢及壓縮
(1)which
(2)whereis
(3)locate
(4)find
(5)grep
(6)gzip
(7)bzip2
(8)tar
(1)which [命令名] 查詢可執行檔案的位置,但是隻能查詢環境變數下的命令
#只搜尋PATH環境變數裡規定的目錄,查詢可執行檔案
如:which ls
如:which ifconfig
如:which X
/usr/bin/which: no X in (/usr/lib/jvm/jdk1.8.0_141/bin:/usr/lib/jvm/jdk1.8.0_141/jre/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/var/local/apache-maven-3.2.2/bin:/root/hadoop-2.7.6/bin:/root/hadoop-2.7.6/sbin:/root/bin)
(2)whereis [選項] [命令名]
#搜尋命令所在路徑及幫助文件所在的位置
選項:
-b:只查詢可執行檔案
-m:只查詢幫助檔案
如:whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz //man目錄下的都是幫助文件
(3)locate [選項] [檔名]
#查詢檔案所在位置
選項:
-i:忽略大小寫差異
-r:後邊可以接正則表示式
locate命令查詢檔案是由已建立的資料庫/var/lib/mlocate裡面的資料查詢到的,不用直接去硬碟中訪問資料,所以查詢速度很快。
centos7中預設沒有locate命令,所以需要自行安裝:
wget epel url下載 修改yum的repos
yum search locate
yum -y install mlocate
updatedb 更新資料庫以後才能查找出來。 只有root使用者才可以執行該操作
[[email protected] ~]# locate jdk-8u141-linux-x64.tar.gz
/root/jdk-8u141-linux-x64.tar.gz
[[email protected] ~]# touch qlf //新建一個檔案,名為qlf
[[email protected] ~]# locate qlf //新建的檔案,找不到
[[email protected] ~]# updatedb //需要更新資料庫,使用updatedb命令
[[email protected] ~]# locate qlf //更新資料庫後能找到檔案qlf的位置
/root/qlf
(4)find [搜尋範圍] [搜尋條件]
#find的命令搜尋速度較慢,因為它是搜尋整個硬碟
如:find / -name original-ks.cfg
●find命令與時間有關的引數:
如:find /var/log/ -mtime +10 //查詢10天前修改的檔案
-atime 檔案訪問時間 access
-ctime 改變檔案屬性 change
-mtime 修改檔案內容 modify
-10 10天內修改檔案
10 10天當天修改的檔案
+10 10天前修改的檔案
●find命令與使用者及使用者組的引數:
-user name
-group name等
如:find -user root //查詢所有者為root的檔案
●find命令其它可進行的操作。
對查找出來的檔案可以進行一些特定的操作:
如:find / -name "*.tar.gz" -exec ls -l {} \;
在/下查詢檔名字為.tar.gz的檔案 並給這些檔案執行命令 ls -l
-rw-r--r-- 1 root root 185516505 Nov 6 10:11 /root/jdk-8u141-linux-x64.tar.gz
-rw-r--r-- 1 root root 216745683 Apr 18 2018 /root/hadoop-2.7.6.tar.gz
-rw-r--r-- 1 20415 101 391853 Apr 18 2018 /root/hadoop-2.7.6/share/hadoop/kms/tomcat/bin/tomcat-native.tar.gz
●find命令與檔案許可權及檔名有關的引數
如:find /root –name “install.log*”
如:find /root –name “ab[cd]” //搜尋檔名叫abc或者叫abd的檔案
Linux的萬用字元:
- 匹配任意內容
? 匹配任意一個字元
[] 匹配任意一箇中括號內的字元
如:
find . -size 25k //查詢檔案大小是25KB的檔案
-25k 小於25KB的檔案
25k 等於25KB的檔案
+25k 大於25KB的檔案
如:find /etc -size +20k -a -size -50k //查詢/etc目錄下,大於20KB並且小於50KB的檔案
-a and 邏輯與,兩個條件都滿足
-o or 邏輯或,兩個條件滿足一個即可
(5)grep命令
grep [選項] 字串 檔名
#在檔案中匹配符合條件的字串 僅僅在檔案裡面的查詢
選項:
-i 忽略大小寫
-v 排除制定字串
如:grep "yes" original-ks.cfg //在檔案中搜索包含yes字串的行
(6)gzip [選項] 檔名
#只能用來壓縮檔案,不能用來壓縮目錄 壓縮以後原始檔消失。強行壓縮目錄時,只是將目錄下面的所有檔案壓縮,不會對目錄進行壓縮。
字尾名為gz
選項:
-c:將壓縮的資料輸出到螢幕上,保留原始檔案
-d:解壓
-v:顯示原檔案/壓縮檔案的壓縮比例等資訊
-r:將目錄下的每個檔案都分別壓縮
如:gzip -v testfile //壓縮testfile檔案
如:gzip -d testfile.gz //解壓testfile.gz
如:gzip -c words > words.gz //壓縮words檔案後,保留原檔案
如:gzip -r teach //該目錄下的所有檔案將會被分別壓縮,該目錄下的子目錄不會被壓縮
如:gzip -dr teach //該目錄下的所有檔案將會被解壓。
(7)bzip2命令和gzip命令用法幾乎相同,bzip2的壓縮比更佳。請同學們自行學習。字尾名為bz2
(8)tar
#打包命令
-c:新建打包檔案
-v:在解壓/解壓縮的處理的過程中,將正在處理的檔名顯示出來
-f filename:-f後邊要跟一個被處理的檔名
-x:解打包或解壓縮的功能,可與-C(大寫)在特定目錄中解開
-z:通過對gzip的支援進行壓縮/解壓縮,此時檔名為最好為*.tar.gz
如:tar -cvf data.tar ./data/ //在當前目錄下新建一個包名為data.tar,將data目錄進行打包
如:tar -xvf data.tar -C ./teach/ //將data.tar解壓到./teach目錄中去
如:tar -zcvf data.tar.gz data //將data目錄打包並壓縮成.tar.gz檔案
如:tar -zxvf data.tar.gz
小提示:
cvf xvf 常作為組合使用。若再壓縮為gz包,則再分別加上z引數。
(9)zip入門
如:zip a.zip /etc/passwd /etc/shadow //將/etc/passwd和/etc/shadow檔案新增到a.zip壓縮包中
如:unzip a.zip
練習:
練習1:使用root使用者在/tmp目錄下新建目錄testing和檔案testing/file,給testing目錄賦許可權為744(chmod 744 testing),給testing/file檔案賦許可權為600(chmod 600 testing/file)。編輯file內容為:content,儲存退出,新建一個使用者為shrek,檢視shrek使用者是否能夠開啟testing目錄。若不能,讓shrek使用者擁有testing目錄(chown shrek /tmp/testing),再次檢視shrek使用者是否能夠進入目錄且編輯、刪除檔案file。
cd /tmp
mkdir testing
chmod 744 testing
touch testing/file
vi file
chmod 600 testing/file
ls -ald testing testing/file
練習2:假設你的umask為003,請問該umask情況下,新建的檔案和資料夾許可權是什麼?
練習3:練習使用gzip對檔案進行壓縮、解壓縮。
練習4:練習使用tar命令對目錄進行打包並壓縮和解包、解壓縮。
參考答案:
練習1:
[[email protected] tmp]# mkdir testing
[[email protected] tmp]# chmod 744 testing
[[email protected] tmp]# touch testing/file
[[email protected] tmp]# ls -ald testing testing/file
drwxr--r-- 2 root root 18 Nov 8 16:00 testing
-rw-r--r-- 1 root root 0 Nov 8 16:00 testing/file
[[email protected] tmp]# cd testing/
[[email protected] testing]# vi file
[[email protected] testing]# cd ..
[[email protected] tmp]# su - shrek //切換使用者
[shrek]@qin-1 tmp]$ cd testing/
-bash: cd: testing/: Permission denied //不能開啟,因為shrek不在root組內,且shrek只有r許可權,沒有x許可權,所以不能開啟
[[email protected] tmp]$ exit
logout
[[email protected] tmp]# chown dmtsai testing/ //shrek為檔案的擁有者後對testing資料夾的許可權是rwx
[[email protected] tmp]# su - shrek
Last login: Thu Nov 8 14:18:12 CST 2018 on pts/1
[[email protected] ~]$ cd /tmp
[[email protected] tmp]$ cd testing/
[[email protected] testing]$ ls
file
[[email protected] testing]$ rm -rf file
練習2:
umask為003,所以去掉的許可權為(- --- --- -wx)
檔案:(- rw- rw- rw-)-(- --- --- -wx)===>- rw- rw- r--
目錄:(d rwx rwx rwx)-(- --- --- -wx)===>d rwx rwx r--