FIND returns different results in script

When I execute this line at the command prompt I get a different answer than when I run it in a script? Any ideas on how to resolve? I'm trying to find all files/dir in a directory except files that start with the word file.
Once I get this command to work, I will add the "delete" part to the command. Just trying to make sure I have all the right files listed first.

COMMAND LINE
svdw1234 : find . -mtime -1 ! -name file\*
.
./test_purge
svdw1234 :

SCRIPT

+ find . -mtime -1 ! -name file\*
.
./test_purge
./test_purge/file_test_purge_subdir.txt
./file_sqr_test.txt
./file.txt
+ return_code=0

Thanks,
Barbara

The syntax is kind of oddball, and chances are your interactive script is executed by something like ksh or bash, whereas your script is presumably executed by /bin/sh.

In particular, I imagine the unquoted exclamation mark might have some unseen side effects.

Try fix the find command to adhere to the spec:

find . -mtime -1 -a \! -name file\*

Also investigate whether the PATH is somehow different inside the script, and/or you have functions or aliases which interfere in the interactive shell.

This should work too:

find . -mtime -1 ! -name "*file*"

Regards

Is your shebang line different from your login shell. Maybe the ! is having unwanted side effects and since your are interested only in files add the -type switch too.

find . -mtime -1 -type f ! -name "file*"

I have changed to the shell to match the script and still not good results?

svdw0088 : ksh
$ find . ! -name 'file*' -mtime -1
.
./test_purge
$

SCRIPT first line:

#!/bin/ksh

SCRIPT output:

+ find . ! -name 'file*' -mtime -1
.
./test_purge
./test_purge/file_test_purge_subdir.txt
./file_sqr_test.txt
./file.txt
+ return_code=0

It still shows the files which start with the word file. I do want to exclude directory structures also.
Thanks,
Barbara

Use double quotes:

find . -mtime -1 ! -name "*file*"

Regards

Franklin52: That's not it, single quotes are stronger than double, so the result should be the same (you want to prevent the asterisk from being expanded by the shell).

blt123: can you run the interactive shell with -x too?

prompt$ ksh -x
$ find . ! -name 'file*' -mtime -1
+ find . ! -name file* -mtime -1
.
./test_purge
./test_purge/file_test_purge_subdir.txt
./file_sqr_test.txt
./file.txt
$ exit
+ exit

See the + lines there? You can get them from an interactive session just like from a script. (No need to start a subshell either, you can just say set -x to enable them; set +x to turn them back off.)

The crucial question is whether the find command gets expanded to something unexpected.

Also, can you try with a hard-coded path to your find binary (/usr/bin/find I would guess)?