6.搜索文件

1.which

功能:查看命令的存放路徑

示例:

[root@localhost ~]# which netstat
/bin/netstat
[root@localhost ~]# which cd
/usr/bin/which: no cd in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) #cd不在證明cd是內建命令


2.find

功能:在目錄結構中搜索文件,並執行指定的操作。此命令提供了相當多的查找條件,功能很強大。

特點:精確查找,磁盤搜索,IO讀寫,cpu開銷相對較大

語法: find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

常用選項:

-name: 按照文件名查找,支持*號和[]號。

-iname:忽略大小寫按照文件名查找

-perm:按照文件權限來查找,支持完全指定和-號、+號部分符合。

-prune:使用這一選項可以使find命令不在當前指定的目錄中查找,如果同時使用-depth選項,那麼-prune將被find命令忽略。

-user:按照文件屬主來查找

-group:按照文件所屬的組來查找

-mtime :-n +n按照文件的更改時間來查找

-amin(atime): -n +n按照文件的訪問時間來查找

-cmin (ctime):-n +n按照文件狀態的更改時間來查找

stat +文件名 可以查看amc文件時間

- n代表n天以內,+n代表那天以前,n代表當天

如下面的例子:

25 26 27 28 29 30 31 (當天爲28)

n=3 表示找出28號的文件

+3 表示找出25、26、27號的文件

-3 表示找出29、30、31號的文件

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

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

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

-type:按照文件類型查找

b - 塊設備文件。

d - 目錄。

c - 字符設備文件。

p - 管道文件。

l - 符號鏈接文件。

f - 普通文件。

s socket文件

-size n:[c] 查找文件長度爲n塊的文件,帶有c時表示文件長度以字節計。

-depth:在查找文件時,首先查找當前目錄中的文件,然後再在其子目錄中查找。

-fstype:查找位於某一類型文件系統中的文件,這些文件系統類型通常可以在配置文件/etc/fstab中找到,該配置文件中包含了本系統中有關文件系統的信息。

-mount:在查找文件時不跨越文件系統mount點。

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

-regex pattern:對搜索結果的 整個路徑 按正規表達式進行過濾,必須對全路徑考慮,例如結果是./test,那正規表達式應該用"./t.*",而不能用"t.*"

-cpio:對匹配的文件使用cpio命令。

find命令還支持使用邏輯運算符:

-a 類似&& ,連接兩個不同的條件(兩個條件必須同時滿足)

-o 類似|| ,連接兩個不同條件 (兩個條件滿足其一即可)

!,也是非、否

-exec:直接執行後面所跟的命令,不提示

-ok:交互式執行後面所跟的命令

-not:對條件取反

示例:

#列出當前目錄及子目錄的所有文件
[root@localhost home]# find
.
./file5
./old03
./old03/.gnome2
./old03/.bash_logout
./old03/.bash_profile
說明:什麼參數都不加就是列出當前目錄及子目錄的所有文件,也可以這樣寫find .  ,find . -print

#查找特殊目錄文件或路徑
[root@localhost scripts]# find ./test -name "*.txt"
./test/test.txt
./test/town.txt
./test/number.txt
./test/name.txt
[root@localhost scripts]# find ./test -iname "*.txt"  #i忽略大小寫
./test/test.txt
./test/town.txt
./test/number.txt
./test/AV.txt
./test/name.txt

#限制目錄查找的深度
需要用到選項maxdepth
[root@localhost ~]# find . -maxdepth 2 -name "*.vim"  
./.vim_old/.vim
./.vim
[root@localhost ~]# find . -maxdepth 3 -name "*.vim"
說明:maxdepth後接的數字代表層級,在當前目錄下,vim_old爲第一層,.vim到第二層就結束了。跟這個選項還有一個相對的mindepth,這個是從子目錄往上找,maxdepth是從父目錄往下找。

#反向查找
[root@localhost ~]# find . -not -name "*.vim"
[root@localhost ~]# find .  ! -name "*.vim"
[root@localhost ~]#find . \( ! -name '*log*' -a ! -name '*cfg*' \) #找到不包含log和cfg的文件
說明:! -not都是代表不包含.vim後綴的文件

#以文件類型及執行其他命令查找
[root@localhost ~]# find . -type f -mtime -1 -print  #在當前文件夾下搜索最近1小時被修改的文件並打印
[root@localhost ~]# find . -type d -name ".svn"|xargs rm -rf  #刪除.svn目錄
[root@localhost ~]# find . -type d -name ".svn" -exec rm -rf {} \;  #刪除.svn目錄
[root@localhost ~]#find . -type f -name "*.php" -delete  #搜索並刪除文件
[root@localhost ~]#find ./ -type f -name "*.sh"
f[root@localhost ~]#find /data/bbb -type d \( -name 'city' -o -name 'equipment' -o -name 'soldier'  \) #多目錄查找
[root@localhost ~]#find . -type d -name "*p*"  #遞歸找出帶有p字符的目錄
[root@localhost ~]#find . -type f -exec ls -l {} \; >yourfile #重定向操作
[root@localhost ~]#find . -type d| sort  #列出所有目錄並排序
[root@localhost ~]# find /etc -type f -empty  #查找空文件
[root@localhost ~]#find /etc -type d -empty  #查找空目錄

#以文件修改時間查找
[root@localhost ~]# find . -type f -atime  -7 -print  # #在當前文件夾下搜索最近7天內被訪問過的文件並打印
  1 >date
  2. Mon Aug 23 19:25:44 CST 2010
  3. >find ./ -mtime +22 -a -mtime -54  #列出mtime爲7月的文件,+22表示22天以前就在7月份了,但是前面的日期沒有限定,22天以前包含1-7月份啊,再加個54天以內的,那就是54-22,剛好是7月份的文件。
[root@docker-node5 ~]#find logs -type f -mtime +5 -exec rm {} \;  #在/ l o g s目錄中查找更改時間在5日以前的文件並刪除
[root@docker-node5 ~]#find ./ -mtime +7 -ok rm -f {} \;    #3保留7天以內的文件(7天以前刪掉)
< rm ... ./file8 > ? y
< rm ... ./file7 > ? y
< rm ... ./file6 > ? y
[root@localhost mnt]# find /var/log/ -mtime +3 -type f -print  #找出3天以前被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime -3 -type f -print  #找出3天內被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime 3 -type f -print  #找出第三天被修改過的文檔
[root@localhost mnt]# find /var/log/ -mtime +2 -mtime -4 -type f -print #找出第三天被修改過的文檔

#多條件查找
[root@localhost mnt]# find /etc -name "*.sh" -o -name "*.txt" #在etc目錄下查找sh或txt結尾的文件,滿足其一即可
[root@docker-node5 ~]#find /etc -name "passwd*" -exec grep "sam" {} \; #查找passwd文件,並查看有沒有sam這個用戶。
[root@localhost mnt]# pwd
/mnt
[root@localhost mnt]# find /mnt/ -name abc
/mnt/test/abc
/mnt/abc
[root@localhost mnt]# find /mnt/ -path /mnt/test -prune -o -name abc -print  #查找abc,除過test下的abc
/mnt/abc

#以文件權限查找
[root@localhost ~]#find . -type f -perm 644 -print  #列出具有特定權限的文件
[root@localhost ~]#find . -group root -exec ls -l {} \;  #搜索屬於root組的文件
[root@localhost ~]# find / -perm 2644  #查找有644及s屬性
[root@localhost ~]# find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/umount
/bin/su
/bin/mount
/bin/ping
/bin/ping6
/sbin/unix_chkpwd
/sbin/pam_timestamp_check
[root@localhost ~]# ll /bin/umount 
-rwsr-xr-x. 1 root root 53472 Oct 15  2014 /bin/umount
[root@localhost ~]# find / -maxdepth 1 -perm /u=r
/
/lib64
/boot
/bin
/home
/usr
[root@localhost ~]# find . -user root  

#以文件大小查找
[root@localhost ~]# find . -type f -size +2k  #搜索文件大於2k的文件

#支持正則表達式查找
-regextype 指定所使用的正則表達式類型,可選的有emacs(默認),posix-awk,posix-basic,posix-egrep,posix-extended,喜歡grep -E,就用posix-egrep
用find查找目錄下以1-3位數字命名的文件
$find ./ -regextype posix-egrep -regex '.*/[0-9]{1,3}'
[root@localhost mnt]# find /etc -regex ".*\.\(txt\|sh\)"  #在etc目錄下查找txt及sh結尾的文件
/etc/kde/env/imsettings-kde.sh
/etc/pki/nssdb/pkcs11.txt
/etc/X11/xinit/xinitrc.d/50-xinput.sh
/etc/X11/xinit/xinitrc.d/localuser.sh
/etc/X11/xinit/xinitrc.d/00-start-message-bus.sh
/etc/bash_completion.d/gdbus-bash-completion.sh

#查找隱藏文件
[root@localhost ~]# find ~ -type f -name ".*"
說明:查找家目錄下的隱藏文件

3.whereis

功能:用於程序名的搜索,只搜索程序的二進制文件

[root@localhost ~]# whereis cdcd: /usr/share/man/man1/cd.1.gz

4.locate

功能:在數據庫中查找,速度快,缺點:不精確,會忽略臨時目錄/tmp、/var/tmp

常用選項:

-i:忽略大小寫

-n:打印查找結果的前n行

示例:

[root@localhost ~]# locate -i /etc/passwd   #報錯,沒有locate命令
-bash: locate: command not found
[root@localhost ~]# yum install -y mlocate   #安裝locate命令
[root@localhost ~]# updatedb  #刷新數據庫
[root@localhost ~]# locate -i /etc/passwd  #不區分大小寫
/etc/passwd
/etc/passwd-
 [root@localhost ~]# locate -n 1 /etc/passwd
/etc/passwd

5.whatis

功能:簡單的解釋幫助

[root@localhost ~]# whatis cdcd [builtins]        (1)  - bash built-in commands, see bash(1)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章