find工具的了解与使用2: -path
find的-path用法
本篇接着"find工具的了解与使用1"继续
[find工具的了解与使用1](https://blog.****.net/weixin_42992444/article/details/106873681)
-path的说明
find中, -path属于一种TESTS, 它用来指定一种匹配的模式. find在指定的目录中找出各种路径的文件, 但用户关注的可能是其中的某些, 那么, 就可以利用-path 指定pattern 来让find只找出符合pattern样式的文件/路径.
man 1 find中, 关于-path这个TESTS的说明如下:
-path pattern (根据完整路径查找文件名为pattern的文件, 从find的starting-point开始对pattern进行匹配)
File name matches shell pattern pattern. The metacharacters do not treat `/' or `.' specially; so, for example,
$ find . -path "./sr*sc"
will print an entry for a directory called `./src/misc' (if one exists).
Note that the pattern match test applies to the whole file name, starting from one of the start points named on the command line. It would only make sense to use an absolute path name here if the relevant start point is also an absolute path. This means that this command will never match anything:
$ find bar -path /foo/bar/myfile -print
Find compares the -path argument with the concatenation of a directory name and the base name of the file it's examining. Since the concatenation will never end with a slash, -path arguments ending in a slash will match nothing (except perhaps a start point specified on the command line).
The predicate -path is also supported by HP-UX find and will be in a forthcoming version of thePOSIX standard.
-path的使用注意事项是, 它是根据完整路径来匹配pattern的, 而且是从starting-point开始,
也就是说, find 找出的东西 是 一个关于文件路径的字符串(假设叫 fn_str), 而pattern必须从这个串(fn_str)的起点字符开始的一个串, 不能是fn_str的中间一部分的字串. 这样才能find出东西. 可以看看下面图中的几个例子:
下图中
find . -path a1找不到任何东西, 但find . -path ./a1则可以, 因为 find的查找起点(starting-point)是.(当前目录), 所以, 要想-path pattern匹配到东西, pattern必须是.(点)开头的一个串.
参考
https://cloud.tencent.com/developer/article/1375092
https://cloud.tencent.com/developer/article/1375268
https://cloud.tencent.com/developer/article/1375110