搜索
find
find命令用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。
-exec find命令对匹配的文件执行该参数所给出的其他linux命令。相应命令的形式为' 命令 - and' {} \;,注意{ }和\;之间的空格。
-ok 和- exec的作用相同,只不过和会人交互而已,OK执行前会向你确认是不是要执行。
find命令主要参数:
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用了- depth选项,那么-prune选项将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, -n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime选项,但它们都和-mtime选项
相似,所以我们在这里只介绍-mtime选项。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
s - socket文件
-size n[cwbkMG] : 文件大小 为 n 个由后缀决定的数据块。其中后缀为:
b: 代表 512 位元组的区块(如果用户没有指定后缀,则默认为 b)
c: 表示字节数
k: 表示 kilo bytes (1024字节)
w: 字 (2字节)
M:兆字节(1048576字节)
G: 千兆字节 (1073741824字节)
-depth 在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-delete (删除)
-maxdepth 查找最大目录层数 如 1,即只查找一层目录
-fstype 查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件
/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount 在查找文件时不跨越文件系统mount点。
-follow 如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio 对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
-o 是或者的意思
-a 是而且的意思
-not 是相反的意思
列出指定目录下的文件和文件夹
[root@localhost ~]# find /etc/sysconfig/network-scripts
/etc/sysconfig/network-scripts
/etc/sysconfig/network-scripts/ifcfg-lo
/etc/sysconfig/network-scripts/ifdown
/etc/sysconfig/network-scripts/ifdown-bnep
/etc/sysconfig/network-scripts/ifdown-eth
/etc/sysconfig/network-scripts/ifdown-ippp
/etc/sysconfig/network-scripts/ifdown-ipv6
/etc/sysconfig/network-scripts/ifdown-isdn
/etc/sysconfig/network-scripts/ifdown-post
/etc/sysconfig/network-scripts/ifdown-ppp
/etc/sysconfig/network-scripts/ifdown-routes
/etc/sysconfig/network-scripts/ifdown-sit
/etc/sysconfig/network-scripts/ifdown-tunnel
/etc/sysconfig/network-scripts/ifup
/etc/sysconfig/network-scripts/ifup-aliases
/etc/sysconfig/network-scripts/ifup-bnep
/etc/sysconfig/network-scripts/ifup-eth
/etc/sysconfig/network-scripts/ifup-ippp
/etc/sysconfig/network-scripts/ifup-ipv6
/etc/sysconfig/network-scripts/ifup-isdn
/etc/sysconfig/network-scripts/ifup-plip
/etc/sysconfig/network-scripts/ifup-plusb
/etc/sysconfig/network-scripts/ifup-post
/etc/sysconfig/network-scripts/ifup-ppp
/etc/sysconfig/network-scripts/ifup-routes
/etc/sysconfig/network-scripts/ifup-sit
/etc/sysconfig/network-scripts/ifup-tunnel
/etc/sysconfig/network-scripts/ifup-wireless
/etc/sysconfig/network-scripts/init.ipv6-global
/etc/sysconfig/network-scripts/network-functions
/etc/sysconfig/network-scripts/network-functions-ipv6
/etc/sysconfig/network-scripts/ifdown-Team
/etc/sysconfig/network-scripts/ifdown-TeamPort
/etc/sysconfig/network-scripts/ifup-Team
/etc/sysconfig/network-scripts/ifup-TeamPort
/etc/sysconfig/network-scripts/ifcfg-ens192
查找以.repo 结尾的文件名
[root@localhost ~]# find /etc/yum.repos.d -name "*.repo"
/etc/yum.repos.d/CentOS-Base.repo
/etc/yum.repos.d/CentOS-CR.repo
/etc/yum.repos.d/CentOS-Debuginfo.repo
/etc/yum.repos.d/CentOS-Media.repo
/etc/yum.repos.d/CentOS-Sources.repo
/etc/yum.repos.d/CentOS-Vault.repo
/etc/yum.repos.d/CentOS-fasttrack.repo
忽略大小写
[root@localhost ~]# find ./ -name '*.py'
./test.py
[root@localhost ~]# find ./ -iname '*.py'
./test.py
./hello.PY
在当前目录和子目录下查找.ini和.log结尾的文件
[root@localhost ~]# find ./ -name '*.ini' -o -name '*.log'
./config.ini
./tcpdump.log
./files/access.log
匹配文件或路径
[root@localhost ~]# find / -path '*local*'
/boot/grub2/locale
/boot/grub2/locale/ast.mo
/boot/grub2/locale/ca.mo
/boot/grub2/locale/da.mo
/boot/grub2/locale/de.mo
/boot/grub2/locale/de@hebrew.mo
/boot/grub2/locale/de_CH.mo
/boot/grub2/locale/en@arabic.mo
/boot/grub2/locale/en@cyrillic.mo
/boot/grub2/locale/en@greek.mo
/boot/grub2/locale/en@hebrew.mo
/boot/grub2/locale/en@piglatin.mo
/boot/grub2/locale/en@quot.mo
/boot/grub2/locale/eo.mo
/boot/grub2/locale/es.mo
/boot/grub2/locale/fi.mo
/boot/grub2/locale/fr.mo
/boot/grub2/locale/gl.mo
/usr/local/share/info
/usr/local/share/man
/usr/local/share/man/man1
/usr/local/share/man/man1x
/usr/local/share/man/man2
/usr/local/share/man/man2x
/usr/local/share/man/man3
/usr/local/share/man/man3x
/usr/local/share/man/man4
/usr/local/share/man/man4x
/usr/local/share/man/man5
/usr/local/share/man/man5x
/usr/local/share/man/man6
/usr/local/share/man/man6x
/usr/local/share/man/man7
/usr/local/share/man/man7x
/usr/local/share/man/man8
/usr/local/share/man/man8x
/usr/local/share/man/man9
/usr/local/share/man/man9x
/usr/local/share/man/mann
/usr/local/src
/usr/local/mysql
/usr/local/nginx
/usr/local/redis
/usr/local/python
...
查找当前目录不是以.log文件结尾的文件
[root@localhost ~]# find ./ ! -name "*.log"
./
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./.bashrc
./.lesshst
./test.py
./.viminfo
./config.ini
./.finalshellc
./.finalshellc/ssout1_m7zvz
./.finalshellc/ssout1_m7zvz.tar.gz
./.finalshellc/ssout2_m7zvz
./.finalshellc/ssout2_m7zvz.tar.gz
./.finalshellc/nsout_m7zvz
./.finalshellc/nsout_m7zvz.tar.gz
./hello.PY
./files
查找当前目录下不是.log和.py .PY结尾的文件
[root@localhost ~]# find ./ ! -name "*.log" -a ! -iname '*.py'
./
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./.bashrc
./.lesshst
./.viminfo
./config.ini
./.finalshellc
./.finalshellc/ssout1_m7zvz
./.finalshellc/ssout1_m7zvz.tar.gz
./.finalshellc/ssout2_m7zvz
./.finalshellc/ssout2_m7zvz.tar.gz
./.finalshellc/nsout_m7zvz
./.finalshellc/nsout_m7zvz.tar.gz
./files
根据文件类型搜索
- f 普通文件
- l 符号连接
- d 目录
- c 字符设备
- b 块设备
- s 套接字
- p Fifo
[root@localhost ~]# find /root -type d
/root
/root/.finalshellc
/root/files
[root@localhost ~]# find /root -type f
/root/.bash_logout
/root/.bash_profile
/root/.cshrc
/root/.tcshrc
/root/.bash_history
/root/.bashrc
/root/.lesshst
/root/test.py
/root/.viminfo
/root/config.ini
/root/tcpdump.log
/root/.finalshellc/ssout1_m7zvz
/root/.finalshellc/ssout1_m7zvz.tar.gz
/root/.finalshellc/ssout2_m7zvz
/root/.finalshellc/ssout2_m7zvz.tar.gz
/root/.finalshellc/nsout_m7zvz
/root/.finalshellc/nsout_m7zvz.tar.gz
/root/hello.PY
/root/files/access.log
根据文件时间戳进行搜索
UNIX/Linux文件系统每个文件都有三种时间戳:
访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。
变化时间(-ctime/天,-cmin/分钟):文件数据元(例如权限等)最后一次修改时间。
搜索2天内被访问过的所有文件
[root@localhost ~]# find . -type f -atime -2
./.bash_logout
./.bash_profile
./.bash_history
./.bashrc
./test.py
./.viminfo
./config.ini
./tcpdump.log
./.finalshellc/ssout1_m7zvz
./.finalshellc/ssout1_m7zvz.tar.gz
./.finalshellc/ssout2_m7zvz
./.finalshellc/ssout2_m7zvz.tar.gz
./.finalshellc/nsout_m7zvz
./.finalshellc/nsout_m7zvz.tar.gz
./hello.PY
./files/access.log
搜索2天前的那天被访问的文件
[root@localhost ~]# find . -type f -atime 2
搜索访问时间超过3分钟的文件
[root@localhost ~]# find . -type f -amin +3
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bash_history
./.bashrc
./.lesshst
./test.py
./.viminfo
./config.ini
./tcpdump.log
./.finalshellc/ssout1_m7zvz
./.finalshellc/ssout1_m7zvz.tar.gz
./.finalshellc/ssout2_m7zvz
./.finalshellc/ssout2_m7zvz.tar.gz
./.finalshellc/nsout_m7zvz
./.finalshellc/nsout_m7zvz.tar.gz
./hello.PY
./files/access.log
根据文件大小进行匹配
文件大小单元:
b —— 块(512字节)
c —— 字节
w —— 字(2字节)
k —— 千字节
M —— 兆字节
G —— 吉字节
搜索大于100M的文件
[root@localhost ~]# find / -type f -size +100M
/proc/kcore
find: ‘/proc/24958’: 没有那个文件或目录
find: ‘/proc/24967’: 没有那个文件或目录
find: ‘/proc/24972’: 没有那个文件或目录
find: ‘/proc/24981’: 没有那个文件或目录
find: ‘/proc/24983/task/24983/fdinfo/6’: 没有那个文件或目录
find: ‘/proc/24983/fdinfo/5’: 没有那个文件或目录
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/usr/lib/locale/locale-archive
搜索文件小于1k的
[root@localhost ~]# find ./ -type f -size -1k
./config.ini
./hello.PY
./files/access.log
搜索文件等于102M的
[root@localhost ~]# find /usr/lib/locale -type f -size 102M
/usr/lib/locale/locale-archive
根据文件权限搜索
[root@localhost ~]# ls -lh
总用量 8.0K
-rw-r--r--. 1 root root 0 6月 13 07:17 config.ini
drwxr-xr-x. 2 root root 24 6月 13 11:07 files
-rw-r--r--. 1 root root 0 6月 13 11:04 hello.PY
-rw-r--r--. 1 tcpdump tcpdump 24 6月 13 09:40 tcpdump.log
-rw-r--r--. 1 root root 68 6月 13 07:17 test.py
[root@localhost ~]# find ./ -type d -perm 755
./.finalshellc
./files
查找某目录中某用户所拥有的文件
[root@localhost ~]# find / -type f -user ryan
/home/ryan/.bash_logout
/home/ryan/.bash_profile
/home/ryan/.bashrc
/var/spool/mail/ryan
查找某目录中某用户组所拥有的文件
[root@localhost ~]# find / -type f -group ryan
/home/ryan/.bash_logout
/home/ryan/.bash_profile
/home/ryan/.bashrc
借助-exec 选项与其他命令结合使用
找出自己家目录下所有的.txt文件并显示详情
[root@localhost ~]# find $HOME -name '*.log' -exec ls -lh {} \;
-rw-r--r--. 1 tcpdump tcpdump 24 6月 13 09:40 /root/tcpdump.log
-rw-r--r--. 1 root root 0 6月 13 11:07 /root/files/access.log
将.py结尾的文件复制到opt目录中
[root@localhost ~]# find ./ -name '*.py' -exec cp -r {} /opt \;
[root@localhost ~]# ls /opt
test.py
which
which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
whereis
whereis命令用来定位指令的二进制程序、源代码文件和man手册页等相关文件的路径。
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b)、man说明文件(参数-m)和源代码文件(参数-s)。如果省略参数,则返回所有信息。
locate
locate命令和slocate命令都用来查找文件或目录。