1. 程式人生 > 其它 >Linux 系統下查詢檔案命令

Linux 系統下查詢檔案命令

Linux 系統下查詢檔案命令

查命令絕對路徑: which用於查詢並顯示給定命令的絕對路徑,環境變數中PATH引數也可以被查出來。

[root@localhost ~]# which bash
/usr/bin/bash

[root@localhost ~]# which ls
alias ls='ls --color=auto'
    /usr/bin/ls

尋找特定檔案: whereis命令用來定位指令的二進位制程式、原始碼檔案和man手冊頁等相關檔案的路徑,該命令只能用於程式名的搜尋

[root@localhost ~]# whereis --help

語法格式:[ whereis [選項] 檔名 ]

        -b              #只找二進位制檔案
        -m              #只找man文件
        -s              #只找原始碼

使用 whereis -b 命令找二進位制檔案,與幫助手冊。

[root@localhost ~]# whereis -b ifconfig
ifconfig: /usr/sbin/ifconfig

[root@localhost ~]# whereis -m ifconfig
ifconfig: /usr/share/man/man8/ifconfig.8.gz

快取查詢檔案: locate 搜尋一個數據庫/var/lib/mlocatedb,這個資料庫中含有本地所有檔案資訊,Linux系統自動建立這個資料庫,並且每天自動更新一次,所以使用locate命令查不到最新變動過的檔案,為了避免這種情況,可以在使用locate之前,先使用updatedb命令,手動更新資料庫,updatedb命令會根據/etc/updatedb.conf來更新檔案.

[root@localhost ~]# yum install -y mlocate
[root@localhost ~]# locate --help

語法格式:[ locate [選項] 檔名 ]

        -d 目錄        #指定資料庫所在的目錄
        -i             #忽略大小寫差異
        -r             #後面接正則表示式

使用 locate 命令查詢一個檔案.

[root@localhost ~]# updatedb 
[root@localhost ~]# locate /etc/passwd
/etc/passwd
/etc/passwd-

遍歷檔案查詢: find 命令可以說是最重要的查詢命令了,該命令引數較多。

[root@localhost ~]# find --help

語法格式:[ find [目錄] [屬性] 檔名 ]

        -name         #按檔名查詢
        -size         #根據大小查詢
        -user         #根據屬主查詢
        -perm         #根據許可權查詢
        -type         #根據型別查詢
        -time         #按時間查詢
        -inum         #根據i節點查詢
        -exec         #查詢後執行命令

-name 按檔名查詢:

常用查詢萬用字元

\*     #匹配任意一個或多個字元
?     #匹配任意一個字元
[]     #指定範圍,外側加引號

查詢/var/目錄下,以.log結尾的檔案.

[root@localhost ~]# find /var/ -name "*.log"
/var/log/tuned/tuned.log
/var/log/audit/audit.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
....省略....

查詢/root/目錄下,以[1-3之間],結尾是.txt的檔案

[root@localhost ~]# ls
1.txt  2.txt  3.txt  Catalog  File

[root@localhost ~]# find /root/ -name "[1-3].txt"
/root/1.txt
/root/2.txt
/root/3.txt

查詢/etc/目錄下,開頭是6個任意字元的檔案

[root@localhost ~]# find /etc/ -name "??????"
/etc/grub.d
/etc/grub.d/README
/etc/shells
/etc/init.d
....省略....

-size 根據大小查詢

單位是 block 資料塊  一塊是512位元組
1M -> 1024k -> 2048 塊  (1塊是0.5k 也就是512位元組)
100M -> 102400k -> 204800塊

查詢/etc/目錄下,小於10k的檔案

root@localhost ~]# find /etc/ -size -10k
/etc/crypttab
/etc/.pwd.lock
/etc/environment
....省略....

查詢/etc/目錄下,大於1M的檔案

[root@localhost ~]# find /etc/ -size +1M   #查詢大於1M的檔案
/etc/udev/hwdb.bin
/etc/selinux/targeted/active/policy.kern
/etc/selinux/targeted/contexts/files/file_contexts.bin
/etc/selinux/targeted/policy/policy.31
....省略....

#注意:+-號如果沒有,是精確到這麼大,通常都會帶上+或-號表示一個範圍.

-user 根據屬主與許可權查詢

在/root目錄中查詢屬於wang使用者的檔案

[root@localhost ~]# find /root/ -user wang
/root/1.txt
/root/2.txt
/root/3.txt
#注意:系統中要存在該使用者,否則會報錯誤.

查詢/boot/目錄中許可權是644的檔案

[root@localhost ~]# find /boot/ -perm 0644
/boot/grub2/device.map
/boot/grub2/i386-pc/gcry_rmd160.mod
/boot/grub2/i386-pc/acpi.mod
/boot/grub2/i386-pc/gcry_rsa.mod
....省略....

-type 根據型別查詢

-type f 二進位制檔案(普通檔案)
-type l 軟連結檔案
-type d 目錄

查詢/usr/bin/目錄下,型別是二進位制檔案.

[root@localhost ~]# find /usr/bin/ -type f
/usr/bin/cp
/usr/bin/gzip
/usr/bin/alias
/usr/bin/csplit
/usr/bin/bash
....省略....

-time 按時間查詢

按天數   ctime  atime  mtime
按分鐘   cmin   amin     mmin

  c  change   #表示屬性被修改過:所有者、所屬組、許可權
  a  access   #被訪問過(被檢視過)
  m  modify   #表示內容被修改過

查詢/etc/目錄下,在120分鐘以內,內容被修改過的檔案

[root@localhost ~]# find /etc/ -mmin -120
/etc/
/etc/resolv.conf
/etc/group-
/etc/gshadow-
/etc/group
/etc/gshadow
....省略....

查詢/etc/目錄下,在7天之前,屬性被修改過的檔案

[root@localhost ~]# find /etc/ -ctime +7
/etc/resolv.conf
/etc/group-
/etc/gshadow-
....省略....

-inum 根據i節點查詢

有一些檔案的硬連結數量很多,有相同的i節點,查詢其中一個檔案的i節點號,一次性刪除。

[root@localhost ~]# find ./ -inum 1024 -exec rm{} \;   #刪除相同I節點的資料

-and or 邏輯連線符

-a    (and 邏輯與)     
-o    (or  邏輯或)

在/etc/目錄下,查詢大於1k,並且小於10k的檔案

[root@localhost ~]# find /etc/ -size +1k -a -size -10k
/etc/
/etc/grub.d/00_header
/etc/grub.d/20_ppc_terminfo
/etc/grub.d/00_tuned
/etc/rc.d/init.d/README
/etc/rc.d/init.d/netconsole
/etc/rc.d/init.d/network
/etc/pam.d
....省略....

-exec 命令執行連線符

[查詢格式] find  ...  -exec 命令 {}  \;

{}        #表示find查詢的結果集
\         #是轉義符,不使用命令別名,直接使用命令本身
;         #分號是表示語句的結束.

#注意:固定格式,只能這樣寫.注意中間的空格.

-----------------------------------------------------------------
說明: 轉義符的作用是什麼?

在linux中有一個別名機制,如rm刪除檔案,執行的卻是rm -i(用which rm 可以檢視命令別名),
使用rm刪除檔案前會提示,就是因為rm -i這個引數。如果想使用命令原意,可以在加\轉義,
如:\rm test.txt   則不會提示,直接刪除

查詢/var/log/目錄下名字以.log結尾的檔案,找到後執行 ls -l 顯示詳細資訊.

[root@localhost ~]# find /var/log/ *.log -exec ls -l {} \;
total 1176
drwxr-xr-x. 2 root   root      204 Sep 18 09:12 anaconda
drwx------. 2 root   root       23 Sep 18 09:12 audit
-rw-------. 1 root   root    53001 Sep 19 00:57 boot.log
-rw-------. 1 root   utmp      384 Sep 18 09:22 btmp
drwxr-xr-x. 2 chrony chrony      6 Apr 12 13:37 chrony
-rw-------. 1 root   root     3523 Sep 19 01:01 cron
-rw-r--r--  1 root   root   119414 Sep 19 00:57 dmesg
-rw-r--r--  1 root   root   119599 Sep 18 23:35 dmesg.old
-rw-r--r--. 1 root   root     1320 Sep 19 00:23 firewalld
-rw-r--r--. 1 root   root      193 Sep 18 09:05 grubby_prune_debug
....

查詢/etc/目錄下名字以"init*"開頭的檔案,找到後,只列出檔案,過濾掉目錄,並執行 ls -l 顯示詳細資訊.

[root@localhost ~]# find /etc/ -name "init*" -a -type f -exec ls -l {} \;

-rw-r--r--. 1 root root 511 Apr 11 01:09 /etc/inittab
-rw-r--r--. 1 root root 798 Apr 11 01:09 /etc/sysconfig/init
-rwxr-xr-x. 1 root root 5419 Jan  2  2018 /etc/sysconfig/network-scripts/init.ipv6-global
-rw-r--r--. 1 root root 30 Apr 11 14:12 /etc/selinux/targeted/contexts/initrc_context

查詢/tmp/下,的yum.log檔案,找到後直接刪除.

[root@localhost tmp]# find /tmp/ -name yum.log -exec rm {} \;
[root@localhost tmp]#

查詢根下,找關於lyshark使用者的所有檔案,找到後直接刪除.

[root@localhost ~]# find / -user lyshark -exec rm -r {} \;

find: ‘/proc/1465/task/1465/fd/6’: No such file or directory
find: ‘/proc/1465/task/1465/fdinfo/6’: No such file or directory
find: ‘/proc/1465/fd/5’: No such file or directory
find: ‘/proc/1465/fdinfo/5’: No such file or directory
find: ‘/root/Catalog’: No such file or directory
find: ‘/home/lyshark’: No such file or directory

#rm -r 連帶目錄一起刪除.報錯原因:-exec 不適合大量傳輸,速率慢導致.

在根下,查詢lyshark使用者的檔案,找到後刪除,刪除前會提示是否刪除.

[root@localhost ~]# find / -user lyshark -ok rm -r {} \;
find: ‘/proc/1777/task/1777/fd/6’: No such file or directory
find: ‘/proc/1777/task/1777/fdinfo/6’: No such file or directory

< rm ... /root/LyShark > ? y

# -ok的使用和-exec是一樣的,區別是-ok,執行時會提示你是否進行下一步操作.
文章出處:https://www.cnblogs.com/LyShark/p/15750017.html
版權宣告:本部落格文章與程式碼均為學習時整理的筆記,部落格中除去明確標註有參考文獻的文章,其他文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!

如果您惡意轉載本人文章並被本人發現,則您的整站文章,將會變為我的原創作品,請相互尊重 !