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

Posted by Jason on Stack Overflow See other posts from Stack Overflow or by Jason
Published on 2012-06-21T03:09:11Z Indexed on 2012/06/21 3:16 UTC
Read the original article Hit count: 101

Filed under:
|

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, I've tried

$files=$(find ../ -maxdepth 1 -type f | sort -n)
$num=$("ls -l" | "grep ^-" | "wc -l")

which returns from the command line:

../1-prefix_blended_fused.jpg: No such file or directory
ls -l :  command not found
grep ^-: command not found
wc -l:   command not found

respectively. These commands work on the command line, but NOT with a bash script.

Given a file filled with image files formatted like 1-pano.jpg, I want to grab all the images in the directory to get the largest numbered file to tack onto the next image being processed.

Why the discrepancy?

© Stack Overflow or respective owner

Related posts about bash

Related posts about shell