need help with counting of files then pass number to variable

hi all,

i'm trying to pass a count of files to a variable thru these set of codes:

sh_count=$(ls -1 fnd_upload_LV*.* |wc -l)

problem is if no files matches that, it will give an error "ls: fnd_upload_LV*.*: No such file or directory".

how do i avoid having the shell script show that error?

thanks

Hi,

Please try below
sh_count=$(ls -1 2> /dev/null |wc -c)

Regards
Arpit

Do you really only want to count files whose names contain a dot?

Don't use ls. The shell will expand the wildcard:

num_files()
{
  [ -e "$1" ] && num_files=$# || num_files=0
}

num_files fnd_upload_LV*
printf "%s\n" "$num_files"
1 Like