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

前端 未结 6 1329
面向向阳花
面向向阳花 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 10:03

    The MacOS / OS X command line solution

    If you are attempting to do this in the command line on a Mac you will soon find out that find does not support the -printf option.

    To accomplish the same result as the solution proposed by fedorqui-supports-monica try this:

    find . -name "pattern_*" -exec stat -f "." {} \; | wc -l
    

    This will find all files matching the pattern you entered, print a . for each of them in a newline, then finally count the number of lines and output that number.

    To limit your search depth to the current directory, add -maxdepth 1 to the command like so:

    find . -maxdepth 1 -name "196288.*" -exec stat -f "." {} \; | wc -l
    

提交回复
热议问题