> For the complete documentation index, see [llms.txt](https://lack.gitbook.io/operation-and-maintenance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lack.gitbook.io/operation-and-maintenance/linuxri-chang-yun-wei/linuxxia-wen-jian-cha-zhao.md).

# linux下文件查找

## which 命令

`which` 命令：

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

格式：which command

用法：

* which ls ：查看ls命令的文件

```bash
[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&#x20;

```bash
[root@localhost ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@localhost ~]# whereis tree
tree: /usr/bin/tree /usr/share/man/man1/tree.1.gz
```

## locate 命令

`locate` 命令：

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

格式：locate option

用法：

* locate 1.txt

```bash
[root@localhost ~]# locate 1.txt
/etc/pki/nssdb/pkcs11.txt
/usr/share/man/man5/pkcs11.txt.5.gz
/usr/share/vim/vim74/doc/gui_x11.txt.gz
/usr/share/vim/vim74/doc/usr_01.txt.gz
/usr/share/vim/vim74/doc/usr_11.txt.gz
/usr/share/vim/vim74/doc/usr_21.txt.gz
/usr/share/vim/vim74/doc/usr_31.txt.gz
/usr/share/vim/vim74/doc/usr_41.txt.gz
```

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

## 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\*(通配符)'&#x20;
* -a      ：      逻辑与

```bash
find . -type f -perm 600     : 查找当前目录下600权限的所有文件
find . -type f -perm 600 -o -perm 644：查找当前目录下600或644权限的所有文件
find . -type f -perm 600 -a -mtime -2 ：查找当前目录下600权限的两天前的文件
find /etc/ -type f ! -mtime -365 : 找出 /etc/ 目录下，一年内从未变更过的文件
find . -type d -perm 600 |xargs -i mv {} {}.bak  ： 查找当前目录下600权限的目录并输出给mv {}匹配所有输出的文件
find . -type d -perm 600 |xargs rm -r  ： 查找当前目录下600权限的目录并删除
find . -name "*.txt" -exec mv {} {}.bak \;  ：查看当前目录下带txt的文件并修改（注意结尾的\;）
```

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

## stat 命令

`stat` 命令

介绍：查看文件详细信息

格式：stat file

用法：

* stat 1.txt

```bash
[root@localhost ~]# stat 1.txt.bak 
  File: '1.txt.bak'
  Size: 4               Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 67162872    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2018-08-01 22:17:14.280451353 +0800
Modify: 2018-08-01 22:17:36.964735995 +0800
Change: 2018-08-01 22:48:16.970764413 +0800
 Birth: -
```

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

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

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

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

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

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

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

```bash
# ls -lc filename         列出文件的 ctime
# ls -lu filename         列出文件的 atime
# ls -l filename          列出文件的 mtime
[root@localhost ~]# ls -lu 1.txt.bak 
-rwxr-xr-x 1 root root 4 Aug  1 22:17 1.txt.bak
[root@localhost ~]# ls -lc 1.txt.bak 
-rwxr-xr-x 1 root root 4 Aug  1 22:48 1.txt.bak
[root@localhost ~]# ls -l 1.txt.bak 
-rwxr-xr-x 1 root root 4 Aug  1 22:17 1.txt.bak
[root@localhost ~]#
```

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