How do I append text to end of file using the cli on Linux? How to add lines to end of file? How to Linux logs work?! What command I need to type to send the output of the command to end of file?!
You need to use the “>>” to append text to end of file. It is also useful to redirect and append/add line to end of file on Linux or Unix-like system.
Append Text Using >> Operator
Using >> you add a new line of specific text to your text file.
# Append a row in a text file
echo "Lateweb.info is one hell of a good site" >> filename.txt
or
printf "If you like us please subscribe for our youtube channel" >> filename.txt
Code language: PHP (php)
You can also use the cat command to link text from one or more files and add it to another file.
cat /var/log/auth.log >> filename.txt
Code language: JavaScript (javascript)
As you may have guessed, you can use different commands to insert values into additional rows. Another such example is the ls command to list files and folders.
# Add a list of files and folder to filename.txt
ls >> files.txt
Code language: CSS (css)
Difference between > and >>
Typically >
is used when wiping out an existing file is ok. This often means that outputs continually overwrites a file based on the latest current state of things. For instance every time you test a program you might over-write the previous test output.
Typically >>
is used for items such as logging events, parsing or other data processing where data is created or transformed piece by piece into a new form
list
. >
will create/replace the output file with name list
. >>
will create (if file list
not exists already) or append the file list
. Examples:$ ls > allmyfiles.txt
creates the file “allmyfiles.txt” and fills it with the directory listing from the ls command
$ echo "End of directory listing" >> allmyfiles.txt
adds “End of directory listing” to the end of the file “allmyfiles.txt”
$ > newzerobytefile
creates a new zero byte file with the name “newzerobytefile” or overwrites an existing file of the same name (making it zero bytes in size)