0

I have a directory with files file1.c, file2.c and file3.c. The output of find is:

$ find -name "*.c"
./file1.c
./file2.c
./file3.c

I want to remove the quotes from the argument of -name. So I use set -f:

 $ set -f
 $ find -name *.c
 ./file1.c
 ./file2.c
 ./file3.c
 $ set +f

Since this worked as expected I made a function in .bashrc based on the previous code:

find() {
    set -f
    eval command find $@
    set +f
}

but testing it gave the error:

$ . ~/.bashrc && find -name *c
find: paths must precede expression: file1.c
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression

Why did set -f; find -name *.c ; set +f give error when calling the function, but did not give error when entering the commands in the shell? I'm using GNU find version 4.6.0

  • Remove eval, and use "$(command -v find)" "$@" – cuonglm Feb 12 at 2:08
  • I followed your suggestion but the function does not output any text and I have to stop with Ctrl+C – Sergio Feb 12 at 2:15
  • ah right, command -v find still report your find function, maybe better to rename the function to something else other than find. In any case, I don't think it's a good solution, because *.c still be evaluated before passing to your function, so it won't work. – cuonglm Feb 12 at 2:23
2

You didn't say, but you must be calling the function like this:

find -name *.c

But globbing hasn't been turned off yet, so the shell expands the *.c before the call. So the find command sees '-name' followed by three arguments, thus the error message.

You could use a backslash instead of quotes.

find -name \*.c

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.