1

I'd like to search in the directory structure recursively for files of the specific file types. But I need to pass the file types from the external file. The output should be list where each line is absolute path to the file. I will use the output for further processing. The external file where is the list of file types looks for example like this (filter.lst):

*.properties

I've tried this (searchfiles.sh):

while read line
do
 echo "$(find $1 -type f -name $line)"
done < $2

Echo command inside the script is only for the test purpose. I ran the script:

./searchfiles.sh test_scripting filter.lst

The the output of the echo of the find command was empty. Why? I tried to alter the script in the following way to test if the command is built correctly and the files *.properties exist:

while read line
do
 echo "find $1 -type f -name $line"
 echo "$(find $1 -type f -name $line)"
done < $2

I've got output:

./searchfiles.sh test_scripting filter.lst
find test_scripting -type f -name *.properties

If I copy manualy "find test_scripting -type f -name *.properties" and paste it to the shell the files are correctly found:

find test_scripting -type f -name *.properties
test_scripting/dir1/audit.properties
test_scripting/audit.properties
test_scripting/dir2/audit.properties

Why does not "find" command process correctly the variables?

1 Answers1

0

The cause of the strange behaviour were hidden characters in the input filter.lst file. The filter.lst was created in the Windows OS and then copied to the Linux OS. Hence the find command didn't find expected files. Test the input file if it contains hidden characters:

od -c filter.lst
0000000   *   .   p   r   o   p   e   r   t   i   e   s  \r  \n
0000016

The hidden character is "\r". Edit the script to remove hidden characters using sed command in each line.

while read line
do
 echo "$(find $1 -type f -name $(echo $line | sed -e 's/\r$//'))"
done < $2

More about removing hidden characters is in this thread.

Notice: The best way is to run the script in the empty directory. If there is the file with the name e.g. example.properties in the directory where you run the script, the "echo $line" (executed as echo *.properties) will only display the list of .properties files - in this case only file example.properties.

Community
  • 1
  • 1