-1

One of the big reasons why find makes things awesome

if I do this:

find -path *ncf

or this:

find -path ncf*

I get no results, but if I do this:

find -path *ncf.js

I get a matching file. Why is that? Is it a peculiarity of find, or something more grandiose? Does anyone have the home address of the guy who wrote find?

  • it's perfectly normal and it's the correct behaviour of find. – Kiwy Jun 25 '18 at 07:16
  • 1
    Note: Put it in quotes. – ctrl-alt-delor Jun 25 '18 at 07:56
  • 2
    Your language is confusing, you mix positive and negative sentiment. Is this an attempt at sarcasm. It does not work, and just causes confusion. – ctrl-alt-delor Jun 25 '18 at 08:05
  • Filename globbing patterns should be quoted if you don't want the shell to expand them. In this case you don't. You want find to get the pattern, not the expanded list of filenames. – Kusalananda Jun 25 '18 at 08:09
  • https://unix.stackexchange.com/questions/55430/why-doesnt-find-show-this-file is not a duplicate, but is related to one of the errors in the example. – ctrl-alt-delor Jun 25 '18 at 08:09
  • Also, check your language. Not knowing how to use a tool requires you to learn how to use it. It does not help to ridicule, use sarcasm, or in other ways blame others for your own failure to understand. – Kusalananda Jun 25 '18 at 08:13

2 Answers2

5

There are two issues here.

  1. You need to escape the *, otherwise it will be processed by the shell (matching files in the current directory, if any):

    find -path \*ncf.js
    

    or

    find -path '*ncf.js'
    
  2. The behaviour you’re seeing comes from the fact that the globbing expression matches against the file path in its entirety, including the extension. (Use -name to match the filename, which still includes the extension.) This isn’t specific to find, try it with ls in the directory containing your files.

Note that you should get into the habit of specifying the start directory, even when it’s .; not all versions of find use that as the default.

Stephen Kitt
  • 434,908
  • 1
    -path matches against the file's full path (like ./foo/bar/ncf.js) while -name matches against the file's name (ncf.js). Beware of different opinions wrt terminology, POSIX makes the distinction path vs name, while for GNU (at least historically) path refers to search paths like $PATH, $MANPATH, while /foo/bar/baz would be a full/whole file name. Hence -wholename instead of -path in earlier versions of GNU find. – Stéphane Chazelas Jun 25 '18 at 06:13
1

I will add because this is not explained in other answer.

find -path "*ncf"
find -path "*ncf.js"

The above are not the same (note: I put the glob in quotes, to stop the shell from expanding it). The reason is: one ends in ncf the other ends in ncf.js. If this is confusing, then you need to drop the idea of file extensions. Unix does not have them: A dot is just a character, it is not treated differently (except if it is the first character, in this case it is hidden: it may not turn up in a directory listing).

ilkkachu
  • 138,973