“find: paths must precede expression:” How do I specify a recursive search that also finds files in the current directory?

前端 未结 6 1839
谎友^
谎友^ 2020-11-28 01:49

I am having a hard time getting find to look for matches in the current directory as well as its subdirectories.

When I run find *test.c

相关标签:
6条回答
  • 2020-11-28 02:10

    In my case i was missing trailing / in path.

    find /var/opt/gitlab/backups/ -name *.tar
    
    0 讨论(0)
  • 2020-11-28 02:21

    What's happening is that the shell is expanding "*test.c" into a list of files. Try escaping the asterisk as:

    find . -name \*test.c
    
    0 讨论(0)
  • 2020-11-28 02:25

    From find manual:

    NON-BUGS         
    
       Operator precedence surprises
       The command find . -name afile -o -name bfile -print will never print
       afile because this is actually equivalent to find . -name afile -o \(
       -name bfile -a -print \).  Remember that the precedence of -a is
       higher than that of -o and when there is no operator specified
       between tests, -a is assumed.
    
       “paths must precede expression” error message
       $ find . -name *.c -print
       find: paths must precede expression
       Usage: find [-H] [-L] [-P] [-Olevel] [-D ... [path...] [expression]
    
       This happens because *.c has been expanded by the shell resulting in
       find actually receiving a command line like this:
       find . -name frcode.c locate.c word_io.c -print
       That command is of course not going to work.  Instead of doing things
       this way, you should enclose the pattern in quotes or escape the
       wildcard:
       $ find . -name '*.c' -print
       $ find . -name \*.c -print
    
    0 讨论(0)
  • 2020-11-28 02:29

    Try putting it in quotes -- you're running into the shell's wildcard expansion, so what you're acually passing to find will look like:

    find . -name bobtest.c cattest.c snowtest.c
    

    ...causing the syntax error. So try this instead:

    find . -name '*test.c'
    

    Note the single quotes around your file expression -- these will stop the shell (bash) expanding your wildcards.

    0 讨论(0)
  • 2020-11-28 02:31

    I came across this question when I was trying to find multiple filenames that I could not combine into a regular expression as described in @Chris J's answer, here is what worked for me

    find . -name one.pdf -o -name two.txt -o -name anotherone.jpg
    

    -o or -or is logical OR. See Finding Files on Gnu.org for more information.

    I was running this on CygWin.

    0 讨论(0)
  • 2020-11-28 02:32

    Try putting it in quotes:

    find . -name '*test.c'
    
    0 讨论(0)
提交回复
热议问题