How to Insert a Line at the Beginning of a File on Linux

In Linux, modifying files from the command line is a common task. Whether you need to prepend configuration parameters, add headers to text files, or make bulk edits, knowing how to insert a line at the beginning of a file can be very useful. This article explores several methods to achieve this efficiently.

In this tutorial you will learn:

  • How to use the sed command to insert a line at the beginning of a file
  • How to use the ed command for file editing
  • How to use shell scripting to prepend a line to a file
  • How to utilize the awk command for this purpose
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based system
Software sed, ed, awk
Other Bash shell
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Methods to Insert a Line at the Beginning of a File

Modifying the start of a file requires different strategies compared to editing other parts. The following methods describe how to insert a line at the beginning using various tools available in Linux.

  1. Using the sed Command: The sed (stream editor) command is a powerful utility for parsing and transforming text.
    sed -i '1i This is the new first line' file.txt

    The -i option edits the file in place. The 1i command tells sed to insert the text at line 1. Replace This is the new first line with the text you want to insert, and filename with the name of your file.

    Using the sed Command to insert first line
    Using the sed Command to insert first line
  2. Using the ed Command: The ed command is a line-oriented text editor. It’s useful for making scripted edits.
    ed -s file.txt << EOF
    0a
    This is the new first line
    .
    w
    q
    EOF

    Here, 0a inserts the text at the beginning. The w command writes the changes to the file, and q quits the editor. Replace This is the new first line with your desired text.

    Using the ed Command to insert first line
    Using the ed Command to insert first line


  3. Using Shell Scripting: A simple shell script can be used to prepend a line to a file.
    { echo 'This is the new first line'; cat file.txt; } > temp && mv temp file.txt

    This command uses a subshell to echo the new line and concatenate the original file content. The result is redirected to a temporary file, which then replaces the original file.

    Using Shell Scripting to insert first line into text file
    Using Shell Scripting to insert first line into text file
  4. Using the awk Command: The awk command is a versatile text processing tool.
    awk 'BEGIN { print "This is the new first line" } { print }' file.txt > temp && mv temp file.txt

    The BEGIN block executes before processing the file, thus printing the new line first. The rest of the file is printed as usual. The output is then saved to a temporary file and moved back to the original file.

    Using the awk Command to insert first line into a file
    Using the awk Command to insert first line into a file

Conclusion

Inserting a line at the beginning of a file in Linux can be accomplished using various tools, each suitable for different scenarios. Whether you prefer sed, ed, shell scripting, or awk, mastering these techniques enhances your ability to manipulate text files efficiently on a Linux system.