Linux檔案查詢常用命令-詳細筆記

語言: CN / TW / HK

which-whereis-locate-grep find 命令使用

查詢檔案一般有以下幾個命令: | which | 檢視可執行檔案的位置 | |--|--| | whereis | 檢視可執行檔案的位置及相關檔案 | | locate | 配合資料庫快取,快速檢視檔案位置 | |grep|過濾匹配,它是一個檔案搜尋工具| | find| 查詢相關檔案 |

舉例:

[[email protected] ~]# which cd
/usr/bin/cd
[[email protected] ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz
[[email protected] ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.g
locate

locate 命令和 find -name 功能差不多,是它的另外一種寫法,但是這個要比 find 搜尋快的多,因 為 find 命令查詢的是具體目錄檔案,而 locate 它搜尋的是一個數據庫 /var/lib/mlocate/mlocate.db,這個資料庫中存有本地所有的檔案資訊;這個資料庫是 Linux 自動創 建並每天自動更新維護。相關的配置資訊在 /etc/updatedb.conf,檢視定時任務資訊在 /etc/cron.daily/mlocate

[[email protected] ~]# yum -y install mlocate
[[email protected] mnt]# touch /opt/xuegod.txt
[[email protected] mnt]# locate xuegod.txt     #發現找不到
[[email protected] mnt]# updatedb   #如果對當天檔案查詢,需要手動更新資料庫  updatedb
[[email protected] mnt]# locate xuegod

grep 查詢使用

作用:過濾,它能夠使用正則表示式來搜尋文字,並把結果打印出來 引數: | -v | 取反 | |--|--| | -i | 忽略大小寫 | | ^# | 以#開頭 | |#$|以#結尾| | ^$ | 空行 | | -n | 對過濾的內容加上行號 | | l | 或者的意思 |

[[email protected] ~]#   ps -aux | grep sshd | grep -v grep
root  1089  0.0  0.2 105996   4088 ?   Ss   20:19  0:00 /usr/sbin/sshd –D
[[email protected] ~]# cat /etc/passwd | grep ^a #以 a  開頭
[[email protected] ~]# grep bash$ /etc/passwd   #以 bash  結尾
[[email protected] ~]# grep "nologin\|root" /etc/passwd | wc -l
注:  \ 表示轉義符
[[email protected] ~]# egrep "nologin|root" /etc/passwd | wc -l
#檢視包括 nologin 或 root  的行
注:egrep  是 grep  加強版本

find 命令使用

格式:find     pathname   -options   [-print]
     命令字    路徑名稱     選項        輸出

引數: pathname: find 命令所查詢的目錄路徑,不輸入代表當前目錄例如用 . 來表示當前目錄,用 / 來表示系統根目錄。

find 命令選項: | -name | 按照檔名查詢檔案‘名稱’| |--|--| | -perm | 按照檔案許可權來查詢檔案。666 777 等 | | -user | 按照檔案屬主來查詢檔案 | |-group|按照檔案所屬的組來查詢檔案| | -mtime | -n/ +n 按照檔案的更改時間來查詢檔案 | | - n | 表示檔案更改時間距現在 n 天以內 | | + n | 表示檔案更改時間距現在 n 天以前 | |-type|查詢某一型別的檔案| | b - | 塊裝置檔案 | | d- | 目錄 | |c -|字元裝置檔案| | p - | 管道檔案 | | l- | 符號連結檔案 | | f - | 普通檔案 | |-size n|查詢符合指定的檔案大小的檔案| | -exec |對匹配的檔案執行該引數所給出的其他 linux 命令, 相應命令的形式為' 命令 {} \ 注意{ }和 \;之間的空格,{}代表查到的內容|

示例 1:希望在 root 目錄下查詢更改時間在 1 天以內,被修改的檔案

[[email protected] ~]# find /root/ -mtime -1

對查詢內容執行相應命令 -exec 這個選項引數後面可以跟自定義的 SHELL 命令,格式如下: 在這裡插入圖片描述

例 2:

[[email protected] ~]# touch {1,2,3}.back
[[email protected] mnt]# find . -name "*.back" -exec ls -l {} \;

例 3:

[[email protected] ~]#   find . -name "*.back" -exec mv {} /opt \;
[[email protected] ~]# ls /opt/
1.back  2.back   3.back  rh   xuegod.txt

例 4:把查詢到的檔案複製到一個指定的目錄

[[email protected] mnt]# find /root -name "*.txt" -exec cp {} /opt\;

例 5:xargs 和 find 命令結合 複製檔案

-i   表示  find  傳遞給 xargs 的結果 由{}來代替   (了
解)
[[email protected] ~]# rm -rf /opt/*
[[email protected] ~]# find . -name "*.txt"   | xargs    -i cp {}
[[email protected] ~]# ls /opt/

例 6:查詢多個型別檔案

比較符的使用: | -a | and 並且| |--|--| | -o | or或者 | | + | 超過 | |-|低於|

[[email protected] ~]# touch a.pdf back.sh
[[email protected] ~]# find . -name "*.sh" -o -name "*.pdf"
[[email protected] ~]# find /etc -size +20k -a -size -50k | wc -l
22
[[email protected] ~]# find /etc -size +20k   | wc -l
49

例 7: 按許可權查詢:-perm

[[email protected] ~]# find /bin/ -perm 755
#等於 0755  許可權的檔案或目錄
[[email protected] ~]# find /bin/ -perm   -644 #    -表示至少,至少有 644  許可權的檔案或目錄

例:檢視系統中許可權至少為 777 的檔案或目錄 建立一些測試檔案:

[[email protected] ~]# mkdir ccc
[[email protected] ~]# chmod 777 ccc
[[email protected] ~]# mkdir test
[[email protected] ~]# chmod 1777 test
[[email protected] ~]# touch b.sh
[[email protected] ~]# chmod 4777 b.sh

查詢: [[email protected] ~]# find /root/ -perm 777 [[email protected] ~]# find /root/ -perm 1777 [[email protected] ~]# find /root/ -perm 4777 例:把系統中許可權不低於 777 的危險目錄查找出來

[[email protected] ~]# find /root/ -perm-777 #至少有 777  許可權

例:把系統中許可權不低於 777 的危險檔案查找出來

[[email protected] ~]# find / -type f -perm-777

例 8:查詢的目錄深度:

-maxdepth 1 #只查詢目錄第一層的檔案和目錄 如:查詢/bin 目錄下許可權等於 644 的檔案

[[email protected] ~]# find /etc/ -maxdepth 1 -perm 644 | more
[[email protected] ~]# find /bin/ -maxdepth 1 -perm 755   #/bin 後面要有/
[[email protected] ~]# find /bin -maxdepth 1 -perm 755     #沒加/無法滿足我們的需求

例 9:查詢系統中所有屬於使用者 user1 的檔案,並把這個檔案,放到/root/findresults 目錄下

注意:/root/findresults 這個需要提前建立好。

[email protected] ~]# mkdir /root/findresults
[[email protected] ~]# useradd user1
[[email protected] ~]# find / -user user1 -exec cp -a {} /root/findresults/

#引數: -a #複製時,保留原來檔案的所有屬性 報錯:

find:  ‘/proc/43475/task/43475/fd/6’: 沒有那個檔案或目錄
find:  ‘/proc/43475/task/43475/fdinfo/6’: 沒有那個檔案或目錄
find:  ‘/proc/43475/fd/6’: 沒有那個檔案或目錄
find:  ‘/proc/43475/fdinfo/6’: 沒有那個檔案或目錄
cp:  無法以目錄"/home/user1" 來覆蓋非目錄"/root/findresults/user1"

同一個目錄下,可以建立檔案 user1 和資料夾 user1 嗎?同一個目錄下建立的檔名和目錄 名一樣嗎? 不可以

[[email protected] ~]# touch abc
[[email protected] ~]# mkdir abc
mkdir:  無法建立目錄"abc": 檔案已存在

解決:

[[email protected] ~]# find / -user user1   #發現
[[email protected] ~]# ll /var/spool/mail/user1   #檢視這個檔案
[[email protected] ~]# ll /home/user1
發現/var/spool/mail/user1  和/home/user1 的名字是一樣的。  而兩者都要複製到
/root/findresults/下,先複製了/var/spool/mail/user1,所以/home/user1  就不能複製了。
[[email protected] ~]# mv /var/spool/mail/user1
[[email protected] ~]# rm -rf /root/findresults/ *
/var/spool/mail/user1.mail
[[email protected] ~]# find / -user mk -exec cp -a {} /root/findresults/ \;
[[email protected] ~]# mv /var/spool/mail/user1.mail
/var/spool/mail/user1
#再修改過來
                                                    文章到此結束
                                                 想要了解更多的技術