亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Linux文件查找命令總結(jié)(上篇)

 更新時(shí)間:2023年01月13日 15:20:16   作者:小熊coder  
這篇文章主要介紹了Linux文件查找命令總結(jié)(上篇),本文章內(nèi)容詳細(xì),通過(guò)案例可以更好的掌握文件查找的相關(guān)命令,本篇為上篇,需要的朋友可以參考下

前文

關(guān)于Linux文件查找命令總結(jié)我們分別介紹了which命令、whereis命令、locate命令、find命令這四個(gè)命令,本篇向大家介紹的是:which命令、whereis命令、locate命令。

which命令語(yǔ)法

我們經(jīng)常在linux要查找某個(gè)文件,但不知道放在哪里了,可以使用下面的一些命令來(lái)搜索:

  • which 查看可執(zhí)行文件的位置。
  • whereis 查看文件的位置。
  • locate 配合數(shù)據(jù)庫(kù)查看文件位置。
  • find 實(shí)際搜尋硬盤查詢文件名稱。
  1. which命令的作用是,在PATH變量指定的路徑中,搜索某個(gè)系統(tǒng)命令的位置,并且返回第一個(gè)搜索結(jié)果。
  2. 也就是說(shuō),使用which命令,就可以看到某個(gè)系統(tǒng)命令是否存在,以及執(zhí)行的到底是哪一個(gè)位置的命令。

命令格式

which 可執(zhí)行文件名稱

命令功能

which指令會(huì)在PATH變量指定的路徑中,搜索某個(gè)系統(tǒng)命令的位置,并且返回第一個(gè)搜索結(jié)果。

命令參數(shù)

  • -n 指定文件名長(zhǎng)度,指定的長(zhǎng)度必須大于或等于所有文件中最長(zhǎng)的文件名。
  • -p 與-n參數(shù)相同,但此處的包括了文件的路徑。
  • -w 指定輸出時(shí)欄位的寬度。
  • -V 顯示版本信息

which命令使用示例

查找文件、顯示命令路徑

命令:

which lsmod

輸出:

[root@localhost ~]# which pwd
/bin/pwd
[root@localhost ~]# which adduser
/usr/sbin/adduser
[root@localhost ~]#

說(shuō)明:

which 是根據(jù)使用者所配置的 PATH 變量?jī)?nèi)的目錄去搜尋可運(yùn)行檔的!

所以,不同的 PATH 配置內(nèi)容所找到的命令當(dāng)然不一樣的!

用 which 去找出 which

命令:

which which

輸出:

[root@localhost ~]# which which
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
   /usr/bin/which
[root@localhost ~]#

說(shuō)明:

竟然會(huì)有兩個(gè) which ,其中一個(gè)是 alias 這就是所謂的『命令別名』,意思是輸入 which 會(huì)等於后面接的那串命令!

找出 cd 這個(gè)命令

命令:

which cd

輸出:

img

? 說(shuō)明:

cd 這個(gè)常用的命令竟然找不到啊!為什么呢?

這是因?yàn)?cd 是bash 內(nèi)建的命令! 但是 which 默認(rèn)是找 PATH 內(nèi)所規(guī)范的目錄,所以當(dāng)然一定找不到的!

whereis 命令語(yǔ)法

whereis命令只能用于程序名的搜索,而且只搜索二進(jìn)制文件(參數(shù)-b)、man說(shuō)明文件(參數(shù)-m)和源代碼文件(參數(shù)-s)。

如果省略參數(shù),則返回所有信息。

和find相比,whereis查找的速度非??欤@是因?yàn)閘inux系統(tǒng)會(huì)將 系統(tǒng)內(nèi)的所有文件都記錄在一個(gè)數(shù)據(jù)庫(kù)文件中。

當(dāng)使用whereis和下面即將介紹的locate時(shí),會(huì)從數(shù)據(jù)庫(kù)中查找數(shù)據(jù),而不是像find命令那樣,通 過(guò)遍歷硬盤來(lái)查找,效率自然會(huì)很高。

但是該數(shù)據(jù)庫(kù)文件并不是實(shí)時(shí)更新,默認(rèn)情況下時(shí)一星期更新一次。

因此,我們?cè)谟脀hereis和locate 查找文件時(shí),有時(shí)會(huì)找到已經(jīng)被刪除的數(shù)據(jù),或者剛剛建立文件,卻無(wú)法查找到,原因就是因?yàn)閿?shù)據(jù)庫(kù)文件沒(méi)有被更新。

命令格式

whereis [-bmsu] [BMS 目錄名 -f ] 文件名

命令功能

whereis命令是定位可執(zhí)行文件、源代碼文件、幫助文件在文件系統(tǒng)中的位置。

這些文件的屬性應(yīng)屬于原始代碼,二進(jìn)制文件,或是幫助文件。

whereis 程序還具有搜索源代碼、指定備用搜索路徑和搜索不尋常項(xiàng)的能力。

命令參數(shù)

-b 定位可執(zhí)行文件。

-m 定位幫助文件。

-s 定位源代碼文件。

-u 搜索默認(rèn)路徑下除可執(zhí)行文件、源代碼文件、幫助文件以外的其它文件。

-B 指定搜索可執(zhí)行文件的路徑。

-M 指定搜索幫助文件的路徑。

-S 指定搜索源代碼文件的路徑。

whereis命令使用示例

將和*文件相關(guān)的文件都查找出來(lái)

命令:

whereis svn

輸出:

[root@localhost ~]# whereis tomcat

tomcat:

[root@localhost ~]# whereis svn

svn: /usr/bin/svn /usr/local/svn /usr/share/man/man1/svn.1.gz

說(shuō)明:

tomcat沒(méi)安裝,找不出來(lái),svn安裝找出了很多相關(guān)文件

只將二進(jìn)制文件 查找出來(lái)

命令:

whereis -b svn

輸出:

[root@localhost ~]# whereis -b svn

svn: /usr/bin/svn /usr/local/svn

[root@localhost ~]# whereis -m svn

svn: /usr/share/man/man1/svn.1.gz

[root@localhost ~]# whereis -s svn

svn:

[root@localhost ~]#

說(shuō)明:

whereis -m svn 查出說(shuō)明文檔路徑,whereis -s svn 找source源文件。

locate 命令語(yǔ)法

locate 讓使用者可以很快速的搜尋檔案系統(tǒng)內(nèi)是否有指定的檔案。

其方法是先建立一個(gè)包括系統(tǒng)內(nèi)所有檔案名稱及路徑的數(shù)據(jù)庫(kù),之后當(dāng)尋找時(shí)就只需查詢這個(gè)數(shù)據(jù)庫(kù),而不必實(shí)際深入檔案系統(tǒng)之中了。

在一般的 distribution 之中,數(shù)據(jù)庫(kù)的建立都被放在 crontab 中自動(dòng)執(zhí)行。

命令格式

Locate [選擇參數(shù)] [樣式]

命令功能

locate命令可以在搜尋數(shù)據(jù)庫(kù)時(shí)快速找到檔案,數(shù)據(jù)庫(kù)由updatedb程序來(lái)更新,updatedb是由cron daemon周期性建立的,locate命令在搜尋數(shù)據(jù)庫(kù)時(shí)比由整個(gè)由硬盤資料來(lái)搜尋資料來(lái)得快,但較差勁的是locate所找到的檔案若是最近才建立或 剛更名的,可能會(huì)找不到,在內(nèi)定值中,updatedb每天會(huì)跑一次,可以由修改crontab來(lái)更新設(shè)定值。(etc/crontab)

locate指定用在搜尋符合條件的檔案,它會(huì)去儲(chǔ)存檔案與目錄名稱的數(shù)據(jù)庫(kù)內(nèi),尋找合乎范本樣式條件的檔案或目錄錄,可以使用特殊字元(如”” 或”?”等)來(lái)指定范本樣式,如指定范本為kcpaner, locate會(huì)找出所有起始字串為kcpa且結(jié)尾為ner的檔案或目錄,如名稱為kcpartner若目錄錄名稱為kcpa_ner則會(huì)列出該目錄下包括 子目錄在內(nèi)的所有檔案。

locate指令和find找尋檔案的功能類似,但locate是透過(guò)update程序?qū)⒂脖P中的所有檔案和目錄資料先建立一個(gè)索引數(shù)據(jù)庫(kù),在 執(zhí)行l(wèi)oacte時(shí)直接找該索引,查詢速度會(huì)較快,索引數(shù)據(jù)庫(kù)一般是由操作系統(tǒng)管理,但也可以直接下達(dá)update強(qiáng)迫系統(tǒng)立即修改索引數(shù)據(jù)庫(kù)。

命令參數(shù)

  • -e 將排除在尋找的范圍之外。
  • -1 如果 是 1.則啟動(dòng)安全模式。在安全模式下,使用者不會(huì)看到權(quán)限無(wú)法看到 的檔案。這會(huì)始速度減慢,因?yàn)?locate 必須至實(shí)際的檔案系統(tǒng)中取得檔案的 權(quán)限資料。
  • -f 將特定的檔案系統(tǒng)排除在外,例如我們沒(méi)有到理要把 proc 檔案系統(tǒng)中的檔案 放在資料庫(kù)中。
  • -q 安靜模式,不會(huì)顯示任何錯(cuò)誤訊息。
  • -n 至多顯示 n個(gè)輸出。
  • -r 使用正規(guī)運(yùn)算式 做尋找的條件。
  • -o 指定資料庫(kù)存的名稱。
  • -d 指定資料庫(kù)的路徑
  • -h 顯示輔助訊息
  • -V 顯示程式的版本訊息

locate命令使用示例

查找和pwd****相關(guān)的所有文件

命令:

locate pwd

輸出:

peida-VirtualBox ~ # locate pwd

/bin/pwd

/etc/.pwd.lock

/sbin/unix_chkpwd

/usr/bin/pwdx

/usr/include/pwd.h

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/fakepwd.pyc

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.py

/usr/lib/python2.7/dist-packages/twisted/python/test/test_fakepwd.pyc

/usr/lib/syslinux/pwd.c32

/usr/share/help/C/empathy/irc-join-pwd.page

/usr/share/help/ca/empathy/irc-join-pwd.page

/usr/share/help/cs/empathy/irc-join-pwd.page

/usr/share/help/de/empathy/irc-join-pwd.page

/usr/share/help/el/empathy/irc-join-pwd.page

 搜索etc目錄下所有以sh開(kāi)頭的文件

命令:

locate /etc/sh

輸出:

peida-VirtualBox ~ # locate /etc/sh

/etc/shadow

/etc/shadow-

/etc/shells

peida-VirtualBox ~ #

搜索etc目錄下,所有以m開(kāi)頭的文件

命令:

locate /etc/m

輸出:

peida-VirtualBox ~ # locate /etc/m

/etc/magic

/etc/magic.mime

/etc/mailcap

/etc/mailcap.order

/etc/manpath.config

/etc/mate-settings-daemon

find 命令語(yǔ)法

Linux下find命令在目錄結(jié)構(gòu)中搜索文件,并執(zhí)行指定的操作。

Linux下find命令提供了相當(dāng)多的查找條件,功能很強(qiáng)大。

由于find具有強(qiáng)大的功能,所以它的選項(xiàng)也很多,其中大部分選項(xiàng)都值得我們花時(shí)間來(lái)了解一下。

即使系統(tǒng)中含有網(wǎng)絡(luò)文件系統(tǒng)( NFS),find命令在該文件系統(tǒng)中同樣有效,只你具有相應(yīng)的權(quán)限。

在運(yùn)行一個(gè)非常消耗資源的find命令時(shí),很多人都傾向于把它放在后臺(tái)執(zhí)行,因?yàn)楸闅v一個(gè)大的文件系統(tǒng)可能會(huì)花費(fèi)很長(zhǎng)的時(shí)間(這里是指30G字節(jié)以上的文件系統(tǒng))。

命令格式

find pathname -options [-print -exec -ok …]

命令功能

用于在文件樹(shù)種查找文件,并作出相應(yīng)的處理

命令參數(shù)

  • pathname: find命令所查找的目錄路徑。例如用.來(lái)表示當(dāng)前目錄,用/來(lái)表示系統(tǒng)根目錄。
  • -print: find命令將匹配的文件輸出到標(biāo)準(zhǔn)輸出。
  • -exec: find命令對(duì)匹配的文件執(zhí)行該參數(shù)所給出的shell命令。相應(yīng)命令的形式為’command’ { } ;,注意{ }和\;之間的空格。
  • -ok: 和-exec的作用相同,只不過(guò)以一種更為安全的模式來(lái)執(zhí)行該參數(shù)所給出的shell命令,在執(zhí)行每一個(gè)命令之前,都會(huì)給出提示,讓用戶來(lái)確定是否執(zhí)行。

命令選項(xiàng)

-name 按照文件名查找文件。

-perm 按照文件權(quán)限來(lái)查找文件。

-prune 使用這一選項(xiàng)可以使find命令不在當(dāng)前指定的目錄中查找,如果同時(shí)使用-depth選項(xiàng),那么-prune將被find命令忽略。

-user 按照文件屬主來(lái)查找文件。

-group 按照文件所屬的組來(lái)查找文件。

-mtime -n +n 按照文件的更改時(shí)間來(lái)查找文件, - n表示文件更改時(shí)間距現(xiàn)在n天以內(nèi),+ n表示文件更改時(shí)間距現(xiàn)在n天以前。find命令還有-atime和-ctime 選項(xiàng),但它們都和-m time選項(xiàng)。

-nogroup 查找無(wú)有效所屬組的文件,即該文件所屬的組在/etc/groups中不存在。

-nouser 查找無(wú)有效屬主的文件,即該文件的屬主在/etc/passwd中不存在。

-newer file1 ! file2 查找更改時(shí)間比文件file1新但比文件file2舊的文件。

-type 查找某一類型的文件,諸如:

  • b - 塊設(shè)備文件。
  • d - 目錄。
  • c - 字符設(shè)備文件。
  • p - 管道文件。
  • l - 符號(hào)鏈接文件。
  • f - 普通文件。

-size n:[c] 查找文件長(zhǎng)度為n塊的文件,帶有c時(shí)表示文件長(zhǎng)度以字節(jié)計(jì)。-depth:在查找文件時(shí),首先查找當(dāng)前目錄中的文件,然后再在其子目錄中查找。

-fstype:查找位于某一類型文件系統(tǒng)中的文件,這些文件系統(tǒng)類型通??梢栽谂渲梦募?etc/fstab中找到,該配置文件中包含了本系統(tǒng)中有關(guān)文件系統(tǒng)的信息。

-mount:在查找文件時(shí)不跨越文件系統(tǒng)mount點(diǎn)。

-follow:如果find命令遇到符號(hào)鏈接文件,就跟蹤至鏈接所指向的文件。

-cpio:對(duì)匹配的文件使用cpio命令,將這些文件備份到磁帶設(shè)備中。

另外,下面三個(gè)的區(qū)別:

  • -amin n 查找系統(tǒng)中最后N分鐘訪問(wèn)的文件
  • -atime n 查找系統(tǒng)中最后n*24小時(shí)訪問(wèn)的文件
  • -cmin n 查找系統(tǒng)中最后N分鐘被改變文件狀態(tài)的文件
  • -ctime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件狀態(tài)的文件
  • -mmin n 查找系統(tǒng)中最后N分鐘被改變文件數(shù)據(jù)的文件
  • -mtime n 查找系統(tǒng)中最后n*24小時(shí)被改變文件數(shù)據(jù)的文件

find命令使用示例

查找指定時(shí)間內(nèi)修改過(guò)的文件

命令:

find -atime -2

輸出:

[root@peidachang ~]# find -atime -2

.

./logs/monitor

./.bashrc

./.bash_profile

./.bash_history

說(shuō)明:

超找48小時(shí)內(nèi)修改過(guò)的文件

根據(jù)關(guān)鍵字查找

命令:

find . -name "*.log"

輸出:

[root@localhost test]# find . -name "*.log" 

./log_link.log

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

說(shuō)明:

在當(dāng)前目錄查找 以.log結(jié)尾的文件。 ". "代表當(dāng)前目錄

按照目錄或文件的權(quán)限來(lái)查找文件

命令:

find /opt/soft/test/ -perm 777

輸出:

[root@localhost test]# find /opt/soft/test/ -perm 777

/opt/soft/test/log_link.log

/opt/soft/test/test4

/opt/soft/test/test5/test3

/opt/soft/test/test3

說(shuō)明:

查找/opt/soft/test/目錄下 權(quán)限為 777的文件

按類型查找

命令:

find . -type f -name "*.log"

輸出:

[root@localhost test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

說(shuō)明:

查找當(dāng)目錄,以.log結(jié)尾的普通文件

查找當(dāng)前所有目錄并排序

命令:

find . -type d | sort

輸出:

[root@localhost test]# find . -type f -name "*.log"

./log2014.log

./test4/log3-2.log

./test4/log3-3.log

./test4/log3-1.log

./log2013.log

./log2012.log

./log.log

./test5/log5-2.log

./test5/log5-3.log

./test5/log.log

./test5/log5-1.log

./test5/test3/log3-2.log

./test5/test3/log3-3.log

./test5/test3/log3-1.log

./test3/log3-2.log

./test3/log3-3.log

./test3/log3-1.log

[root@localhost test]#

按大小查找文件

命令:

find . -size +1000c -print

輸出:

[root@localhost test]# find . -size +1000c -print

.

./test4

./scf

./scf/lib

./scf/service

./scf/service/deploy

./scf/service/deploy/product

./scf/service/deploy/info

./scf/doc

./scf/bin

./log2012.log

./test5

./test5/test3

./test3

[root@localhost test]#

說(shuō)明:

查找當(dāng)前目錄大于1K的文件

find命令之exec使用示例

find是我們很常用的一個(gè)Linux命令,但是我們一般查找出來(lái)的并不僅僅是看看而已,還會(huì)有進(jìn)一步的操作,這個(gè)時(shí)候exec的作用就顯現(xiàn)出來(lái)了。

exec解釋:

-exec 參數(shù)后面跟的是command命令,它的終止是以;為結(jié)束標(biāo)志的,所以這句命令后面的分號(hào)是不可缺少的,考慮到各個(gè)系統(tǒng)中分號(hào)會(huì)有不同的意義,所以前面加反斜杠。

{} 花括號(hào)代表前面find查找出來(lái)的文件名。

使用find時(shí),只要把想要的操作寫在一個(gè)文件里,就可以用exec來(lái)配合find查找,很方便的。

在有些操作系統(tǒng)中只允許-exec選項(xiàng)執(zhí)行諸如l s或ls -l這樣的命令。

大多數(shù)用戶使用這一選項(xiàng)是為了查找舊文件并刪除它們。

建議在真正執(zhí)行rm命令刪除文件之前,最好先用ls命令看一下,確認(rèn)它們是所要?jiǎng)h除的文件。

exec選項(xiàng)后面跟隨著所要執(zhí)行的命令或腳本,然后是一對(duì)兒{ },一個(gè)空格和一個(gè)\,最后是一個(gè)分號(hào)。為了使用exec選項(xiàng),必須要同時(shí)使用print選項(xiàng)。

如果驗(yàn)證一下find命令,會(huì)發(fā)現(xiàn)該命令只輸出從當(dāng)前路徑起的相對(duì)路徑及文件名。

ls -l命令放在find命令的-exec選項(xiàng)中

命令:

find . -type f -exec ls -l {} \;

輸出:

[root@localhost test]# find . -type f -exec ls -l {} \; 

-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log

-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log

-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log

-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log

-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log

-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log

[root@localhost test]#

說(shuō)明:

上面的例子中,find命令匹配到了當(dāng)前目錄下的所有普通文件,并在-exec選項(xiàng)中使用ls -l命令將它們列出。

在目錄中查找更改時(shí)間在n日以前的文件并刪除它們

命令:

find . -type f -mtime +14 -exec rm {} \; 

輸出:

[root@localhost test]# ll

總計(jì) 328

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root   33 10-28 16:54 log2013.log

-rw-r--r-- 1 root root  127 10-28 16:51 log2014.log

lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log

-rw-r--r-- 1 root root   25 10-28 17:02 log.log

-rw-r--r-- 1 root root   37 10-28 17:07 log.txt

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxrwx 2 root root  4096 10-28 14:47 test3

drwxrwxrwx 2 root root  4096 10-28 14:47 test4

[root@localhost test]# find . -type f -mtime +14 -exec rm {} \;

[root@localhost test]# ll

總計(jì) 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxrwx 2 root root  4096 11-12 19:32 test3

drwxrwxrwx 2 root root  4096 11-12 19:32 test4

[root@localhost test]# 

說(shuō)明

在shell中用任何方式刪除文件之前,應(yīng)當(dāng)先查看相應(yīng)的文件,一定要小心!

當(dāng)使用諸如mv或rm命令時(shí),可以使用-exec選項(xiàng)的安全模式。它將在對(duì)每個(gè)匹配到的文件進(jìn)行操作之前提示你。

在目錄中查找更改時(shí)間在n日以前的文件并刪除它們**,**在刪除之前先給出提示

命令:

find . -name "*.log" -mtime +5 -ok rm {} \;

輸出:

[root@localhost test]# ll

總計(jì) 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

lrwxrwxrwx 1 root root   7 10-28 15:18 log_link.log -> log.log

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxrwx 2 root root  4096 11-12 19:32 test3

drwxrwxrwx 2 root root  4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} \;

< rm ... ./log_link.log > ? y

< rm ... ./log2012.log > ? n

[root@localhost test]# ll

總計(jì) 312

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxrwx 2 root root  4096 11-12 19:32 test3

drwxrwxrwx 2 root root  4096 11-12 19:32 test4

[root@localhost test]#

說(shuō)明

在上面的例子中, find命令在當(dāng)前目錄中查找所有文件名以.log結(jié)尾、更改時(shí)間在5日以上的文件,并刪除它們,只不過(guò)在刪除之前先給出提示。 按y鍵刪除文件,按n鍵不刪除。

-exec中使用grep命令

命令:

find /etc -name "passwd*" -exec grep "root" {} \;

輸出:

[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} \;

root:x:0:0:root:/root:/bin/bash

root:x:0:0:root:/root:/bin/bash

[root@localhost test]#

說(shuō)明:

任何形式的命令都可以在-exec選項(xiàng)中使用。 在上面的例子中我們使用grep命令。

find命令首先匹配所有文件名為“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后執(zhí)行g(shù)rep命令看看在這些文件中是否存在一個(gè)root用戶。

查找文件移動(dòng)到指定目錄

命令:

find . -name "*.log" -exec mv {} .. \;

輸出:

[root@localhost test]# ll

總計(jì) 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf

drwxrwxr-x 2 root root 4096 11-12 22:49 test3

drwxrwxr-x 2 root root 4096 11-12 19:32 test4

[root@localhost test]# cd test3/

[root@localhost test3]# ll

總計(jì) 304

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log

[root@localhost test3]# find . -name "*.log" -exec mv {} .. \;

[root@localhost test3]# ll

總計(jì) 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計(jì) 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxr-x 2 root root  4096 11-12 22:50 test3

drwxrwxr-x 2 root root  4096 11-12 19:32 test4

[root@localhost test]#

用exec選項(xiàng)執(zhí)行cp命令

命令:

find . -name "*.log" -exec cp {} test3 \;

輸出:

[root@localhost test3]# ll

總計(jì) 0[root@localhost test3]# cd ..

[root@localhost test]# ll

總計(jì) 316

-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log

-rw-r--r-- 1 root root   61 11-12 22:44 log2013.log

-rw-r--r-- 1 root root   0 11-12 22:25 log2014.log

drwxr-xr-x 6 root root  4096 10-27 01:58 scf

drwxrwxr-x 2 root root  4096 11-12 22:50 test3

drwxrwxr-x 2 root root  4096 11-12 19:32 test4

[root@localhost test]# find . -name "*.log" -exec cp {} test3 \;

cp: “./test3/log2014.log” 及 “test3/log2014.log” 為同一文件

cp: “./test3/log2013.log” 及 “test3/log2013.log” 為同一文件

cp: “./test3/log2012.log” 及 “test3/log2012.log” 為同一文件

[root@localhost test]# cd test3

[root@localhost test3]# ll

總計(jì) 304

-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log

-rw-r--r-- 1 root root   61 11-12 22:54 log2013.log

-rw-r--r-- 1 root root   0 11-12 22:54 log2014.log

[root@localhost test3]#

到此這篇關(guān)于Linux文件查找命令總結(jié)(上篇)的文章就介紹到這了,下篇的內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • linux中文件權(quán)限的使用以及修改方式

    linux中文件權(quán)限的使用以及修改方式

    這篇文章主要介紹了linux中文件權(quán)限的使用以及修改方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 11個(gè)有用的Linux命令

    11個(gè)有用的Linux命令

    Linux命令行吸引了大多數(shù)Linux愛(ài)好者。一個(gè)正常的Linux用戶一般掌握大約50-60個(gè)命令來(lái)處理每日的任務(wù)。今天為你解釋下面幾個(gè)命令:sudo、python、mtr、Ctrl+x+e、nl、shuf、shuf、last。
    2013-10-10
  • Linux 下載安裝VSCode 使用編程輸出當(dāng)前時(shí)間的方法

    Linux 下載安裝VSCode 使用編程輸出當(dāng)前時(shí)間的方法

    這篇文章主要介紹了Linux 下載安裝VSCode 使用編程輸出當(dāng)前時(shí)間,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • linux更改目錄顯示顏色實(shí)例詳解

    linux更改目錄顯示顏色實(shí)例詳解

    這篇文章主要介紹了linux更改目錄顯示顏色實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Yum安裝中出現(xiàn)錯(cuò)誤mirrorlist.txt的解決方法

    Yum安裝中出現(xiàn)錯(cuò)誤mirrorlist.txt的解決方法

    前幾天在使用Yum install命令安裝軟件的時(shí)候一直提示一個(gè)mirrorlist.txt錯(cuò)誤,通過(guò)查找相關(guān)的資料現(xiàn)在找到了解決的方法,所以這篇文章就整理出來(lái)和大家分享一下,有同樣遇到這個(gè)問(wèn)題的朋友們可以參考借鑒下。
    2016-10-10
  • Linux中selinux基礎(chǔ)配置教程詳解

    Linux中selinux基礎(chǔ)配置教程詳解

    SELinux(Security-Enhanced Linux)的簡(jiǎn)單配置,涉及SELinux的工作模式、配置文件修改、查看和修改上下文信息,以及恢復(fù)文件或目錄的上下文信息。這篇文章主要介紹了Linux中selinux基礎(chǔ)配置,需要的朋友可以參考下
    2018-11-11
  • 詳解Ubuntu下配置samba實(shí)現(xiàn)文件夾共享

    詳解Ubuntu下配置samba實(shí)現(xiàn)文件夾共享

    這篇文章主要介紹了詳解Ubuntu下配置samba實(shí)現(xiàn)文件夾共享的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Ubuntu 20.04 CUDA&cuDNN安裝方法(圖文教程)

    Ubuntu 20.04 CUDA&cuDNN安裝方法(圖文教程)

    這篇文章主要介紹了Ubuntu 20.04 CUDA&cuDNN安裝方法(圖文教程),文中通過(guò)圖文代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • CentOS7 systemd添加自定義系統(tǒng)服務(wù)的方法

    CentOS7 systemd添加自定義系統(tǒng)服務(wù)的方法

    這篇文章主要介紹了CentOS7 systemd添加自定義系統(tǒng)服務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-03-03
  • Linux通過(guò)設(shè)備號(hào)找到設(shè)備的方法

    Linux通過(guò)設(shè)備號(hào)找到設(shè)備的方法

    所有的Linux設(shè)備文件均位于/dev目錄下,是根(/)文件系統(tǒng)的一個(gè)組成部分,因?yàn)檫@些設(shè)備文件在操作系統(tǒng)啟動(dòng)過(guò)程中必須可以使用。接下來(lái)通過(guò)本文給大家介紹Linux通過(guò)設(shè)備號(hào)找到設(shè)備的方法,感興趣的朋友一起看看吧
    2018-07-07

最新評(píng)論