AIX find with Variables

I am trying to do a find with a variable but no matter which way I try it does not work. This is aix. Can I get some ideas on what I am doing wrong?

for i in `cat file`;     do find / -type f -name "$i" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name "\$i" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name "\\$i" -exec ls -l {} +; done
for i in `cat file`;     do find / -type f -name "${i}" -exec ls -l {} +; done
for i in `cat file`;     do find / -type f -name "{$i}" -exec ls -l {} +; done

is not too good a starting point for an analysis.

Yes - start with a decent request - details like misbehaviour, error messages, file / directory structures, input data, execution logs, ...

2 Likes
  1. and 4. look okay.
    This loop method is very slow: for each item it needs to scan all available files...
    Turn on debug mode with set -x (turn off with set+x )
1 Like

This the only output I get. Normally my version of find does not give any output when it does not find something so I added the execute option. Thats why it was so difficult to figure out.

find: 0652-083 Cannot execute :: A file or directory in the path name does not exist.
find: 0652-083 Cannot execute :: A file or directory in the path name does not exist.

I figured it out. There were several problems. Since I am using a variable I can not use single quotes so I switched to double quotes. This created a new problem. Asterisks do not behave as expected in double quotes. It does not matter if you use a one, two, three, or four backslashes.

I have seen seen from a lot of trial and error this works.

for i in `cat file`;     do find / -type f -name "$i"\* -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name $i\* -exec ls -l {} + ; done

This did not work. Is there a way to use an asterisks in double quotes with find?

for i in `cat file`;     do find / -type f -name "$i\*" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name "$i\\*" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name "$i\\\*" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name "$i\\\\*" -exec ls -l {} + ; done

If you want to look for filenames that begin with the value of the i variable:

find / -type f -name "$i*" -exec ls -l {} +

Within the "quotes" the shell expands $i but does not evaluate the * (filename generation).
But the find evaluates the * (for each pathname like the shell would do it).

1 Like

Yes that works. I see where I went wrong. The $i was getting confused.

for i in `cat file`;     do find / -type f -name "$i_string*" -exec ls -l {} + ; done

I needed to do this to protect the $i. Use braces for the $i or take the $i out of the quotes.

for i in `cat file`;     do find / -type f -name "${i}_string*" -exec ls -l {} + ; done
for i in `cat file`;     do find / -type f -name $i"_string*" -exec ls -l {} + ; done

Your first sample is correct.
Your second sample again allows the shell to fully expand $i including file name generation. The $i must be inside "quotes" to avoid it.
Another correct form is

for i in `cat file`;  do find / -type f -name "$i""_string*" -exec ls -l {} + ; done