find如何用
作为linux小白,find还是要会用。
打开find -h,有点发蒙,查看了几个使用方法,做了些试验,然后参考find -h好理解了一点。
在/tmp/test创建几个文件
结构如下
文件 testfile
目录 test1,该目录下面还有一个同名文件 testfile。
可以用ls -R列出的文件结构,也用tree查看一下结构。
root@OpenWrt:/tmp/test# mkdir test1
root@OpenWrt:/tmp/test# cd test1
root@OpenWrt:/tmp/test/test1# touch testfile
root@OpenWrt:/tmp/test/test1# cd ..
root@OpenWrt:/tmp/test# ls -R
.:
test1 testfile
./test1:
testfile
root@OpenWrt:/tmp/test# tree
.
├── test1
│ └── testfile
└── testfile
1 directory, 2 files
然后用以下几个方式查找文件,其中"find .“中的“.”代表当前目录,如果运行当前目下某文件一般用”./"。
root@OpenWrt:/tmp/test# find testfile
testfile
root@OpenWrt:/tmp/test# find -name testfile
./test1/testfile
./testfile
root@OpenWrt:/tmp/test# find . testfile
.
./test1
./test1/testfile
./testfile
testfile
root@OpenWrt:/tmp/test# find . -name testfile
./test1/testfile
./testfile
从结果上可以看到,find的用法
如果find 后面直接写要查找的文件名,只会查找当前目录下的该文件或者文件夹。
如果想遍历整个文件夹,需要用后面加上点东西,比如“.” 或者“-name”都可以。
不用“-name”的结果是列出所有包含要查内容的文件夹,使用“-name”后可以更清晰一点。
发现find -name testfile和find . -name testfile结果是一样的。
试验完成,看看其他的一些用法:
find / -name xyz* --加上通配符*号,则表示查找以xyz开头的文件
-iname 不区分大小写 find . –iname [filename]
-type 按类型进行查找,d查找目录,f查找文件
find . –type d –name [document]
find . type f –name [filename]
-user 按所有者查找文件/目录
find . –user [username]
-mtime -n +n参数表示:按照文件的更改时间来查找文件, -n表示文件更改时间距现在n天以内,+n表示文件更改时间距现在n天以前。这和-size后面的加减号意义一样。
find . –type f –mtime 1 查找1天前修改的文件
find ./ -type f -size +1c -size -1000k 查找1B到1000k的所有文件
然后看看,请看一下help内容
root@OpenWrt:/# find -h
find: unrecognized: -h
BusyBox v1.30.0 () multi-call binary.
Usage: find [-HL] [PATH]... [OPTIONS] [ACTIONS]
Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'
-L,-follow Follow symlinks
-H ...on command line only
-xdev Don't descend directories on other filesystems
-maxdepth N Descend at most N levels. -maxdepth 0 applies
actions to command line arguments only
-mindepth N Don't act on first N levels
-depth Act on directory *after* traversing it
Actions:
( ACTIONS ) Group actions for -o / -a
! ACT Invert ACT's success/failure
ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2
ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2
Note: -a has higher priority than -o
-name PATTERN Match file name (w/o directory name) to PATTERN
-iname PATTERN Case insensitive -name
-path PATTERN Match path to PATTERN
-ipath PATTERN Case insensitive -path
-regex PATTERN Match path to regex PATTERN
-type X File type is X (one of: f,d,l,b,c,s,p)
-perm MASK At least one mask bit (+MASK), all bits (-MASK),
or exactly MASK bits are set in file's mode
-mtime DAYS mtime is greater than (+N), less than (-N),
or exactly N days in the past
-newer FILE mtime is more recent than FILE's
-user NAME/ID File is owned by given user
-group NAME/ID File is owned by given group
-size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))
+/-N: file size is bigger/smaller than N
-prune If current file is directory, don't descend into it
If none of the following actions is specified, -print is assumed
-print Print file name
-print0 Print file name, NUL terminated
-exec CMD ARG ; Run CMD with all instances of {} replaced by
file name. Fails if CMD exits with nonzero
在加一个结合执行操作的find
-exec或者-ok参数:
find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为’command’ { } \;,注意{ }和\;之间的空格。-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
例如找到25天前的文档,并直接删除
find ./ -mtime +25 -exec rm -rf {} \;
例如找到25天前的文档,并逐个确认是否删除:
find ./ -mtime +25 -ok rm {} \;