1

I am using the following command to read the last line every time the file changes.

tail -f -n1 Entrie.txt

It happens that the first line written shows it without problems, but later when I write the second it throws the following error

tail: Entrie.txt: file truncated

After showing that error, it shows all the values ​​written in the file. But it is not only that, after writing many lines the error stops coming out and is fixed, but I need it to always work well.

I am running this command in Linux.

I'm adding lines to the file in an editor and saving.

1
1

You can modify file by writing new version of it or appending data to current one.

Example in shell:

  • date > sample_file.txt - recreates file with new text (output of date command)
  • date >> sample_file.txt - appends text to current file

First case scenario - your file has been truncated (content deleted and recreated, it may be done with simple "write" in text editor) and tail warns about this. Second case - tail works as you intended.

3
  • Can't modify the txt from an editor? – Juan Fernando Narvaez Apr 17 '20 at 5:44
  • @JuanFernandoNarvaez: No. Text editor (most of them) creates temporary file so you can edit it and do not mess with original content. When you are saving old file is truncated / deleted and substituted with temporary copy. – DevilaN Apr 17 '20 at 6:02
  • ok, your answer was very helpful. – Juan Fernando Narvaez Apr 17 '20 at 6:06
1

When a text editor is saving a file, what very often happens is that it saves into a temporary file, empties (truncates) the original file, and copies the contents from the temporary file into the original file. It does this to not alter the permissions, ownership and other meta-data on the original file.

This means that from the perspective of tail, the file is truncated (emptied), so it starts showing the last line of the file as the editor is rewriting it (which is why it displays the whole file).

To add lines to the file in such a way that your tail command always shows only the last line of the file and nothing else, you will have to make sure to append lines to the file. Since text editors generally rewrite the file, you may have to do this using other means.

A very basic way of appending data to a file is by using cat:

cat >>Entrie.txt

This would make cat append to the file Entrie.txt. It would wait for input from you, and you would be able to type directly into the file from the terminal. The current line is written to the end of the file as soon as you press Enter. To stop entering data, press Ctrl+D on an empty line.

This obviously does not give you any opportunity to correct what you have written on previous lines once you press Enter though.

Another alternative would be to edit the new lines in a separate file, say additions.txt, with your editor. Save that file and then do

cat additions.txt >>Entrie.txt

This would add the full contents of additions.txt to the end of the file Entrie.txt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.