Wednesday, July 2, 2014

ls command doesn’t work but find works!

Oh no! The ls command does not work as expected! I have the following code in my bash script that will count the total number of txt and pdf file extension.

FILECOUNT=`ls /source_file_path/${1}/*.[Tt][Xx][Tt] /source_file_path/${1}/*.[Pp][Dd][Ff] | wc -l`

Somehow this statement will not work if I schedule it in the cronjob. But it will work if I manually execute the script. Feeling so weird. I was lucky that someone from the forum giving me an advise saying that find command would be a better choice for my use case. Here comes the new code.

FILECOUNT=$(find /source_file_path/${1}/ -maxdepth 1 -name "*.[Pp][Dd][Ff]" -o -name "*.[Tt][Xx][Tt]" | wc -l)

Beware of the maxdepth option, I made a mistake that if I miss this option in the find command, it will return me all files located in the subfolder as well.

No comments: