How to get the number of files in a folder as a variable?

前端 未结 10 1418
粉色の甜心
粉色の甜心 2020-12-28 13:21

Using bash, how can one get the number of files in a folder, excluding directories from a shell script without the interpreter complaining?

With the help of a friend

10条回答
  •  长情又很酷
    2020-12-28 14:12

    The most straightforward, reliable way I can think of is using the find command to create a reliably countable output.

    Counting characters output of find with wc:

    find . -maxdepth 1 -type f -printf '.' | wc --char
    

    or string length of the find output:

    a=$(find . -maxdepth 1 -type f -printf '.')
    echo ${#a}
    

    or using find output to populate an arithmetic expression:

    echo $(($(find . -maxdepth 1 -type f -printf '+1')))
    

提交回复
热议问题