Linux常用命令——文件搜索命令


1、文件搜索命令:locate

locate 文件名
--在后台數據庫中按文件名搜索,搜索速度更快,只能根據文件名搜索,功能單一

/var/lib/mlocate
--locate命令所搜索的后台數據庫

updatedb
--更新數據庫
[root@localhost ~]# ls
anaconda-ks.cfg  binaries  binaries.tar.gz  initial-setup-ks.cfg  test123
[root@localhost ~]# locate anaconda-ks.cfg 
/root/anaconda-ks.cfg
[root@localhost ~]# touch test3
[root@localhost ~]# locate test3  --后台數據庫不是實時更新,更新頻率為一天一次
[root@localhost ~]# updatedb      --更新數據庫
[root@localhost ~]# locate test3
/root/test3

  locate文件遵守/etc/update.conf配置文件。https://www.cnblogs.com/Simon212/p/11031075.html

 2、命令搜索命令:whereis和which

whereis 命令名
    搜索命令所在路徑及幫助文檔所在位置
        選項:
            -b:  只查找可執行文件
            -m: 只查找幫助文件

  

which 文件名
    搜索命令所在路徑及別名

  

[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

3、文件搜索命令:find 

find [搜索范圍] [搜索條件]
    搜索文件
    避免大范圍搜索,會非常消耗系統資源
    find是在系統中搜索符合條件的文件名。如果需要匹配,使用通配符匹配,通配符是完全匹配

  Linux中的通配符:

  *  匹配任意內容

  ?  匹配任意一個字符

  []  匹配任意一個中括號內的字符

[root@localhost ~]# ls
anaconda-ks.cfg  binaries  binaries.tar.gz  initial-setup-ks.cfg  test123  test3
[root@localhost ~]# find / -name binaries
/root/binaries
[root@localhost ~]# find /root -name "binaries*"
/root/binaries.tar.gz
/root/binaries
[root@localhost ~]# find /root -name "test[123]"
/root/test123/test2
/root/test3

 find支持的其他命令:

[root@localhost ~]# find /root -iname binaries   --不區分大小寫

  

[root@localhost ~]# find /root -user root  --按照所有者搜索
/root
/root/.bash_logout
/root/.bash_profile
/root/.bashrc
/root/.cshrc
/root/.tcshrc
/root/anaconda-ks.cfg
/root/.cache
/root/.cache/dconf
/root/.cache/dconf/user
/root/.cache/abrt
/root/.cache/abrt/lastnotification
/root/.dbus
/root/.dbus/session-bus
/root/.dbus/session-bus/9621f505534747eb82894e790c8120e1-9
/root/initial-setup-ks.cfg
/root/.config
/root/.config/abrt
/root/.bash_history
/root/.bashrc_test
/root/.xauth91pRTQ
/root/binaries.tar.gz
/root/.xauthsXfZnT
/root/.viminfo
/root/test123
/root/test123/test2
/root/.Xauthority
/root/.anaconda-ks.cfg.swp
/root/test3

  

[root@localhost ~]# find /root -nouser    --查找沒有所有者的文件
    Linux沒有所有者的文件基本上是垃圾文件,但有兩個例外
    1.由內核產生的文件不經過系統用戶,有可能沒有所有者
    2.外來文件有可能沒有所有者

  

find /var/log/ -mtime +10
    查找10天前修改的文件
    
    
    -10        10天內修改的文件
    10         10天當天修改的文件
    +10        10天前修改的文件

    atime        文件訪問時間
    ctime        改變文件屬性
    mtime        修改文件內容

  

find . -size 25k
    查找文件大小是25KB的文件

    -25k    小於25KB的文件
    25k     等於25KB的文件
    +25k    大於25KB的文件
  搜索千字節需要小寫,搜索兆字節需要大寫
  默認的單位是按照扇區划分的數據塊,建議最好加入單位

  

find . inum 262422
    查找i節點是262422的文件

  

find /etc -size +20k -a -size -50k -exec ls -lh {} \;
    查找/etc/目錄下,大於20KB並且小於50KB的文件,並顯示詳細信息
        -a    and    邏輯與,兩個條件都滿足
        -o    or      邏輯或,兩個條件滿足一個即可
        -exec/-ok 命令 {} \;     對搜索結果執行操作

4、字符串搜索命令grep

grep [選項] 字符串 文件名
    在文件當中匹配符合條件的字符串
    選項:
        -i    忽略大小寫
        -v   排除指定字符串

5、find命令和grep命令的區別

find命令:在系統當中搜索符合條件的文件名,如果需要匹配,使用通配符匹配,通配符是完全匹配

grep命令:在文件當中搜索符合條件的字符串,如果需要匹配,使用正則表達式進行匹配,正則表達式是包含匹配


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM