查命令絕對(duì)路徑:
which用于查找并顯示給定命令的絕對(duì)路徑,環(huán)境變量中PATH參數(shù)也可以被查出來(lái)。
[root@localhost ~]# which bash/usr/bin/bash[root@localhost ~]# which lsalias ls=’ls –color=auto’ /usr/bin/ls
尋找特定文件:
whereis命令用來(lái)定位指令的二進(jìn)制程序、源代碼文件和man手冊(cè)頁(yè)等相關(guān)文件的路徑,該命令只能用于程序名的搜索
[root@localhost ~]# whereis –help語(yǔ)法格式:[ whereis [選項(xiàng)] 文件名 ] -b #只找二進(jìn)制文件 -m #只找man文檔 -s #只找源代碼
使用 whereis -b 命令找二進(jìn)制文件,與幫助手冊(cè)。
[root@localhost ~]# whereis -b ifconfigifconfig: /usr/sbin/ifconfig[root@localhost ~]# whereis -m ifconfigifconfig: /usr/share/man/man8/ifconfig.8.gz
緩存查找文件:
locate 搜索一個(gè)數(shù)據(jù)庫(kù)/var/lib/mlocatedb,這個(gè)數(shù)據(jù)庫(kù)中含有本地所有文件信息,Linux系統(tǒng)自動(dòng)創(chuàng)建這個(gè)數(shù)據(jù)庫(kù),并且每天自動(dòng)更新一次,所以使用locate命令查不到最新變動(dòng)過(guò)的文件,為了避免這種情況,可以在使用locate之前,先使用updatedb命令,手動(dòng)更新數(shù)據(jù)庫(kù),updatedb命令會(huì)根據(jù)/etc/updatedb.conf來(lái)更新文件.
[root@localhost ~]# yum install -y mlocate[root@localhost ~]# locate –help語(yǔ)法格式:[ locate [選項(xiàng)] 文件名 ] -d 目錄 #指定數(shù)據(jù)庫(kù)所在的目錄 -i #忽略大小寫差異 -r #后面接正則表達(dá)式
使用 locate 命令查詢一個(gè)文件.
[root@localhost ~]# updatedb [root@localhost ~]# locate /etc/passwd/etc/passwd/etc/passwd-
遍歷文件查找:
find 命令可以說(shuō)是最重要的查找命令了,該命令參數(shù)較多。
[root@localhost ~]# find –help語(yǔ)法格式:[ find [目錄](méi) [屬性] 文件名 ] -name #按文件名查找 -size #根據(jù)大小查找 -user #根據(jù)屬主查找 -perm #根據(jù)權(quán)限查找 -type #根據(jù)類型查找 -time #按時(shí)間查找 -inum #根據(jù)i節(jié)點(diǎn)查詢 -exec #查找后執(zhí)行命令
-name 按文件名查找:
常用查詢通配符
* #匹配任意一個(gè)或多個(gè)字符? #匹配任意一個(gè)字符[] #指定范圍,外側(cè)加引號(hào)
查找/var/目錄下,以.log結(jié)尾的文件.
[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之間],結(jié)尾是.txt的文件
[root@localhost ~]# ls1.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個(gè)任意字符的文件
[root@localhost ~]# find /etc/ -name “??????”/etc/grub.d/etc/grub.d/README/etc/shells/etc/init.d….省略….
-size 根據(jù)大小查找
單位是 block 數(shù)據(jù)塊 一塊是512字節(jié)1M -> 1024k -> 2048 塊 (1塊是0.5k 也就是512字節(jié))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….省略….#注意:+-號(hào)如果沒(méi)有,是精確到這么大,通常都會(huì)帶上+或-號(hào)表示一個(gè)范圍.
-user 根據(jù)屬主與權(quán)限查找
在/root目錄中查找屬于wang用戶的文件
[root@localhost ~]# find /root/ -user wang/root/1.txt/root/2.txt/root/3.txt#注意:系統(tǒng)中要存在該用戶,否則會(huì)報(bào)錯(cuò)誤.
查找/boot/目錄中權(quán)限是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 根據(jù)類型查找
-type f 二進(jìn)制文件(普通文件)-type l 軟鏈接文件-type d 目錄
查找/usr/bin/目錄下,類型是二進(jìn)制文件.
[root@localhost ~]# find /usr/bin/ -type f/usr/bin/cp/usr/bin/gzip/usr/bin/alias/usr/bin/csplit/usr/bin/bash….省略….
-time 按時(shí)間查找
按天數(shù) ctime atime mtime按分鐘 cmin amin mmin c change #表示屬性被修改過(guò):所有者、所屬組、權(quán)限 a access #被訪問(wèn)過(guò)(被查看過(guò)) m modify #表示內(nèi)容被修改過(guò)
查找/etc/目錄下,在120分鐘以內(nèi),內(nèi)容被修改過(guò)的文件
[root@localhost ~]# find /etc/ -mmin -120/etc//etc/resolv.conf/etc/group-/etc/gshadow-/etc/group/etc/gshadow….省略….
查找/etc/目錄下,在7天之前,屬性被修改過(guò)的文件
[root@localhost ~]# find /etc/ -ctime +7/etc/resolv.conf/etc/group-/etc/gshadow-….省略….
-inum 根據(jù)i節(jié)點(diǎn)查詢
有一些文件的硬鏈接數(shù)量很多,有相同的i節(jié)點(diǎn),查找其中一個(gè)文件的i節(jié)點(diǎn)號(hào),一次性刪除。
[root@localhost ~]# find ./ -inum 1024 -exec rm{} ; #刪除相同I節(jié)點(diǎn)的數(shù)據(jù)
-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 命令執(zhí)行連接符
[查詢格式] find … -exec 命令 {} ;{} #表示find查詢的結(jié)果集 #是轉(zhuǎn)義符,不使用命令別名,直接使用命令本身; #分號(hào)是表示語(yǔ)句的結(jié)束.#注意:固定格式,只能這樣寫.注意中間的空格.(公眾號(hào):網(wǎng)絡(luò)工程師阿龍)—————————————————————–說(shuō)明: 轉(zhuǎn)義符的作用是什么?在linux中有一個(gè)別名機(jī)制,如rm刪除文件,執(zhí)行的卻是rm -i(用which rm 可以查看命令別名),使用rm刪除文件前會(huì)提示,就是因?yàn)閞m -i這個(gè)參數(shù)。如果想使用命令原意,可以在加轉(zhuǎn)義,如:rm test.txt 則不會(huì)提示,直接刪除
查找/var/log/目錄下名字以.log結(jié)尾的文件,找到后執(zhí)行 ls -l 顯示詳細(xì)信息.
[root@localhost ~]# find /var/log/ *.log -exec ls -l {} ;total 1176drwxr-xr-x. 2 root root 204 Sep 18 09:12 anacondadrwx——. 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 btmpdrwxr-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*”開頭的文件,找到后,只列出文件,過(guò)濾掉目錄,并執(zhí)行 ls -l 顯示詳細(xì)信息.
[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]#
查找根下,找關(guān)于lyshark用戶的所有文件,找到后直接刪除.
[root@localhost ~]# find / -user lyshark -exec rm -r {} ;find: ‘/proc/1465/task/1465/fd/6’: No such file or directoryfind: ‘/proc/1465/task/1465/fdinfo/6’: No such file or directoryfind: ‘/proc/1465/fd/5’: No such file or directoryfind: ‘/proc/1465/fdinfo/5’: No such file or directoryfind: ‘/root/Catalog’: No such file or directoryfind: ‘/home/lyshark’: No such file or directory#rm -r 連帶目錄一起刪除.報(bào)錯(cuò)原因:-exec 不適合大量傳輸,速率慢導(dǎo)致.
在根下,查找lyshark用戶的文件,找到后刪除,刪除前會(huì)提示是否刪除.
[root@localhost ~]# find / -user lyshark -ok rm -r {} ;find: ‘/proc/1777/task/1777/fd/6’: No such file or directoryfind: ‘/proc/1777/task/1777/fdinfo/6’: No such file or directory ? y# -ok的使用和-exec是一樣的,區(qū)別是-ok,執(zhí)行時(shí)會(huì)提示你是否進(jìn)行下一步操作.
來(lái)源:http://u6.gg/kq67b