【Linux基础】find在linux中查找文件
find作用
find主要用来查找文件(根据文件的名称查找,不能查找文件的内容)
find - search for files in a directory hierarchy/在目录层次结构中搜索文件
主要参数
-type 根据文件类型查找文件
-name 根据名称查找文件,固定写法 最好加上双引号
-iname 根据名称查找文件,不区分大小写,固定写法 最好加上双引号
-mtime 根据修改时间查找文件 +7 查找七天前文件 -10查找10天内文件 8查找往前数的第八天的文件
-size 根据文件大小查找文件 +10M 大于10M -1G小于1G 20M 等于20M
-inum 根据inode号查找文件
案例
#案例一 根据文件类型来查找文件
[root@wql ~]# pwd
/root[root@wql ~]# mkdir wql[root@wql ~]# cd wql[root@wql wql]# touch {1..3}.txt
[root@wql wql]# touch {1..3}.log
[root@wql wql]# touch {1..3}.TXT
[root@wql wql]# mkdir dir-{1..3}[root@wql wql]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.TXT
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.TXT
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.TXT
drwxr-xr-x 2 root root 6 Apr 8 01:56 dir-1
drwxr-xr-x 2 root root 6 Apr 8 01:56 dir-2
drwxr-xr-x 2 root root 6 Apr 8 01:56 dir-3默认不加路径参数 即为当前路径
[root@wql wql]# find -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# find . -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# find ./ -type f
./1.txt
./2.txt
./3.txt
./1.log
./2.log
./3.log
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]# 默认rm跳过了.目录[root@wql wql]# find -type d
.
./dir-1
./dir-2
./dir-3
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# find -type d | xargs rm -rf
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’
[root@wql wql]# ll
total 0
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 1.TXT
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 2.TXT
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.log
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.txt
-rw-r--r-- 1 root root 0 Apr 8 01:56 3.TXT
[root@wql wql]# [root@wql wql]# rm -rf .
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘.’
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# rm -rf ..
rm: refusing to remove ‘.’ or ‘..’ directory: skipping ‘..’
[root@wql wql]# find ./ -type f -name "*.txt"
./1.txt
./2.txt
./3.txt
[root@wql wql]#
[root@wql wql]# find ./ -type f -iname "*.txt"
./1.txt
./2.txt
./3.txt
./1.TXT
./2.TXT
./3.TXT
[root@wql wql]#
#支持变量和通配符
[root@wql wql]# find ./ -type f -name "*$wql"
./1.txt
./2.txt
./3.txt
[root@wql wql]# #精确查找不带""未报错
[root@wql wql]# find ./ -type f -name 1.txt
./1.txt
[root@wql wql]#
#但是使用通配符后 不带"" 报错
[root@wql wql]# find ./ -type f -name *.txt
find: paths must precede expression: 2.txt
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
[root@wql wql]#
#案例根据inode号查找文件
[root@wql wql]# ll -i
total 0
33592651 -rw-r--r-- 1 root root 0 Apr 8 01:56 1.log
33592648 -rw-r--r-- 1 root root 0 Apr 8 01:56 1.txt
33592654 -rw-r--r-- 1 root root 0 Apr 8 01:56 1.TXT
33592652 -rw-r--r-- 1 root root 0 Apr 8 01:56 2.log
33592649 -rw-r--r-- 1 root root 0 Apr 8 01:56 2.txt
33592655 -rw-r--r-- 1 root root 0 Apr 8 01:56 2.TXT
33592653 -rw-r--r-- 1 root root 0 Apr 8 01:56 3.log
33592650 -rw-r--r-- 1 root root 0 Apr 8 01:56 3.txt
33592656 -rw-r--r-- 1 root root 0 Apr 8 01:56 3.TXT
[root@wql wql]#
[root@wql wql]# find -inum 33592651
./1.log
[root@wql wql]#
[root@wql wql]# cat find ./ -type f
123
456
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# touch {1…3}.log
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# find ./ -type f -or name “.txt"
find: paths must precede expression: name
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path…] [expression]
[root@wql wql]# find ./ -type f -or -name ".txt”
./1.txt
./2.txt
./1.log
./2.log
./3.log
[root@wql wql]#
[root@wql wql]#
[root@wql wql]# find ./ -type f -name “.txt"
./1.txt
./2.txt
[root@wql wql]# find ./ -type f -name ".txt” -or -name “*.log”
./1.txt
./2.txt
./1.log
./2.log
./3.log
[root@wql wql]#