find usage

在使用find命令时遇到了如下的error:

>find .  -name *.java
find: paths must precede expression: staticBlock.java
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

>find .  -name \*.java
./objectConvert.java
./webservice/SingleFileHTTPServer.java
./webservice/input.java
./StringMain.java
./staticBlock.java

这里的\有什么作用呢??

原来是这样:

[user@localhost perl]$ set -x; find . -name *.pl
+ find . -name fun.pl json_parser.pl login_client.pl test_perl.pl test_perl_withoutM.pl xmlParser.pl
find: paths must precede expression: json_parser.pl
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
++ printf '\033]0;%s@%s:%s\007' user localhost '~/workspace/perl'
[user@localhost perl]$ find ./ -name fun.pl
+ find ./ -name fun.pl
./fun.pl
++ printf '\033]0;%s@%s:%s\007' user localhost '~/workspace/perl'
[user@localhost perl]$ set -x; find . -name \*.pl
+ set -x
+ find . -name '*.pl'
./fun.pl
./json_parser.pl
./login_client.pl
./test_perl.pl
./test_perl_withoutM.pl
./threads/join.pl
./threads/test.pl
./threads/thread.pl
./xmlParser.pl
++ printf '\033]0;%s@%s:%s\007' user localhost '~/workspace/perl'

使用set -x就发现,find 这个命令显示被shell 解析了一遍,然后将模式匹配后的文件名交给find命令来执行。 但如果这个命令我们放到当前目录的父目录,且目录中没有以.pl结尾的文件时,就会发现第一条命令是可以执行成功的,为什么?因为*在模式匹配文件时,如果没有匹配到文件名,就以字符出现,所以在交给find命令执行时,它仍然是个字符*号(参见shopt nullglob).

[superman@localhost workspace]$ cd ../
[superman@localhost workspace]$ ls *.pl 
空
[superman@localhost workspace]$ shopt -s nullglob
[superman@localhost workspace]$ set -x; find . -name *.pl
+ find . -name
find: missing argument to `-name'
++ printf '\033]0;%s@%s:%s\007' localhost '~/workspace'
[superman@localhost workspace]$ shopt -u nullglob
+ shopt -u nullglob
++ printf '\033]0;%s@%s:%s\007' localhost '~/workspace'
[superman@localhost workspace]$ set -x; find . -name *.pl+ set -x
+ find . -name '*.pl'
./perl/fun.pl
./perl/json_parser.pl
./perl/login_client.pl
./perl/test_perl.pl
./perl/test_perl_withoutM.pl
./perl/threads/join.pl
./perl/threads/test.pl
./perl/threads/thread.pl
./perl/xmlParser.pl
++ printf '\033]0;%s@%s:%s\007'  localhost '~/workspace'

由此可见,当find中使用通配符时,通配符先是被shell处理一遍,然后再交给find命令来执行。

http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4099773&pid=24009926&page=1&extra=#pid24009926


附录:

set 用法:

    set [+abefhkmnptuvxBCEHPT] [+o option-name] [arg ...]
     Without  options,  the name and value of each shell variable are displayed in a format that can be reused as input for setting or resetting the cur‐
     rently-set variables.  Read-only variables cannot be reset.  In posix mode, only shell variables are listed.  The output is sorted according to  the
     current  locale.   When  options  are specified, they set or unset shell attributes.  Any arguments remaining after option processing are treated as
     values for the positional parameters and are assigned, in order, to $1, $2, ...  $n.  Options, if specified, have the following meanings:
            -a      Automatically mark variables and functions which are modified or created for export to the environment of subsequent commands.
      -b      Report the status of terminated background jobs immediately, rather than before the next primary prompt.  This is effective  only  when  job
                      control is enabled.
       -e      Exit  immediately  if  a pipeline (which may consist of a single simple command),  a subshell command enclosed in parentheses, or one of the
               commands executed as part of a command list enclosed by braces (see SHELL GRAMMAR above) exits with a non-zero status.  The shell  does  not
             exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if
             or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any  command  in  a
                      pipeline  but  the  last,  or  if  the command's return value is being inverted with !.  A trap on ERR, if set, is executed before the shell
                      exits.  This option applies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and
                      may cause subshells to exit before executing all the commands in the subshell.
              -f      Disable pathname expansion.
              -h      Remember the location of commands as they are looked up for execution.  This is enabled by default.
              -k      All  arguments  in  the  form  of assignment statements are placed in the environment for a command, not just those that precede the command
                      name.
              -m      Monitor mode.  Job control is enabled.  This option is on by default for interactive shells on systems that  support  it  (see  JOB  CONTROL
                      above).  Background processes run in a separate process group and a line containing their exit status is printed upon their completion.
              -n      Read commands but do not execute them.  This may be used to check a shell script for syntax errors.  This is ignored by interactive shells.

 -x      After expanding each simple command, for command, case command, select command, or arithmetic for command, display  the  expanded  value  of
                      PS4, followed by the command and its expanded arguments or associated word list.


当使用set -f时,通配符在匹配文件名时就不work了,

[superman@localhost perl]$ set -f; set -x; find . -name *.pl; set +x
+ find . -name '*.pl'
./fun.pl
./json_parser.pl
./login_client.pl
./test_perl.pl
./test_perl_withoutM.pl
./threads/join.pl
./threads/test.pl
./threads/thread.pl
./xmlParser.pl
+ set +x
由此可见,使用 set -f的作用与将通配符转义的效果是一样。另外也可以用单引号括起来通配符,以防止其转义。

还有

>find .  -name \*.java -print0
./objectConvert.java./webservice/SingleFileHTTPServer.java./webservice/input.java./StringMain.java./staticBlock.java

这个-print0和-print又有什么区别呢?

>find .  -name \*.java -print
./objectConvert.java
./webservice/SingleFileHTTPServer.java
./webservice/input.java
./StringMain.java
./staticBlock.java

-print0是输出以\0结尾,而不是用newline符号\n.

>echo -e "\0aa"
aa
>echo -e "\naa"

aa
>echo  "\0aa"
\0aa
 


-print0
              True; print the full file name on the standard output,  followed
              by  a  null  character  (instead  of  the newline character that
              -print uses).  This allows file names that contain  newlines  or
              other  types  of white space to be correctly interpreted by pro‐
              grams that process the find output.  This option corresponds  to
              the -0 option of xargs.

例如:

>find .  -name \*.java -print0 | xargs ls
xargs: Warning: a NUL character occurred in the input.  It cannot be passed through in the argument list.  Did you mean to use the --null option?
./objectConvert.java
>find .  -name \*.java -print0 | xargs -0 ls
./objectConvert.java  ./staticBlock.java  ./StringMain.java  ./webservice/input.java  ./webservice/SingleFileHTTPServer.java
>find .  -name \*.java -print0 | xargs -0 ls -l
-rw-r--r-- 1 user users  553 Aug 30 13:23 ./objectConvert.java
-rw-r--r-- 1 user users 1061 Aug 27 09:10 ./staticBlock.java
-rw-r--r-- 1 user users  595 Sep 13 02:41 ./StringMain.java
-rw-r--r-- 1 user users 4059 Aug 28 02:15 ./webservice/input.java
-rw-r--r-- 1 user users 4059 Aug 28 02:12 ./webservice/SingleFileHTTPServer.java




第一步,我们需要安装Firefox30,不能高于30; http://ftp.mozilla.org/pub/firefox/releases/30.0b9/win32/zh-CN/Firefox Setup 30.0b9.exe 安装浏览器后关闭自动升级: 打开Firefox浏览器--打开菜单(右上角三条横杆)--选项--更新--不检查更新 另外可以将反馈选项中的都取消 第二步,安装前端开发人员最普及的开发工具 Firebug-2.0.16 (下载包中有) 安装方法: 打开Firefox浏览器--打开菜单(右上角三条横杆)--附加组件--扩展--点击右上角有个雪花一样的设置按钮--从文件安装附加组件--选择firebug-2.0.16-fx.xpi安装就可以 第三步,安装CSS Usage 0.2.9(下载包中有) 第四步,在Firefox浏览器中打开我们要优化的页面(本地的页面也可以),点击右上角的firebug(有个小蜜蜂图标),打开firebug工具窗口,我们会看到在工具选项中我们有一个 CSS Usage工具的按钮。 首先我们来分析最上面的三个功能按钮的使用 Scan: 通过字面意思我们就能知道,这是一个扫描当前页面的工具,如果我们的站点只有一个页面或者几个页面,我们可以通过使用此功能按键来查看页面的css实用情况. Clear: 清除扫描结果,当我们查看完网页,并对CSS 进行了修改后,我们就不需要以前的扫描结果了,那么我们就可以使用Clear功能键,清除以前的扫描结果缓存,重新开始其他的扫描. AutoScan: 我们的网站可能会有很多的页面,更有可能有很多的弹出层,如果我们每次都点击扫描的话,会占用我们大量的时间,AutoScan功能键可以使我们的扫描工作更自动化,提高我们的工作效率. 简单来说: Scan: 扫描当前页面的CSS使用情况。 Clear:清除扫描结果。 AutoScan:自动扫描每个打开的页面。 我们可以使用AutoScan的叠加结果 然后CSS文件名称后面有个export cleaned css 点击后可导出扫描的结果,结果就是干净的CSS文件。 我们能看到这一句Line CSS Selector Seen, Seen before, Unseen, :hover,告诉我们” 列出CSS选择器的状态: 绿色–表示当前扫描看到的,=保留 深绿色–的以前的扫描中看到的,=保留 红色–的表示在当前和以前扫描中均未发现的.=这部分可以清除 灰色–的代表伪类的选择器CSS,这部分将会被忽视. 如果我们仅仅是给CSS减肥的话,那我们就可以对红色的选择器开始动手了 注意,说了这么久,大家一定知道了CSS Usage是一个扫描冗余CSS样式的工具,可以清理多余的CSS样式, 但是,你一定要扫描足够多的网页,尽可能地找到最多的样式。 同时,CSS Usage还提供给我们关于CSS某个选择器被实用多少次的统计,就是CCS文件后面的(1 scans) 如果我们一直使用 Auto Scan功能的话,我们的Scan次数也是在累积的,比如我们从首页到博文目录、再到图片、最后到达关于我,每一次页面跳转,CSS Usage 都会自动增加页面的扫描次数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值