To find the file with most of the lines in the current directory and its subdirectories, zsh :
lines() REPLY=$(wc -l < "$REPLY") wc -l -- **/*(D.nO+lined[1])
Defines the lines function that will be used as the glob sorting function, which returns in $REPLY number of lines of the file whose path is specified in $REPLY .
Then we use zsh recursive substitution <T26> to find the correct files ( . ), Numerically ( n ) sorted in reverse order ( O ) with the lines function ( +lines ), and select the first [1] . ( D to enable dotfiles and move dots).
Doing this with standard utilities is a bit tricky if you don't want to make assumptions about what character file names may contain (e.g. newline, space ...). With GNU tools, which can be found on most Linux distributions, this is a little easier since they can work with complete NUL lines:
find . -type f -exec sh -c ' for file do size=$(wc -c < "$file") && printf "%s\0" "$size:$file" done' sh {} + | tr '\n\0' '\0\n' | sort -rn | head -n1 | tr '\0' '\n'
Or with zsh or GNU bash syntax:
biggest= max=-1 find . -type f -print0 | { while IFS= read -rd '' file; do size=$(wc -l < "$file") && ((size > max)) && max=$size biggest=$file done [[ -n $biggest ]] && printf '%s\n' "$max: $biggest" }
source share