6.搜索文件

1.which
php

功能:查看命令的存放路径
node

示例:
正则表达式

[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.finddocker

功能:在目录结构中搜索文件,并执行指定的操做。此命令提供了至关多的查找条件,功能很强大。
数据库

特色:精确查找,磁盘搜索,IO读写,cpu开销相对较大
express

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

经常使用选项:
bash

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

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

-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 表示找出2五、2六、27号的文件

-3 表示找出2九、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)
相关文章
相关标签/搜索