3.文件管理
本章同步視頻:https://edu.51cto.com/sd/e4874
3.4 文件查找
3.4.1 命令查找
1.which - shows the full path of (shell) commands
(1)which 說明
which 命令用於查找並顯示給定命令的絕對路徑,環境變量 PATH 中保
存了查找命令時須要遍歷的目錄, which 命令會在環境變量$PATH 設
置的目錄裏查找符合條件的文件。也就是說,使用 which 命令就能夠看
到某個系統指令是否存在,以及執行的命令位置。
(2)which 使用
[root@localhost tmp]# which date #列出找到的第一個結果
/bin/date
[root@localhost tmp]# which -a date #列出所有結果
/bin/date
/usr/bin/date
3.4.2 文檔查找
1.whereis - locate the binary, source, and manual page files for
a command
[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 #只顯示二進制文件
date: /usr/bin/date
[root@localhost tmp]# whereis -m date #只顯示幫助文檔
date: /usr/share/man/man1/date.1.gz
/usr/share/man/man1p/date.1p.gz
2.locate - find files by name
(1)locate 說明
locate(locate) 命令用來查找文件或目錄。 locate 命令要比 find 快得
多,緣由在於它不搜索具體目錄,而是搜索一個數據庫
/var/lib/mlocate/mlocate.db 。這個數據庫中含有本地全部文件信息。
Linux 系統自動建立這個數據庫,而且天天自動更新一次,所以,咱們
在用 whereis 和 locate 查找文件時,有時會找到已經被刪除的數據,
或者剛剛創建文件,卻沒法查找到,緣由就是由於數據庫文件沒有被更
新。爲了不這種狀況,能夠在使用 locate 以前,先使用 updatedb
命令,手動更新數據庫。整個 locate 工做實際上是由四部分組成的:
⚫ /usr/bin/updatedb 主要用來更新數據庫,經過 crontab 自動完成的
⚫ /usr/bin/locate 查詢文件位置
⚫ /etc/updatedb.conf updatedb 的配置文件
⚫ /var/lib/mlocate/mlocate.db 存放文件信息的文件
(2)locate 用法
[root@localhost tmp]# locate date
[root@localhost tmp]# locate -c date #只顯示結果數量
1643
[root@localhost tmp]# locate -S #顯示 locate 數據庫信息
Database /var/lib/mlocate/mlocate.db:
13,931 directories
152,748 files
7,576,250 bytes in file names
3,412,156 bytes used to store database
[root@localhost tmp]# locate -l 5 date #只顯示前 5 條結果
/boot/grub2/i386-pc/date.mod
/boot/grub2/i386-pc/datehook.mod
/boot/grub2/i386-pc/datetime.mod
/etc/updatedb.conf
/etc/dbus-1/system.d/org.freedesktop.timedate1.conf
(3)更新 locate 的數據庫
[root@localhost tmp]# updatedb
(4)缺陷
[root@localhost ~]# touch aaaaaa #新建文件
[root@localhost ~]# locate aaaaaa #找不到
[root@localhost ~]# updatedb #更新數據庫
[root@localhost ~]# locate aaaaaa #找到了
/root/aaaaaa
3.4.3 find - search for files in a directory hierarchy
1.按時間查找
(1)與時間有關的用法:共有 -atime, -ctime 與 -mtime,以 -mtime 說
明
-mtime n :n 爲數字,意義爲在 n 天以前的『一天以內』被更動過內
容的檔案;
-mtime +n :列出在 n 天以前(不含 n 天自己)被更動過內容的檔案檔名;
-mtime -n :列出在 n 天以內(含 n 天自己)被更動過內容的檔案檔名。
-newer file :file 爲一個存在的檔案,列出比 file 還要新的檔案檔名
(2)用法
[root@localhost tmp]# touch xxxxx #新建立一個文件
[root@localhost tmp]# find /tmp/ -mtime -1
/tmp/
/tmp/xxxxx
#查找/tmp 目錄下 1 天內修改過的文件
[root@localhost tmp]# ls
passwd root test xxxxx
[root@localhost tmp]# find /tmp/ -mtime +1
/tmp/root #結果已去掉隱藏文件
/tmp/test
/tmp/passwd
#查找/tmp 目錄下 1 天前修改過的文件
[root@localhost tmp]# timedatectl set-time "2020-3-18 19:56:30"
#修改系統時間
[root@localhost tmp]# date #當前時間爲「明天」
Wed Mar 18 19:56:33 CST 2020
[root@localhost tmp]# find . -mtime 1
.
./xxxxx
#查找在當前目錄下昨天修改過的文件
[root@localhost tmp]# touch xxx
[root@localhost tmp]# find . -newer xxxxx
.
./xxx
#查找在當前目錄下比 xxxxx 要新的文件
2.按用戶查找
(1)與使用者或組名有關的參數:
⚫ -uid n :n 爲數字,這個數字是用戶的帳號 ID,亦即 UID ,這個
UID 是記錄在 /etc/passwd 裏面與帳號名稱對應的數字。
⚫ -gid n :n 爲數字,這個數字是組名的 ID,亦即 GID,這個 GID
記錄在 /etc/group
⚫ -user name :name 爲使用者帳號名稱喔!例如 dmtsai
⚫ -group name:name 爲組名喔,例如 users ;
⚫ -nouser:尋找檔案的擁有者不存在 /etc/passwd 的人!
⚫ -nogroup:尋找檔案的擁有羣組不存在於 /etc/group 的檔案!
(2)用法
[root@localhost tmp]# ll
total 8
-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 的文件
./calf
[root@localhost tmp]# find . -gid 1000
#在當前目錄下查找 gid 爲 1000 的文件
./calf
[root@localhost tmp]# find . -user calf
#在當前目錄下查找用戶名爲 calf 的文件
./calf
[root@localhost tmp]# find . -group calf
#在當前目錄下查找組名爲 calf 的文件
./calf
[root@localhost tmp]# userdel stu
#刪除用戶和用戶組 stu
[root@localhost tmp]# find . -nouser
#在當前目錄下查找無主文件
./stu
[root@localhost tmp]# find . -nogroup
#在當前目錄下查找無組文件
./stu
3.按文件大小查找
(1) 按文件大小查找
-size [+-]SIZE:搜尋比 SIZE 還要大(+)或小(-)的檔案。c' for bytes<br/>
w' for two-byte wordsk' for Kilobytes (units of 1024 bytes)<br/>
M' for Megabytes
`G' for Gigabytes
-empty 查找空文件
(2)用法
[root@localhost tmp]# ll
total 8
-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 的文件
./passwd
[root@localhost tmp]# find . -size -100c
#查找小於 100bytes 的文件
./root
./test
./calf
./stu
[root@localhost tmp]# find . -size +100c
#查找大於 100bytes 的文件
.
./passwd
[root@localhost tmp]# find . -empty #查找空文件
./root
./calf
./stu
#注:以上答案均已去除隱藏文件。
4.按文件類型查找
(1)按文件類型查找
-type TYPE:搜尋檔案的類型爲 TYPE 的,類型主要有:通常正規檔案
(f), 裝置檔案 (b, c),目錄 (d), 連結檔 (l), socket (s), 及 FIFO (p) 等屬性。
(2)用法
[root@localhost tmp]# ll
total 8
-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
./passwd
./calf
[root@localhost tmp]# find . -type d
.
./dir
5.按權限查找
(1)按文件權限查找
⚫ -perm mode :搜尋檔案權限『恰好等於』 mode 的檔案!
⚫ -perm -mode :搜尋檔案權限『必需要所有囊括 mode 的權限』的檔
案,舉例來講,咱們要搜尋 -rwxr--r-- ,亦即 0744 的檔案,使用 -
perm -0744,當一個檔案的權限爲 -rwsr-xr-x ,亦即 4755 時,也會
被列出來,由於 -rwsr-xr-x 的屬性已經囊括了 -rwxr--r-- 的屬性了。
⚫ -perm /mode :搜尋檔案權限『包含任一 mode 的權限』的檔案,舉
例來講,咱們搜尋-rwxr-xr-x ,亦即 -perm /755 時,但一個文件屬性
爲 -rw-------也會被列出來,由於他有 -rw.... 的屬性存在!
(2)用法
[root@localhost tmp]# ll
total 8
-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 便可
./calf
./stu
[root@localhost tmp]# find . -perm -664
#權限大於 664 便可
.
./root
./calf
./stu
[root@localhost tmp]# find . -perm -020
#同組用戶可寫便可
./root
./calf
./stu
[root@localhost tmp]# find . -perm -030
#要求同組用戶可寫可執行
[root@localhost tmp]# find . -perm /040
#同組用戶可讀便可
./root
./test
./passwd
./calf
./stu
[root@localhost tmp]# find . -perm /030
#同組用戶可寫或者可執行便可
./root
./calf
./stu
[root@localhost tmp]# find . -perm /007
#其餘用戶可讀或者可寫或者可執行便可
./root
./test
./passwd
./calf
6.按文件名查找
(1)按文件名查找
-name filename:搜尋文件名爲 filename 的檔案;
(2)用法
[root@localhost tmp]# ls
calf passwd root stu test
[root@localhost tmp]# find . -name calf #查找名爲 calf 的文件
./calf
[root@localhost tmp]# find . -name c #查找以 c 開頭的文件
./calf
[root@localhost tmp]# find . -name "????"
#查找名字爲 4 個字符的文件
./root
./test
./calf
[root@localhost tmp]# find . -name "" #查找全部文件
.
./root
./test
./passwd
./calf
./stu
[root@localhost tmp]# find . -name "oo"
./root
#查找名字中含有 oo 的文件
7.對查找出來的文件作二次處理
(1)二次處理
⚫ -exec command :command 爲其餘指令,-exec 後面可再接額外的
指令來處理搜尋到的結果。
⚫ -print :將結果打印到屏幕上,這個動做是預設動做!
(2)用法
[root@localhost tmp]# ll
total 8
-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
#查找 calf,而後列出詳細信息
[root@localhost tmp]# find . -nouser -exec rm -f {} \;
#查找孤兒文件,而後刪除
[root@localhost tmp]# ls
calf passwd root test
3.5 權限與指令間的關係
1.讓用戶能進入某目錄成爲『可工做目錄』的基本權限爲什麼:
◼ 可以使用的指令:例如 cd 等變換工做目錄的指令;
◼ 目錄所需權限:用戶對這個目錄至少須要具備 x 的權限
◼ 額外需求:若是用戶想要在這個目錄內利用 ls 查閱文件名,則用戶對此目錄還需
要 r 的權限。
2.用戶在某個目錄內讀取一個檔案的基本權限爲什麼?
◼ 可以使用的指令:例如本章談到的 cat, more, less 等等
◼ 目錄所需權限:用戶對這個目錄至少須要具備 x 權限;
◼ 檔案所需權限:使用者對檔案至少須要具備 r 的權限才行!
3.讓使用者能夠修改一個檔案的基本權限爲什麼?
◼ 可以使用的指令:例如 nano 或將來要介紹的 vi 編輯器等;
◼ 目錄所需權限:用戶在該檔案所在的目錄至少要有 x 權限;
◼ 檔案所需權限:使用者對該檔案至少要有 r, w 權限
4.讓一個使用者能夠創建一個檔案的基本權限爲什麼?
◼ 目錄所需權限:用戶在該目錄要具備 w,x 的權限,重點在 w 啦!
5.讓用戶進入某目錄並執行該目錄下的某個指令之基本權限爲什麼?
◼ 目錄所需權限:用戶在該目錄至少要有 x 的權限;
◼ 檔案所需權限:使用者在該檔案至少須要有 x 的權限shell