ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Linux下find命令使用

Linux下find命令使用

原创 Linux操作系统 作者:lucyne 时间:2015-08-14 11:13:23 0 删除 编辑

Linux下find命令使用

1. Linux下查找文件介绍:

系统都是由成千上万个不同种类的文件所组成的。其中有系统本身文件,用户文件等等。我们有时候经常忘记或记不清具体是那个目录或是那个文件。Linux中查找文件的命令通常为“find”命令,“find”命令可以帮助我们在使用,管理Linux的日常事务中方便的查找出我们需要的文件。Linux下可以使用“find”命令在目录结构中搜索文件,并执行指定的操作。Linux下“find”命令提供了相当多的查找条件,功能也非常强大。本文将介绍“find”的一些常用命令。

2. find 命令格式

find语法格式为:

find pathname –options [-print -exec -ok ...]

 

参数

说明

pathname

find命令查找的目录路径。.来表示当前目录,用/来表示系统根目录。

-options

find命令命令选项

-print

find命令将匹配的文件输出到标准输出

-exec

find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' {} \;

-ok

和-exec的作用相同,但是在安全模式下执行

 

[-options] 可选参数,其中可选的参数比较多,常用的有如下几个:

参数

说明

-name 

按照文件名查找文件

-perm 

按照文件权限查找文件

-prune

可以指定该参数忽略目录

-user

按照文件用户查找文件

-group

按照文件用户组查找文件

-mtime -n +n

按照文件更改时间查找文件

-type

按照文件类型查找。

b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。

-size

执照文件长度查找,可以通过- + 控制大小

-amin n

查找系统中最后N分钟访问的文件

-atime n

查找系统中最后n*24小时访问的文件

-cmin n

查找系统中最后N分钟被改变文件状态的文件

-ctime n

查找系统中最后n*24小时被改变文件状态的文件

-mmin n

查找系统中最后N分钟被改变文件数据的文件

-mtime n

查找系统中最后n*24小时被改变文件数据的文件

2.1.   find命令可选参数查询实例

环境准备:

[root@db1 opt]# ll

total 132

-rw-r--r--  1 root   root      9130 Mar 12  2011 index_recover.txt

-rw-r--r--  1 root   root      2854 Mar 20  2011 lock.txt

-rw-r--r--  1 root   root        30 Jun 14 08:51 metalink.txt

-rw-r--r--  1 root   root     17750 Mar 12  2011 nobackup_recover.txt

-rw-r--r--  1 root   root     13842 Mar 20  2011 nologging.txt

drwxr-xr-x  2 oracle oinstall  4096 Sep 30 15:46 ora

-rw-r--r--  1 root   root       883 Jun 14 09:37 oracle.txt

drwxr-xr-x  2 root   root      4096 Sep 30 15:48 prune

-rw-r--r--  1 root   root     40079 Mar 12  2011 scn_bak.txt

-rwxr-xr-x  1 root   root     22134 Mar 19  2011 sysstat.txt

 

注:目录ora与目录prune下都存在一个login.sql文件

 

2.1.1.     –name实例

查询名字以“no”开头的文件

[root@db1 opt]# find . -name "no*"

./nobackup_recover.txt

./nologging.txt

 

2.1.2.     –perm 实例

查询权限为755的文件

[root@db1 opt]# find . -perm 755

./ora

./sysstat.txt

./prune

2.1.3.     –user 实例

查询名属主为oracle的文件

[root@db1 opt]# find . -user oracle

./ora

./ora/login.sql

2.1.4.     –group实例

查询名属组为oinstall的文件

[root@db1 opt]# find . -group oinstall

./ora

./ora/login.sql

2.1.5.     –mtime实例

查询1分钟内访问过的文件

[root@db1 opt]# find . -mtime -1

./ora

./prune

./prune/login.sql

./oracle.txt

2.1.6.     –type实例

查询文件类型为目录的

[root@db1 opt]# find . -type d

./ora

./prune

2.1.7.     –size实例

查询文件长度大于100000c的

[root@db1 opt]# find . -size +100000c

./scn_bak2.txt

2.1.8.     其他常用

find / -amin -10 # 查找在系统中最后10分钟访问的文件
 find / -atime -2 # 查找在系统中最后48小时访问的文件
 find / -empty # 查找在系统中为空的文件或者文件夹
 find / -group cat # 查找在系统中属于 groupcat的文件
 find / -mmin -5 # 查找在系统中最后5分钟里修改过的文件
 find / -mtime -1 #查找在系统中最后24小时里修改过的文件
 find / -nouser #查找在系统中属于作废用户的文件
 find / -user oracle #查找在系统中属于FRED这个用户的文件

2.2.   使用exec或ok来执行shell命令

使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找。在有些操作系统中只允许-exec选项执行诸如ls或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。

exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个\,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。

 

使用-exec来执行shell命令

[root@db1 opt]# find . -name "no*" -print -exec ll { } \;

./nobackup_recover.txt

./nologging.txt

 

使用-ok来执行shell命令

[root@db1 opt]# find .   -mtime +5 -ok rm {  } \;

< rm ... ./ora/login.sql > ?

 

查询系统中文件长度为0的文件,并通过exec列出去路径

$ find / -type f -size 0 -exec ls -l { } \;


查询当天修改过的文件

[root@db1 opt]# find . -mtime -1 -type f -exec ls -l {} \;

-rw-r--r--  1 root root 230 Sep 30 15:48 ./prune/login.sql

-rw-r--r--  1 root root 641264 Sep 30 16:16 ./scn_bak2.txt

-rw-r--r--  1 root root 888 Sep 30 16:10 ./oracle.txt

 


查询更改时间在1日以前的文件并删除它们

root@db1 opt]# find . -type f -mtime +1 -exec -ok rm {} \;

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26390465/viewspace-1771284/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
. . 一个 DBA. . .

注册时间:2012-01-06

  • 博文量
    84
  • 访问量
    783162