Linux 操作系統(二)搜索文件命令find、locate、which、whereis、grep、wc


以下命令均已在 Kali Linux 下驗證。

 

1.find 命令

 

--1--

 

find /usr/share -name test.lst //精准搜索,文件名需要與-name后的內容一模一樣包括后綴

 

--2--

 

find /usr/share -name "tes*//通配符* 表示匹配所有

 

find /usr/share -name "test.???" //通配符? 表示匹配單個字符

 

--3--

 

find /usr/share -iname "test.lst" // -iname 參數,不區分文件名大小寫

 

--4--

 

find /usr/share -size +1024   //按照文件大小查找; -size 參數 +n大於 -n小於 n等於; 1塊=512k, 2048塊=10M;

 

--5--

 

find /home -user root  //按照所有者查找;

 

--6--

 

find /etc -mmin -30  //按時間屬性查找,-amin訪問時間

 

find /etc -cmin -30  //按時間屬性查找,-cmi改變文件屬性

 

find /etc -amin -30  //按時間屬性查找,-amin改變文件內容

 

--7--

 

find /usr/share 條件1 -a 條件2 // -a 兩個條件都需要滿足

 

find /usr/share 條件1 -o 條件2 // -o 滿足一個就可

 

如:

 

find /usr/share -name "test*" -a -type f  //其中-type f文件 -type d 目錄 -type l 軟連接文件

 

--8--

 

find /usr/share -name "test.lst" -exec ls -l {} \;  // -exec/-ok 把find命令的結果 用-exec/ok調用的命令2來處理;"{}"相當於占位符,作為find的查找結果。

 

注:

 

find 命令是直接在硬盤中進行搜索,如果指定的搜索范圍過大,find命令就會消耗較大的系統資源,

導致服務器壓力過大。所以,在使用 find 命令搜索時,盡量更精准的查找,這樣消耗系統的資源少,

查找的速度快。命令所在路徑:/bin/find.

 

示例:

--1--
root@hugboy:/home/hugboy# find /usr/share -name test.lst
/usr/share/john/test.lst
--2--
root@hugboy:/home/hugboy# find /usr/share -name tes*
/usr/share/postgresql-common/testsuite
/usr/share/automake-1.16/test-driver
/usr/share/icons/Flat-Remix-Blue-Dark/apps/scalable/teslagrad.svg
/usr/share/perl5/Mail/Mailer/testfile.pm
/usr/share/perl5/Sys/Hostname/testall.pl
/usr/share/sqlmap/lib/core/testing.py
/usr/share/sqlmap/lib/techniques/union/test.py
...

root@hugboy:/home/hugboy# find /usr/share -name test.???
/usr/share/john/test.lst
/usr/share/wfuzz/wordlist/general/test.txt
/usr/share/doc/node-normalize.css/examples/test.svg
/usr/share/doc/p7zip/DOC/MANUAL/cmdline/commands/test.htm
--3--
root@hugboy:/home/hugboy# find /usr/share -iname test.lst
/usr/share/john/TesT.lst
/usr/share/john/test.lst
--4--
root@hugboy:/home/hugboy# find /usr/share -size +9999
/usr/share/exploitdb/files_exploits.csv
/usr/share/sqlmap/data/txt/wordlist.tx_
/usr/share/proj/proj.db
/usr/share/john/latin1.chr
/usr/share/john/utf8.chr
/usr/share/john/ascii.chr
/usr/share/basemap/data/UScounties.shp
/usr/share/basemap/data/shadedrelief.jpg
...
--5--
root@hugboy:/home/hugboy# find -user hugboy
.
./.bash_logout
./.Xauthority
./CTF/sm.txt
./.msf4
./.msf4/plugins
./.msf4/logos
./.msf4/history
./.msf4/logs
./.msf4/logs/sessions
./.msf4/logs/scripts
./.msf4/logs/scripts/scraper
...
--6--

root@hugboy:/home/hugboy# find /usr/share -amin -30
/usr/share
/usr/share/mime/image/jpeg.xml
/usr/share/mime/application/pdf.xml
/usr/share/icons/Adwaita/cursors/col-resize
/usr/share/icons/Adwaita/cursors/watch
/usr/share/icons/Flat-Remix-Blue-Dark/apps/scalable/vulnhub.svg
/usr/share/icons/Flat-Remix-Blue-Dark/apps/scalable/kali-web-application-trans.svg
/usr/share/icons/Flat-Remix-Blue-Dark/apps/scalable/devhelp.svg
/usr/share/icons/Flat-Remix-Blue-Dark/apps/scalable/accessories-text-editor.svg
...
--7--
root@hugboy:/home/hugboy# find /usr/share -name test.lst -a -type f
/usr/share/john/test.lst


--8--
root@hugboy:/home/hugboy# find /usr/share -name test.lst -exec ls -l {} \;
-rw-r--r-- 1 root root 85 4月  27 21:19 /usr/share/john/test.lst
View Code

 

 

2.locate 命令

 

--1--

 

locate test.lst //從數據庫中搜索文件名

 

--2--

 

updatedb // 更新數據庫

 

--3--

 

locate -i test.lst

 

注:

 

1) locate相比find速度更快,因為它是從已經建立好的數據庫中搜索文件,消耗系統資源非常小。

2) 但是locate不會實時更新數據庫,新建的文件會搜索不到,需要手動更新。(updatadb)

3) locate不收錄臨時目錄tmp下的文件。

4) 命令所在路徑:/usr/bin/locate.

 

 

示例:

root@hugboy:/usr# touch ilovelisa
root@hugboy:/usr# locate ilovelisa
root@hugboy:/usr# updatedb
root@hugboy:/usr# locate ilovelisa
/usr/ilovelisa
View Code

 

 

3.which 命令

 

--1--

 

which cp  //列出命令cp所在路徑,和命令的別名(如果存在的話)

 

 

注:

 

 

 

所在路徑:/usr/bin/which.

 

 示例:

root@hugboy:/usr# which find
/usr/bin/find
root@hugboy:/usr# which cp
/usr/bin/cp
root@hugboy:/usr# 
View Code

 

 

4.whereis 命令

 

--1--

 

whereis cp //查找二進制命令,源文件和幫助文檔的命令

 

注:

 

所在路徑:/usr/bin/whereis.

 

示例: 

root@hugboy:/usr# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
View Code

 

 

5.grep 命令

 

--1--

 

grep "this" test.lst  //存在內容"test"的行

 

--2--

 

grep -i "this" test.lst  //忽略大小寫

 

--3--

 

grep -v "this" test.lst  //反轉查找,不存在內容"test"的行

 

--4--

 

grep "^string" test.lst  //以string開頭

 

grep "string$" test.lst  //以string結尾

 

grep "^$" test.lst //空行

 

注:

 

所在路徑:/bin/grep.

 

示例:

root@hugboy:/usr/share/john# cat test.lst
This a test to proof linux order.

and my love Godness is Lisa hahah...
Just a Test.
this all,Bye~
--1--
root@hugboy:/usr/share/john# grep "this" test.lst
this all,Bye~

--2--
root@hugboy:/usr/share/john# grep -i "this" test.lst
This a test to proof linux order.
this all,Bye~

--3--
root@hugboy:/usr/share/john# grep -v "this" test.lst
This a test to proof linux order.

and my love Godness is Lisa hahah...
Just a Test.

--4--
root@hugboy:/usr/share/john# grep "...$" test.lst
This a test to proof linux order.
and my love Godness is Lisa hahah...
Just a Test.
this all,Bye~

root@hugboy:/usr/share/john# grep "^$" test.lst

root@hugboy:/usr/share/john# grep -n "^$" test.lst
2:
View Code

 

 

6.wc 統計

 

--1--

wc -l test.lst // 行數

 

wc -w test.lst // 單詞數

 

wc -c test.lst // 字節數

 

注:

 

命令所在路徑:/usr/bin/wc.

 

示例:

 

root@hugboy:/usr/share/john# wc -l test.lst
5 test.lst
root@hugboy:/usr/share/john# wc -w test.lst
19 test.lst
root@hugboy:/usr/share/john# wc -c test.lst
99 test.lst
root@hugboy:/usr/share/john# 
View Code

 

7.參考文章

Linux常用命令...


免責聲明!

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



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