zoukankan      html  css  js  c++  java
  • find+*的问题

    转自find+*的问题


    不久前做移植的时候想把某个目录下的C文件都找出来,然后拷贝下,结果一直报错,我用的是*.c作为pattern。今天看论坛的时候知道为什么了。

    $ ls 

    test2.c  test.c  test.txt

    目录下有两个.c文件,还有一个.txt文件

    $ find . -name *.c

    error : find: 路径必须在表达式之前: test.c
    用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

    $ find . -name *.txt

    ./test.txt

    $ find . -name "*.c"

    ./test2.c
    ./test.c

    原因:

    若是不加引号 $ find . -name *.txt 成功,而不加引号的 $ find . -name *.c 出错了,得看看shell是怎么执行这句话的,首先shell读到了 *.txt 和 *.c,因为 * 是shell认为的 meta (理解为特殊字符),所以就先解释*txt 先执行成 test.txt,传递给find,find就去执行自己的操作,根据pattern发现符合要求。而 *.c 被shell翻译成了test.c和test2.c,这时候命令就变成了 find . -name test.c test2.c 这就出错了!因为find -name 选项后面只能支持一个文件的搜索。所以对于test2.c是前面没有选项,就报错了!

    对于有引号的 find . -name "*.c",shell读到"*.c"的时候,会当作参数处理,传递给find,find之后会自己处理 *.c,可以查看 man find ,里面有

    The metacharacters (`*', `?', and `[]') match a `.'


  • 相关阅读:
    443. String Compression
    506. Relative Ranks
    825. Friends Of Appropriate Ages
    447. Number of Boomerangs
    54. Spiral Matrix
    744. Find Smallest Letter Greater Than Target
    HDU-1565 方格取数(1)
    zoj 3672 Gao The Sequence
    ZOJ 3675 Trim the Nails
    poj -2955 Brackets
  • 原文地址:https://www.cnblogs.com/noble/p/4144192.html
Copyright © 2011-2022 走看看