linux下文件查找

which 命令

which 命令:

介绍:搜索文件路径(PATH中定义且有权限)

格式:which command

用法:

  • which ls :查看ls命令的文件

[root@localhost ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
[root@localhost ~]# which tree
/usr/bin/tree
[root@localhost ~]#

whereis 命令

whereis 命令:

介绍:查看文件路径和相关包,实际是内部有定时搜索

格式:whereis command

用法:

  • whereis ls

locate 命令

locate 命令:

介绍: 不精切的查找,包含字符就满足(/tmp下除外)需要yum install -y mlocate 安装对应的软件包,使用updatedb 命令更新内部数据库后才能查找

格式:locate option

用法:

  • locate 1.txt

只要路径中符合查找的字段的后会输出

find 命令

find 命令(重要):

介绍:查找文件

格式:find path [option] string

用法:

  • find /dir/ -name 'filename'|'filename*(通配符)' :下指定目录下按名查找

  • find /dir/ -tpye d|s|l|p|f :按文件类型查找

  • find /dir/ -mtime [-|+]days : 按天查找(+为变更天数以前,-为多少天以内,以目前时间为参考点)

  • find /dir/ -mmin [-|+]min : 按分钟找

  • find /dir/ -inum num : 按inode查找

  • find /dir/ -name -tpye : 复合条件查找

  • find /dir/ -perm num : 权限查找

  • find /dir/ -maxdepth 1 : 只限当前目录(不包括子目录)

  • find /dir/ -size [-|+]10[k|M|G] : 按大小查找

  • -o : 逻辑或 `find /dir/ -name 'filename'|'filename*(通配符)'

  • -a : 逻辑与

find 命令中用到按时间查找,linux文件有三个类型时间,用stat命令查看

stat 命令

stat 命令

介绍:查看文件详细信息

格式:stat file

用法:

  • stat 1.txt

文件的 Access time,atime 是在读取文件或者执行文件时更改的。

文件的 Modified time,mtime 是在写入文件时随文件内容的更改而更改的。

文件的 Change time,ctime 是在写入文件、更改所有者、权限或链接设置时随 Inode 的内容

更改而更改的。因此,更改文件的内容即会更改 mtime 和 ctime,但是文件的 ctime 可能会

在 mtime 未发生任何变化时更改,如权限更改了但文件内容没有更改。

如何获得一个文件的atime mtime 以及ctime?

ls(1) 命令可用来列出文件的 atime、ctime 和 mtime。

atime不一定在访问文件之后被修改,因为:使用ext3文件系统的时候,如果在mount的时候使用 了noatime参数那么就不会更新atime的信息。而这是加了 noatime 取消了, 不代表真实情況。 反正, 这三个 time stamp 都放在 inode 中. 若 mtime, atime 修改, inode 就一定會改, 既然 inode 改了, 那 ctime 也就跟著要改了(理论上是这样的,但是真实情况并非如此,如果 是读取文档或者执行二进制文件的时候,虽然atime会变,但ctime不变,这是系统这样设计的).

最后更新于

这有帮助吗?