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

前端 未结 10 1258
粉色の甜心
粉色の甜心 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 13:56

    REmove the qoutes and you will be fine

    0 讨论(0)
  • 2020-12-28 13:59

    Expanding on the accepted answer (by Dennis W): when I tried this approach I got incorrect counts for dirs without subdirs in Bash 4.4.5.

    The issue is that by default nullglob is not set in Bash and numdirs=(*/) sets an 1 element array with the glob pattern */. Likewise I suspect numfiles=(*) would have 1 element for an empty folder.

    Setting shopt -s nullglob to disable nullglobbing resolves the issue for me. For an excellent discussion on why nullglob is not set by default on Bash see the answer here: Why is nullglob not default?

    Note: I would have commented on the answer directly but lack the reputation points.

    0 讨论(0)
  • 2020-12-28 14:00
    ls -l | grep -v ^d | wc -l
    

    One line.

    0 讨论(0)
  • 2020-12-28 14:04

    The quotes are causing the error messages.

    To get a count of files in the directory:

    shopt -s nullglob
    numfiles=(*)
    numfiles=${#numfiles[@]}
    

    which creates an array and then replaces it with the count of its elements. This will include files and directories, but not dotfiles or . or .. or other dotted directories.

    Use nullglob so an empty directory gives a count of 0 instead of 1.

    You can instead use find -type f or you can count the directories and subtract:

    # continuing from above
    numdirs=(*/)
    numdirs=${#numdirs[@]}
    (( numfiles -= numdirs ))
    

    Also see "How can I find the latest (newest, earliest, oldest) file in a directory?"

    You can have as many spaces as you want inside an execution block. They often aid in readability. The only downside is that they make the file a little larger and may slow initial parsing (only) slightly. There are a few places that must have spaces (e.g. around [, [[, ], ]] and = in comparisons) and a few that must not (e.g. around = in an assignment.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-28 14:10

    file_num=$(ls -1 --file-type | grep -v '/$' | wc -l)

    this is a bit lightweight than a find command, and count all files of the current directory.

    0 讨论(0)
提交回复
热议问题