2

I have used find command in many directories but when running the following from /etc:

find /etc -name *.conf

it errors out and cannot figure it out. It is only happening with the /etc directory. I can run this exact command from any other directory and works fine. Call this an OCD thing, I just have to know why.

Thomas
  • 6,362
Darwin
  • 23
  • add the error msg to question, might give a clue.. is it permission denied? – Sundeep Jul 02 '17 at 15:14
  • find: bad option coreadm.conf (btw OS is Solaris 10) – Darwin Jul 02 '17 at 15:15
  • Always quote wildcards like "*.conf"when using them in find - else the shell will try to expand them against the current directory before calling find – steeldriver Jul 02 '17 at 15:17
  • try quoting the argument.. find /etc/ -name '*.conf' ... the directory you ran it from likely has the file named coreadm.conf – Sundeep Jul 02 '17 at 15:17
  • using the quotes resolved it, but does still leave the question as to why it works everywhere else and have to modify it within /etc – Darwin Jul 02 '17 at 15:19
  • I can do find /var -name within /var and not need to modify the find pattern – Darwin Jul 02 '17 at 15:20
  • 1
    shell processes the text you typed on command line before it is executed... globs will get expanded if it matches file names in current working directory, double quoted strings will be evaluated for command/variable/history substitutions, file redirections and so on.. you see the error only from /etc directory because *.conf matched the file in that directory named coreadm.conf... – Sundeep Jul 02 '17 at 15:30
  • I also ran within /etc directory: find /etc -name *s. This also gave an error, but with the file certs. This is resolved using quotes again, but why does /etc directory require the modification and no other directory does – Darwin Jul 02 '17 at 15:33
  • 1
    The difference is not related to matches in the directory you run it against (in this case /etc), it is whether there are any matches in the directory you run it from (i.e. your PWD) – steeldriver Jul 02 '17 at 15:39

2 Answers2

4

The problem is that the shell expands your option "*.conf" before executing the command.

use find /etc -name "*.conf" instead

Bonsi
  • 141
  • adding the quotes does allow it to work, but wanting to know why /etc is the only directory that requires modification when doing a pattern search with the find command – Darwin Jul 02 '17 at 15:22
  • Most likely because there is no .conf - file directly in other directories. – Bonsi Jul 02 '17 at 15:25
  • so is it because the search I am doing is .conf? I go into, say, /var and do: find /var -name *s and has no problem finding all words ending in "s". Only the /etc directory requires quotes around the pattern. – Darwin Jul 02 '17 at 15:27
  • Try it with a wildcard that matches two files. The shell will then expand this to find /var -name local mail(i used find /var -name *l)

    This has nothing to to with the directory, rather with the contens of that directory

    – Bonsi Jul 02 '17 at 15:34
  • ahh, I see what you mean now by the contents determine the need for modification of the search – Darwin Jul 02 '17 at 15:38
0

The metacharacters ('*', ?' and[]') match a `.' at the start of the base name (this is a change in the findutils-4.4.2). Basically, you just exclude the '.' to get the result your looking for:

find /etc -name *conf

  • It's true GNU find's -name used to skip hidden files (and that was fixed in 4.2.2 in 2004, not in 4.4.2) but I can't see what that has to do with this Q&A. In any case, that *conf has to be quoted in a Unix shell command line for it not to be expanded as a glob. – Stéphane Chazelas Jul 02 '17 at 17:58