Split a text file in half

I often have the need to split long text files into smaller chunks, and my need requires they are split by line.

Here's my one liner solution:

f=$"filename"; s=$(wc -l $f | awk '{print $1}'); \
h=$(echo "scale=0;" $(($s/2+1)) | bc -q); \
split -l $h $f "output_"; for file in output_*; \
do mv "$file" "$file.txt"; done

Notes:

  • BC can handle floating point numbers, so scale=0 limits any decimal output.

  • Bash math requires double parentheses.