Here is an alternate way you can solve the problem. Maybe this way will work for you. This only calls find once, making it much faster and more efficient.
stackoverflow.sh
#!/bin/sh
declare -a NAME_LIST=(`cat list2`)
#declare -i LIST_SIZE=${#NAME_LIST[*]}
declare EXPRESSION="find ${PWD} "
# build up the find command
for name in ${NAME_LIST[@]}; do
if test ${NAME_LIST[0]} = ${name}; then
EXPRESSION+="-type f -and -name $name "
else
EXPRESSION+="-or -type f -and -name $name "
fi
done
eval $EXPRESSION
In my enhanced version, $EXPRESSION
is equal to:
find /Users/charlie -type f -and -name one -or -type f -and -name two -or -type f -and -name three -or -type f -and -name four -or -type f -and -name five -or -type f -and -name six -or -type f -and -name seven -or -type f -and -name eight
Of course, your original code works fine for me in bash on OSX (which has the same find command as you do on linux). Obviously, since you're also echoing the name of just the file (and not the path) the output will be a list of both the files and the paths.
The only thing that I think could be causing issues is if your file is unicode encoded and the actual filenames themselves do not have unicode. Otherwise it DOES print the absolute path to the file.
stackoverflow.sh:
#!/bin/sh
NAMES=`cat list2`
for name in $NAMES; do
echo $name >&2
find $PWD -type f -name $name
done
Executing the program:
$ charlie on macbook in ~
❯❯ cat list2
one
two
three
four
five
six
seven
eight
$ charlie on macbook in ~
❯❯ ./stackoverflow.sh
one
/Users/charlie/one
two
/Users/charlie/src/web/two
three
/Users/charlie/misc/three
four
/Users/charlie/four
five
/Users/charlie/Documents/five
six
/Users/charlie/Documents/Ideas/six
seven
/Users/charlie/seven
eight
/Users/charlie/Documents/eight
$ charlie on macbook in ~
❯❯ ./stackoverflow.sh
/Users/charlie/one
/Users/charlie/src/web/two
/Users/charlie/misc/three
/Users/charlie/four
/Users/charlie/Documents/five
/Users/charlie/Documents/Ideas/six
/Users/charlie/seven
/Users/charlie/Documents/eight
$ charlie on macbook in ~
❯❯ ./stackoverflow.sh 2>stderr-output
/Users/charlie/one
/Users/charlie/src/web/two
/Users/charlie/misc/three
/Users/charlie/four
/Users/charlie/Documents/five
/Users/charlie/Documents/Ideas/six
/Users/charlie/seven
/Users/charlie/Documents/eight
$ charlie on macbook in ~
❯❯ cmp list2 stderr-output
$ charlie on macbook in ~
❯❯ echo $?
0