linux常用命令-查找文件(find/locate)

时间:2022-12-10 14:07:40

find:根据条件查找文件

命令格式:find 查找路径 条件(-name、-perm、-user、-group等) 条件值(正则表达式)

[root@localhost ~]# find / -name my.cnf(在根目录下查找名字等于my.cnf的文件)
/home/test/file/ran/vmaxconf/my.cnf
/home/test/file/ran/conf/my.cnf
/home/test/ems/uif/conf/my.cnf
/home/test/ems/uif/bak/mysql/my.cnf
/home/test/ems/update/backup/uif/conf/my.cnf
/home/test/ems/mysql/my.cnf
/home/test/ems/mysql/mysql-test/suite/federated/my.cnf
/home/test/ems/mysql/mysql-test/suite/rpl/my.cnf

[root@localhost ~]# find / -user test  (在根目录下查找用户为test的文件)
/home/test
/home/test/.bashrc
/home/test/.emacs
/home/test/.bash_logout
/home/test/.bash_profile
/home/test/.bash_history
/home/test/.kshrc
/home/test/bin

常用条件:
-name   filename             #查找名为filename的文件
-perm                             #按执行权限来查找
-user    username            #按文件属主来查找
-group groupname           #按组来查找
-mtime   -n +n                #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime    -n +n                #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime    -n +n                #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-nogroup                        #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                         #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2                #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f          #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]                #查长度为n块[或n字节]的文件

locate :根据文件名查找文件(相当于find -name的作用)

[root@localhost ~]# locate my.cnf  (文件名包含my.cnf)
/home/netnumen/ems/mysql/my.cnf
/home/netnumen/ems/mysql/mysql-test/include/default_my.cnf
/home/netnumen/ems/mysql/mysql-test/suite/federated/my.cnf
/home/netnumen/ems/mysql/mysql-test/suite/rpl/my.cnf
/home/netnumen/ems/uif/bak/mysql/my.cnf
/home/netnumen/ems/uif/conf/my.cnf
/home/netnumen/ems/update/backup/uif/conf/my.cnf
/home/vmax/file/ran/conf/my.cnf
/home/vmax/file/ran/vmaxconf/my.cnf

locate 是从系统中的updatedb数据库中查找文件,updatedb数据库是定时更新的,如果你找的文件在数据库更新后创建或修改的就会找不到