Linux-查找-find-whereis-which

3.文件管理

本章同步視頻:https://edu.51cto.com/sd/e4874


3.4 文件查找

3.4.1 命令查找

1.which - shows the full path of (shell) commandsshell

(1)which說明數據庫

which 命令用於查找並顯示給定命令的絕對路徑,環境變量PATH中保存了查找命令時須要遍歷的目錄, which 命令會在環境變量$PATH 設置的目錄裏查找符合條件的文件。也就是說,使用which命令就能夠看到某個系統指令是否存在,以及執行的命令位置。less

(2)which使用socket

[root@localhost tmp]# which date   #列出找到的第一個結果編輯器

/bin/dateide

[root@localhost tmp]# which -a date     #列出所有結果ui

/bin/datespa

/usr/bin/dateorm

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工做實際上是由四部分組成的:

l  /usr/bin/updatedb   主要用來更新數據庫,經過crontab自動完成的

l  /usr/bin/locate         查詢文件位置

l  /etc/updatedb.conf   updatedb的配置文件

l  /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 還要新的檔案檔名

                                              spacer.gif

(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)與使用者或組名有關的參數:

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 的檔案!

(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

`w'    for two-byte words

`k'    for Kilobytes (units of 1024 bytes)

`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)按文件權限查找

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.... 的屬性存在!

(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)二次處理

l  -exec command :command 爲其餘指令,-exec 後面可再接額外的指令來處理搜尋到的結果。

l  -print        :將結果打印到屏幕上,這個動做是預設動做!

image.png

(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.讓用戶能進入某目錄成爲『可工做目錄』的基本權限爲什麼:

n  可以使用的指令:例如 cd 等變換工做目錄的指令;

n  目錄所需權限:用戶對這個目錄至少須要具備 x 的權限

n  額外需求:若是用戶想要在這個目錄內利用 ls 查閱文件名,則用戶對此目錄還須要 r 的權限。

2.用戶在某個目錄內讀取一個檔案的基本權限爲什麼?

n  可以使用的指令:例如本章談到的 cat, more, less等等

n  目錄所需權限:用戶對這個目錄至少須要具備 x 權限;

n  檔案所需權限:使用者對檔案至少須要具備 r 的權限才行!

3.讓使用者能夠修改一個檔案的基本權限爲什麼?

n  可以使用的指令:例如 nano 或將來要介紹的 vi 編輯器等;

n  目錄所需權限:用戶在該檔案所在的目錄至少要有 x 權限;

n  檔案所需權限:使用者對該檔案至少要有 r, w 權限

4.讓一個使用者能夠創建一個檔案的基本權限爲什麼?

n  目錄所需權限:用戶在該目錄要具備w,x的權限,重點在 w 啦!

5.讓用戶進入某目錄並執行該目錄下的某個指令之基本權限爲什麼?

n  目錄所需權限:用戶在該目錄至少要有 x 的權限;

n  檔案所需權限:使用者在該檔案至少須要有 x 的權限

3.6 總結

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  觀察檔案的類型可使用 file 指令來觀察;

l  touch 的目的在修改檔案的時間參數,但亦可用來創建空檔案;

l  一個檔案記錄的時間參數有三種,分別是 access time(atime), status time (ctime), modification time(mtime),ls 默認顯示的是 mtime。

l  chattr與lsattr設定及觀察隱藏屬性。

l  檔案具備SUID的特殊權限時,表明當用戶執行此一binary程序時,在執行過程當中用戶會暫時具備程序擁有者的權限。

l  目錄具備SGID的特殊權限時,表明用戶在這個目錄底下新建的檔案之羣組都會與該目錄的組名相同。

l  目錄具備SBIT的特殊權限時,表明在該目錄下用戶創建的檔案只有本身與root可以刪除!

l利用 find 能夠加入許多選項來直接查詢文件系統,以得到本身想要知道的文檔。

相關文章
相關標籤/搜索