Find count of files matching a pattern in a directory in linux

前端 未结 6 1331
面向向阳花
面向向阳花 2021-02-02 09:06

I am new to linux. I have a directory in linux with approx 250,000 files I need to find count of number of files matching a pattern.

I tried using following command :

6条回答
  •  失恋的感觉
    2021-02-02 09:54

    You should generally avoid ls in scripts and in fact, performing the calculation in a shell function will avoid the "argument list too long" error because there is no exec boundary and so the ARGV_MAX limit doesn't come into play.

    number_of_files () {
        if [ -e "$1" ]; then
            echo "$#"
        else
            echo 0
        fi
    }
    

    The conditional guards against the glob not being expanded at all (which is the default out of the box; in Bash, you can shopt -s nullglob to make wildcards which don't match any files get expanded into the empty string).

    Try it:

    number_of_files 20061101-20131101_kh5x7tte9n_2010_*
    

提交回复
热议问题