What is Best Method to Prepend A Few Lines of Text to a Large File?

Unfortunately there isn't really a good way to prepend information to a file.

All of the "solutions" that I am aware of are basically variants of this:

  1. Take the existing file's data and store it (either in RAM or in a temp file)

  2. Add the new information to the file

  3. Append the old information to the new file

That being said, I bet that would probably be fast enough for your purposes.

Here's a simple prepend.sh shell script which will take whatever input it is given and prepend it to a file, which is define as FILE=

(You can/should edit that line to the appropriate file on your system.)

#!/usr/bin/env zsh -f
# Purpose: add new information to the top of a file
#
# From:	Timothy J. Luoma
# Mail:	luomat at gmail dot com
# Date:	2020-07-10

	## Change this to wherever your file is
FILE="$HOME/Documents/SomeLargeFile.txt"

## Shouldn't need to change anything below this line ##

NAME="$0:t:r"

	# a useful default for macOS
PATH='/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'

	# if the script is given no arguments, don't do anything
if [[ "$#" -gt "0" ]]
then

		# does the file exist and have a size greater than zero bytes?
	if [[ -s "$FILE" ]]
	then

			# if we get here, the answer is yes

			# this lets us use `$EPOCHSECONDS` as a variable
		zmodload zsh/datetime

			# a random temporary filename
		TEMPFILE="${TMPDIR-/tmp/}${NAME}.${EPOCHSECONDS}.$$.${RANDOM}.txt"

			# there's no way that file should exist, but let's remove it if it does
		rm -f "$TEMPFILE"

			# move the existing file to the temporary file
		mv -f "$FILE" "$TEMPFILE"

			# now we take whatever input we are given and save it to the file
		echo "$@" >>| "$FILE"

		## OR, remove the previous line and use this with Keyboard Maestro shell variable
		#
		# echo "$KMVAR_Whatever" >>| "$FILE"

			# now we append all of the information which was previously in the file
			# and delete the temp file
		cat "$TEMPFILE" >>| "$FILE" && rm -f "$TEMPFILE"


	else

			# if we get here, the file didn't exist before, so we just create it

		echo "$@" >>| "$FILE"

	fi
fi

exit 0
#
#EOF

2 Likes