linux-文件查找

查找文件

一般查找find

最常用的方式find PATH -name FILENAME.例如查找当前用户目录下的1.txt文件,find ~ -name 1.txt.同时还可以使用*号匹配.如果想忽略文件名的大小写,可以添加-iname参数.

[zengchao@localhost dir]$ tree
.
├── 1.txt
├── 2.txt
├── A.doc
├── b.doc
└── child
    ├── c1.txt
    └── c2.doc

3 directories, 4 files
[zengchao@localhost dir]$ find ./ -name 1.txt 
./1.txt
[zengchao@localhost dir]$ find ./ -name a.doc 
[zengchao@localhost dir]$ find ./ -iname a.doc
./A.doc
[zengchao@localhost dir]$ find ./ -name '*.txt'
./1.txt
./2.txt
./child/c1.txt

数据库查找:locate

locate命令查找数据会依赖本地的数据库文件,默认每天会检索一遍系统中的文件,然后记录到数据库中.也可以使用命令updatedb手动更新数据库文件.例如下面的操作:

[zengchao@localhost ~]$ touch zengchao_test.file
[zengchao@localhost ~]$ locate zengchao_test.file
[zengchao@localhost ~]$ sudo updatedb
[zengchao@localhost ~]$ locate zengchao_test.file
/home/zengchao/zengchao_test.file
[zengchao@localhost ~]$ rm -rf zengchao_test.file 
[zengchao@localhost ~]$ locate zengchao_test.file
/home/zengchao/zengchao_test.file
[zengchao@localhost ~]$ sudo updatedb
[zengchao@localhost ~]$ locate zengchao_test.file

首先创建了一个文件,然后使用locate去查找,但是并没有查找到,接着我手动更新数据库文件,可以找到该文件.然后我删除刚创建的文件,接着查找还是能找到,然后再次更新数据库文件,这个时候就无法找到了.

which/whereis

which用于从系统的PATH变量所定义的目录中查找可执行的文件绝对路径.例如超找passwd这个命令在系统中的绝对路径.

[zengchao@localhost ~]$ which passwd
/bin/passwd

whereis查找也能查找到器路径,但是与which不用的是,它不但能找出其二进制文件,还能找出相关的man文件.

zengchao@localhost ~]$ whereis passwd
passwd: /usr/bin/passwd /etc/passwd /usr/share/man/man1/passwd.1.gz /usr/share/man/man5/passwd.5.gz

你可能感兴趣的:(linux-文件查找)