1.which - shows the full path of (shell) commandsshell
which 命令用於查找並顯示給定命令的絕對路徑,環境變量PATH中保存了查找命令時須要遍歷的目錄, which 命令會在環境變量$PATH 設置的目錄裏查找符合條件的文件。也就是說,使用which命令就能夠看到某個系統指令是否存在,以及執行的命令位置。less
[root@localhost tmp]# which date #列出找到的第一個結果編輯器
[root@localhost tmp]# which -a date #列出所有結果ui
1.whereis - locate the binary, source, and manual page files for視頻
[root@localhost tmp]# whereis date
date: /usr/bin/date /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz
[root@localhost tmp]# whereis -l #顯示whereis查找的位置
[root@localhost tmp]# whereis -b date #只顯示二進制文件
[root@localhost tmp]# whereis -m date #只顯示幫助文檔
date: /usr/share/man/man1/date.1.gz /usr/share/man/man1p/date.1p.gz
locate(locate) 命令用來查找文件或目錄。 locate命令要比find快得多,緣由在於它不搜索具體目錄,而是搜索一個數據庫/var/lib/mlocate/mlocate.db 。這個數據庫中含有本地全部文件信息。Linux系統自動建立這個數據庫,而且天天自動更新一次,所以,咱們在用whereis和locate 查找文件時,有時會找到已經被刪除的數據,或者剛剛創建文件,卻沒法查找到,緣由就是由於數據庫文件沒有被更新。爲了不這種狀況,能夠在使用locate以前,先使用updatedb命令,手動更新數據庫。整個locate工做實際上是由四部分組成的:
l /usr/bin/updatedb 主要用來更新數據庫,經過crontab自動完成的
l /etc/updatedb.conf updatedb的配置文件
l /var/lib/mlocate/mlocate.db 存放文件信息的文件
[root@localhost tmp]# locate date
[root@localhost tmp]# locate -c date #只顯示結果數量
[root@localhost tmp]# locate -S #顯示locate數據庫信息
Database /var/lib/mlocate/mlocate.db:
3,412,156 bytes used to store database
[root@localhost tmp]# locate -l 5 date #只顯示前5條結果
/boot/grub2/i386-pc/datehook.mod
/boot/grub2/i386-pc/datetime.mod
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
[root@localhost tmp]# updatedb
[root@localhost ~]# touch aaaaaa #新建文件
[root@localhost ~]# locate aaaaaa #找不到
[root@localhost ~]# updatedb #更新數據庫
[root@localhost ~]# locate aaaaaa #找到了
(1)與時間有關的用法:共有 -atime, -ctime與 -mtime,以 -mtime說明
-mtime n :n 爲數字,意義爲在 n 天以前的『一天以內』被更動過內容的檔案;
-mtime +n :列出在 n 天以前(不含 n 天自己)被更動過內容的檔案檔名;
-mtime -n :列出在 n 天以內(含 n 天自己)被更動過內容的檔案檔名。
-newer file :file 爲一個存在的檔案,列出比 file 還要新的檔案檔名
[root@localhost tmp]# touch xxxxx #新建立一個文件
[root@localhost tmp]# find /tmp/ -mtime -1
[root@localhost tmp]# find /tmp/ -mtime +1
[root@localhost tmp]# timedatectl set-time "2020-3-18 19:56:30"
#修改系統時間
[root@localhost tmp]# date #當前時間爲「明天」
[root@localhost tmp]# find . -mtime 1
[root@localhost tmp]# touch xxx
[root@localhost tmp]# find . -newer xxxxx
l -uid n :n 爲數字,這個數字是用戶的帳號 ID,亦即 UID ,這個 UID 是記錄在 /etc/passwd 裏面與帳號名稱對應的數字。
l -gid n :n 爲數字,這個數字是組名的 ID,亦即 GID,這個 GID 記錄在 /etc/group
l -user name :name 爲使用者帳號名稱喔!例如dmtsai
l -group name:name 爲組名喔,例如 users ;
l -nouser:尋找檔案的擁有者不存在 /etc/passwd 的人!
l -nogroup:尋找檔案的擁有羣組不存在於 /etc/group 的檔案!
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw-r--. 1 stu stu 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[root@localhost tmp]# find . -uid 1000
#在當前目錄下查找uid爲1000的文件
[root@localhost tmp]# find . -gid 1000
#在當前目錄下查找gid爲1000的文件
[root@localhost tmp]# find . -user calf
#在當前目錄下查找用戶名爲calf的文件
[root@localhost tmp]# find . -group calf
#在當前目錄下查找組名爲calf的文件
[root@localhost tmp]# userdel stu
#刪除用戶和用戶組stu
[root@localhost tmp]# find . -nouser
#在當前目錄下查找無主文件
[root@localhost tmp]# find . -nogroup
#在當前目錄下查找無組文件
-size [+-]SIZE:搜尋比 SIZE 還要大(+)或小(-)的檔案。
`k' for Kilobytes (units of 1024 bytes)
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw-r--. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[root@localhost tmp]# find . -size 2003c
#查找大小爲2003bytes的文件
[root@localhost tmp]# find . -size -100c
#查找小於100bytes的文件
[root@localhost tmp]# find . -size +100c
#查找大於100bytes的文件
[root@localhost tmp]# find . -empty #查找空文件
-type TYPE:搜尋檔案的類型爲 TYPE 的,類型主要有:通常正規檔案 (f), 裝置檔案 (b, c),目錄 (d), 連結檔 (l), socket (s), 及 FIFO (p) 等屬性。
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
drwxr-xr-x. 2 root root 6 Mar 18 20:27 dir
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
[root@localhost tmp]# find . -type f
[root@localhost tmp]# find . -type d
l -perm mode :搜尋檔案權限『恰好等於』 mode 的檔案!
l -perm -mode :搜尋檔案權限『必需要所有囊括 mode 的權限』的檔案,舉例來講,咱們要搜尋 -rwxr--r-- ,亦即 0744 的檔案,使用 -perm -0744,當一個檔案的權限爲 -rwsr-xr-x ,亦即 4755 時,也會被列出來,由於 -rwsr-xr-x 的屬性已經囊括了 -rwxr--r-- 的屬性了。
l -perm /mode :搜尋檔案權限『包含任一 mode 的權限』的檔案,舉例來講,咱們搜尋-rwxr-xr-x ,亦即 -perm /755 時,但一個文件屬性爲 -rw-------也會被列出來,由於他有 -rw.... 的屬性存在!
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw----. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[root@localhost tmp]# find . -perm 664
#權限爲664便可
[root@localhost tmp]# find . -perm -664
#權限大於664便可
[root@localhost tmp]# find . -perm -020
#同組用戶可寫便可
[root@localhost tmp]# find . -perm -030
#要求同組用戶可寫可執行
[root@localhost tmp]# find . -perm /040
#同組用戶可讀便可
[root@localhost tmp]# find . -perm /030
#同組用戶可寫或者可執行便可
[root@localhost tmp]# find . -perm /007
#其餘用戶可讀或者可寫或者可執行便可
-name filename:搜尋文件名爲 filename 的檔案;
[root@localhost tmp]# find . -name calf #查找名爲calf的文件
[root@localhost tmp]# find . -name c* #查找以c開頭的文件
[root@localhost tmp]# find . -name "????"
#查找名字爲4個字符的文件
[root@localhost tmp]# find . -name "*" #查找全部文件
[root@localhost tmp]# find . -name "*oo*"
#查找名字中含有oo的文件
l -exec command :command 爲其餘指令,-exec 後面可再接額外的指令來處理搜尋到的結果。
l -print :將結果打印到屏幕上,這個動做是預設動做!
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 calf
-rw-r--r--. 1 root root 2003 Mar 12 10:59 passwd
-rwxrw-r--. 1 root root 0 Mar 5 10:32 root
-rw-rw----. 1 1001 1001 0 Mar 18 20:10 stu
-rw-r--r--. 1 root root 65 Mar 12 11:04 test
[root@localhost tmp]# find . -name calf -exec ls -l {} \;
-rw-rw-r--. 1 calf calf 0 Mar 18 20:06 ./calf
[root@localhost tmp]# find . -nouser -exec rm -f {} \;
n 額外需求:若是用戶想要在這個目錄內利用 ls 查閱文件名,則用戶對此目錄還須要 r 的權限。
n 可以使用的指令:例如本章談到的 cat, more, less等等
n 檔案所需權限:使用者對檔案至少須要具備 r 的權限才行!
n 可以使用的指令:例如 nano 或將來要介紹的 vi 編輯器等;
n 目錄所需權限:用戶在該檔案所在的目錄至少要有 x 權限;
n 目錄所需權限:用戶在該目錄要具備w,x的權限,重點在 w 啦!
5.讓用戶進入某目錄並執行該目錄下的某個指令之基本權限爲什麼?
l 絕對路徑:『必定由根目錄 / 寫起』;相對路徑:『不禁 / 寫起,而是由相對當前目錄寫起』
l 特殊目錄有:., .., -, ~, ~account須要注意;
l 與目錄相關的指令有:cd, mkdir, rmdir, pwd 等重要指令;
l 用戶能使用的指令是依據 PATH 變量所規定的目錄去搜尋的;
l ls 能夠檢視檔案的屬性,尤爲 -d, -a, -l 等選項特別重要!
l 檔案的複製、刪除、移動能夠分別使用:cp, rm , mv等指令來操做;
l 檢查檔案的內容(讀文件)可以使用的指令包括有:cat, tac, nl, more, less, head, tail, od 等
l touch 的目的在修改檔案的時間參數,但亦可用來創建空檔案;
l 一個檔案記錄的時間參數有三種,分別是 access time(atime), status time (ctime), modification time(mtime),ls 默認顯示的是 mtime。
l 檔案具備SUID的特殊權限時,表明當用戶執行此一binary程序時,在執行過程當中用戶會暫時具備程序擁有者的權限。
l 目錄具備SGID的特殊權限時,表明用戶在這個目錄底下新建的檔案之羣組都會與該目錄的組名相同。