Linux查找类命令
1.which命令
which命令:查找可执行文件并显示所在的位置(搜索范围根据path变量指定)
root@localhost ~]# which mkdir
/usr/bin/mkdir
2.whereis命令
whereis命令:用于查找文件(该命令会在特定的范围内查找文件)
[root@localhost /]# whereis mkdir mkdir: /usr/bin/mkdir /usr/share/man/man1/mkdir.1.gz
从左到右分别为查询的程序名、bash路径、bash的man手册路径
3.locate
locate命令:查找合乎范本样式条件的文档或目录
[root@localhost 11-28]# locate mkdir
/root/lianxi/11-22/mkdir.c
/usr/bin/mkdir
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/mkdirlockfile.py
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.opt-1.pyc
/usr/lib/python3.6/site-packages/pip/_vendor/lockfile/__pycache__/mkdirlockfile.cpython-36.pyc
/usr/share/man/man1/mkdir.1.gz
mlocate.db 是locate命令去查询的数据库文件
[root@localhost 11-28]# locate passwd.zip
[root@localhost 11-28]# updatedb
[root@localhost 11-28]# locate passwd.zip
/root/lianxi/11-28/passwd.zip
updatedb为更新数据库(默认情况下每天自动更新一次)
4.find命令
find命令:用来在指定目录下查找文件(默认情况下为当前目录)
格式:find [查找范围] [查找条件] [动作]
-name 按照名字去查找
[root@localhost home]# ls
aa hello.py hello.sh
[root@localhost home]# find /root/home -name hello.sh
/root/home/hello.sh
/root/home/aa/hello.sh
-iname 按照名字去查找(不区分大小写)
[root@localhost home]# ls
aa hello.py hello.sh HELLO.txt
[root@localhost home]# find /root/home -iname hello.txt
/root/home/HELLO.txt
-size 文件大小
[root@localhost home]# find /boot -size +50M 在boot目录下查找大于50M的文件(+大于 -小于)
/boot/initramfs-0-rescue-7ccf57be21544a6b8443991e96ac5a2c.img
-type 文件类型
d:目录
f:普通文件
s:socket文件
l:链接文件
p:管道文件
[root@localhost home]# ll
drwxr-xr-x. 2 root root 38 11月 28 22:40 aa
lrwxrwxrwx. 1 root root 9 12月 2 10:50 halo.txt -> HELLO.txt
[root@localhost home]# find /root/home/ -type l
/root/home/halo.txt
-maxdepth 目录的深度
[root@localhost home]# tree tree/
tree/
├── aa
│ └── aa
│ └── bb
├── bb
│ ├── aa
│ └── bb
└── cc
├── bb
│ ├── aa
│ └── bb
└── cc
[root@localhost home]# find . -maxdepth 3 -name aa
./tree/aa
./tree/aa/aa
./tree/bb/aa
-mtime 多少天修改的 -2(2天内修改) +2(两天前修改)
[root@localhost home]# stat HELLO.txt
文件:HELLO.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:fd00h/64768d Inode:50700104 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2020-12-02 10:44:15.134873102 +0800
最近更改:2020-12-02 10:44:15.134873102 +0800
最近改动:2020-12-02 10:44:15.134873102 +0800
创建时间:-
[root@localhost home]# find . -mtime -2
.
./HELLO.txt
./halo.txt
-newer +对象 查找比当前文件更新的文件
[root@localhost home]# ll
总用量 8
lrwxrwxrwx. 1 root root 9 12月 2 10:50 halo.txt -> HELLO.txt
-rwxr-xr-x. 1 root root 112 11月 28 22:15 hello.py
-rwxr-xr-x. 1 root root 12 11月 28 22:14 hello.sh
-rw-r--r--. 1 root root 0 12月 2 10:44 HELLO.txt
[root@localhost home]# find . -newer hello.sh
.
./hello.py
./HELLO.txt
./halo.txt
5.特殊查找类条件
-a 逻辑与两边的条件都必须满足 -a可以省略
[root@localhost home]# find . -name "hello*" -size -10k
./hello.sh
./hello.py
[root@localhost home]# find . -name "hello*" -a -size -10k
./hello.sh
./hello.py
-o 逻辑或两边的条件任意满足一个
[root@localhost home]# find -name "hello*" -o -type l
./hello.sh
./hello.py
./halo.txt
[root@localhost home]# ll
总用量 8
lrwxrwxrwx. 1 root root 9 12月 2 10:50 halo.txt -> HELLO.txt
-rwxr-xr-x. 1 root root 112 11月 28 22:15 hello.py
-rwxr-xr-x. 1 root root 12 11月 28 22:14 hello.sh
-rw-r--r--. 1 root root 0 12月 2 10:44 HELLO.txt
not 逻辑非!取反
[root@localhost home]# find . ! -name "hello*"
.
./HELLO.txt
./halo.txt
-exec 执行的命令
[root@localhost home]# cat hello.sh
echo hello!
[root@localhost home]# find . -name hello.sh -exec cat {} \;
echo hello!
\表示结束符号 find命令查找结束
{} 代表前面find所查找出来的内容(容器,存放查找出来的内容)