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

前端 未结 10 1386
粉色の甜心
粉色の甜心 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:09

    How about:

    count=$(find .. -maxdepth 1 -type f|wc -l)
    echo $count
    let count=count+1 # Increase by one, for the next file number
    echo $count
    

    Note that this solution is not efficient: it spawns sub shells for the find and wc commands, but it should work.

提交回复
热议问题