Counting lines
# create a test file testfile="${HOME}/Desktop/testfile.txt" jot -b 'sample text' 10 | cat -n > "$testfile" printf "%s\n" >> "$testfile" # add an empty line printf "%s\n\r\n\r\n\r\r\n" "sample text" >> "$testfile" # add lines with '\r\n' as line separators printf "%s" "sample text" >> "$testfile" # add a last line without a terminating '\n' open -e "$testfile" function odcfile() { /usr/bin/od -A n -c < "$@" | /usr/bin/sed -E -e 's/^[[:space:]]{11}//' \ -e s/[[:space:]]{4}/$'\001'/g \ -e 's/[[:space:]]+//g' | \ /usr/bin/tr -d '\n' | /usr/bin/tr '\001' ' ' | \ /usr/bin/sed -e s/\\\\n/$'\\\\\\n\\\n'/g return 0 } odcfile "$testfile" # wc -l wc -l < "$testfile" # returns the number of '\n' characters # ed ed -s "$testfile" <<< '=' # all lines # cat cat -n "$testfile" | awk 'END {print $1}' # all lines # grep grep -c $'\n' "$testfile" # all lines grep -c '^.*$' "$testfile" # all lines #grep -c $'\000' "$testfile" grep -c $'\r$' "$testfile" # all lines ending with \r\n grep -c '^[[:space:]]*$' "$testfile" # all blank lines grep -cv '^[[:space:]]*$' "$testfile" # all non-blank lines # sed sed -n '$=' "$testfile" # all lines sed -n /$'\r'$/= "$testfile" | wc -l # all lines ending with \r\n sed -n '/^[[:space:]]*$/=' "$testfile" | wc -l # all blank lines sed -n '/[^[:space:]]/=' "$testfile" | wc -l # all non-blank lines # awk awk 'END {print NR}' "$testfile" # all lines awk '{x++} END {print x}' "$testfile" # all lines awk '/^.*$/ {++x} END {print x}' "$testfile" # all lines awk '/\r$/ {++x} END {print x}' "$testfile" # all lines ending with \r\n awk '/^[[:space:]]*$/ {++x} END {print x}' "$testfile" # all blank lines awk '/[^[:space:]]/ {++x} END {print x}' "$testfile" # all non-blank lines # nl nl "$testfile" | awk 'END {print $1}' nl -b a "$testfile" | awk 'END {print $1}' # including empty lines #--------------------------------------------- # counting the lines of files in a directory DIR=/path/to/dir find "$DIR" -type f -name "*.txt" -print0 | xargs -0 wc -l find "$DIR" -type f -name "*.txt" -print0 | xargs -0 sed -n '$=' find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk 'END {print NR}' find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '{x++} END {print x}' find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/\r$/ {++x} END {print x}' find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/^[[:space:]]*$/ {++x} END {print x}' find "$DIR" -type f -name "*.txt" -print0 | xargs -0 awk '/[^[:space:]]/ {++x} END {print x}' # rather slow, but without the "Argument list too long" issue: /usr/sbin/sysctl kern.argmax # cf. http://www.onlamp.com/pub/a/bsd/2002/03/14/FreeBSD_Basics.html declare -i linecnt=0 while read -d $'\0' file; do #linecnt=$((${linecnt} + $(/usr/bin/sed -n '$=' "${file}"))) let "linecnt += $(/usr/bin/sed -n '$=' "${file}")" # cf. help let done < <(/usr/bin/find "$DIR" -type f -name "*.txt" -print0) echo $linecnt