Bash Command 1: find
GNU find searches the directory tree rooted at each given starting-point by evaluating the given expression from left to right, according to the rules of precedence, until the outcome is known, at which point find moves on to the next file name. If no starting-point is specified,
.is assumed. (fromman find)
可以看出 find 命令的主要组成部分是 expression。
The part of the command line after the list of starting points is the expression. This is a kind of query specification describing how we match files and what we do with files that were matched. An expression is composed of a sequence of things:
Tests
Tests return a true or false value, usually on the basis of some property of a file we are considering. The -empty test, for example, is true only when the current file is empty.Actions
Actions have side effects (such as printing something on the standard output) and return either true or false, usually based on whether or not they are successful. The -print action for example prints the name of the current file on the standard output.Global options
Global options affect the operation of tests and actions specified on any part of the command line. Global options always return true. The -depth option for example makes find traverse the file system in a depth-first order.Positional options
Positional options affect only tests or actions which follow them. Positional options always return true. The -regextype option for example is positional, specifying the regular expression dialect for regular expressions occurring later on the command line.Operators
Operators join together the other items within items within the expression. They include for example -o (meaning logical OR) and -a (meaning logical AND). Where an operator is missing, -a is assumed.If the whole expression contains no actions other than -prune or -print, -print is performed on all files for which the whole expression is true.
The -delete action also acts like an option (since it implies -depth).
我写这篇博客,最主要是为了指出 Tests 中的 -path 选项,我发现 -path 的参数必须是绝对路径。比如,当前路径下有一个文件
problems/migrations/a.py
find . -path "problems/migrations/*.py"
并不能找到这个文件,得写成
find . -path "./problems/migrations/*.py"
manual page 中对 -path 选项的说明如下:
-path 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). To ignore a whole directory tree, use -prune rather than checking every file in the tree. For example, to skip the directorysrc/emacsand all files and directories under it, and print the names of the other files found, do something like this:
find . -path ./src/emacs -prune -o -printNote 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 -printfind 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 the POSIX standard.
第三段中提到的 "the base name of the file" 指的是一个文件的「名字」,不包括该文件的路径。比如,文件 ./problems/migrations/a.py 的 base name 为 a.py。通过 basename 命令即可获得一个文件的 base name。
第二段中的
「the pattern match test」applies to 「the whole file name」starting from one of the start points named on the command line.
大概能解释我遇到的问题:
前面指定了一个 start point .(应视作一个 directory 而非字符 '.'?) 而后面的 pattern 中却不包含 . 这一 directory(即 current directory),所以匹配失败。
这个问题涉及两个关键概念:「the base name of a file」 和 「the whole file name」。
find 命令的工作机理有待深入探究。
Bash Command 1: find的更多相关文章
- Linux:-bash: ***: command not found
Linux:-bash: ***: command not found,系统很多命令都用不了,均提示没有此命令. 突然之间linux很多命令都用不了,均提示没有此命令. 这应该是系统环境变量出现了问题 ...
- pod 命令-bash: --: command not found
pod 命令-bash: --: command not found 升级完系统执行 pod update 或者 pod install命令的时候出现: -bash: --: command not ...
- bash:command not found
在linux下执行某一常用命令时,提示类似错误信息:bash:bash:command not found 原因是所执行的命令在当前系统环境变量里找不到路径. 例如:刚安装了openOffice时,执 ...
- linux下提示bash:command not found
新安装的linux系统,如果进行精简安装可能会出现bash:command not found 的提示,大家在安装的时候可以选择默认安装basic的组件,一般即可.到时候可以再升级. 如果新装的系 ...
- Mac打开Terminal报错-bash : : command not found
问题描述: Mac系统在打开Terminal的时候,报错-bash : : command not found. 问题分析: 报错并不影响Terminal的使用,于是忽略不计.但是在修改.bash_p ...
- 今天领导分享了一个探测端口的命令-linux下提示bash:command not found
今天领导分享了一个探测端口的命令,于是试了一下,提示未找到-bash: nc: command not found 因此决定将bash的命令在复习一下,温故而知新 总结整理于此: 确定你的DNS可以 ...
- Linux下提示命令找不到:bash:command not found
Linux下输入某些命令时会提示:bash:command not found. 首先,查看$PATH中是否包含了这些命令. $PATH:决定了shell到哪些目录中去寻找命令或程序,PATH值是一系 ...
- Centos命令行报bash:.....:command not found的解决办法
命令行报bash:.....:command not found的解决办法(几乎所有命令) 命令行输入命令执行后报“bash:....:command not found”这是由于系统PATH设置 ...
- Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax)
Linux/Unix 指令使用说明的格式介绍(the Bash Command 'Usage' Syntax) 摘自 金马的Blog 原文 http://www.lijinma.com/blo ...
- linux move file / folder bash command
linux move file / folder bash command mv $ which mv $ man mv # mv [-f] source target/ target folder ...
随机推荐
- 你会如何给全局对象添加toString()方法
首先,在讨论如何给所有方法window对象添加toString方法的时候,我们先来说说window的对象继承与对象实例,以及构造函数的this指针,还有变量的提升与方法的调用方式,最终一探window ...
- 为管理复杂组件状态困扰?试试 vue 简单状态管理 Store 模式【转】
https://juejin.im/post/5cd50849f265da03a54c3877 在 vue 中,通信有几种形式: 父子组件 emit/on vuex 中共享 state 跨组件 Eve ...
- 剑指offer46 求1+2+...+n 以及& &&区别
参考代码: class Solution { public: int Sum_Solution(int n) { int result = n; result && (result + ...
- OO作业第二单元总结
目录 一.设计策略 1 2 3 二.程序分析 1 2 3 S.O.L.I.D分析 三.Bug分析 1 2 3 四.互测策略 五.心得体会 一.设计策略 1 第一次完成的是一个傻瓜电梯,简单来说,就是来 ...
- java,编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz.
需求:编写一个从1循环到150并在每行打印一个值,另外在每个3的倍数行上打印出foo,在每个5的倍数行上打印biz,在每个7的倍数上打印baz. package study01; public cla ...
- .NET 中,编译器直接支持的数据类型称为基元类型(primitive type).基元类型和.NET框架类型(FCL)中的类型有直接的映射关系.
.NET 中,编译器直接支持的数据类型称为基元类型(primitive type).基元类型和.NET框架类型(FCL)中的类型有直接的映射关系. The primitive types are Bo ...
- error PRJ0019: 工具从 “正在执行生成后事件... ”
error PRJ0019: 工具从"正在执行生成后事件..." 原因是属性->生成事件->生成后事件 命令行设置错误导致的,修改即可 因为path前面有空格,所以这里 ...
- CentOS7下Mysql5.7主从数据库配置
本文配置主从使用的操作系统是Centos7,数据库版本是mysql5.7. 准备好两台安装有mysql的机器(mysql安装教程链接) 主数据库配置 每个从数据库会使用一个MySQL账号来连接主数据库 ...
- ccf 201803-1 跳一跳(Python实现)
一.原题 问题描述 试题编号: 201803-1 试题名称: 跳一跳 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 近来,跳一跳这款小游戏风靡全国,受到不少玩家的喜爱. 简化 ...
- 如何编写自己的C语言头文件
一些初学C语言的人,不知道头文件(*.h文件)原来还可以自己写的.只知道调用系统库函数时,要使用#include语句将某些头文件包含进去.其实,头文件跟.C文件一样,是可以自己写的.头文件是一种文本文 ...