2

I am using tail -f (in Terminal on Mac OS X el capitan) to view the live changes to my file (the results of pulling data from a database using a PERL script). However, sometimes, the Perl script will truncate the file and add new data to it. Sometimes, when this happens, it gives me this message:

tail: test.txt: file truncated

And then does not show any contents of the file afterwards. This seems to only happen when I'm replacing a file with LESS rows than before. When the new rows are longer than before running the script, I do not get this error and the tail -f continues to work. I have confirmed that there are, in fact, data in the file that tail -f is not showing after getting this (error?) message.

I've seen this similar question: Suppress 'file truncated' messages when using tail

tail -f test.txt 2> /dev/null

But that just suppresses the message and still breaks, it doesn't continue to show me the shorter, truncated file contents.

Is there a better command to use to live view changes to the file? Or a flag for tail -f to not care when the file is truncated?

3
  • 4
    Have you tried tail -F which is the same as -f but also implies --retry option
    – AlexD
    May 10, 2017 at 15:17
  • 1
    Just tried, but it didn't work. Same thing as above even with -F -- it doesn't display contents of new file (when it is shorter results). Just stops at the File Truncate message and doesn't return to terminal until I command+C. I find it very weird that it always works when the results are longer than the previous results. It's always truncating the file, regardless.
    – ProGrammar
    May 10, 2017 at 15:31
  • As stated in comments below by @roaima, Macs don't have --retry...
    – ProGrammar
    May 10, 2017 at 15:53

4 Answers 4

5

As others have pointed out, the tail command that ships with OS X does not have the --retry option. However, you could simply install the GNU version of tail which has that option; it is part of the GNU coreutils. For example, if you use MacPorts you could install them by running sudo port install coreutils.

An alternative to live watching a file is the watch command, which unfortunately also doesn't ship with OS X. However, you can use this simple workaround.

1
1

Here's a tail-F script. It takes a single argument - the file to tail. There are probably better options (such as installing GNU tail), but it's here to demonstrate that simple versions of tools can often be built from the parts available.

#!/bin/bash
#
file="$1"
size=$(stat -c '%s' "$file" 2>/dev/null)
test -z "$size" && echo "No file '$file'" >&2

while sleep 1
do
    n_size=$(stat -c '%s' "$file" 2>/dev/null)

    if [[ -n "$n_size" ]]
    then
        if [[ 0 == "$size" ]]
        then
            # Output whole file (so far)
            dd bs=1K if="$file" 2>/dev/null
            size="$n_size"

        elif [[ "$n_size" > "${size:-0}" ]]
        then
            # Output new part of file
            dd bs="$size" skip=1 if="$file" 2>/dev/null
            size="$n_size"

        elif [[ -z "$size" ]]
        then
            echo "New file '$file'" >&2
            size=0

        elif [[ "$n_size" < "${size:-0}" ]]
        then
            echo "Rewinding file '$file'" >&2
            size=0
        fi
    fi
done
-1

Use tail --f=name

The default value of the -f flag is descriptor. Changing it to name, tail will follow the file name and will continue to display even after the file is truncated.

PS. You will still receive the message tail: File truncated

1
  • As pointed out in all three of the other visible answers, this is not the GNU command. The question does very clearly state MacOS.
    – JdeBP
    Jan 10, 2020 at 19:25
-2

The following command keep retry the file even it is removed or inaccessible. It will again prints the contents after creating it.

tail -F filename --retry
3
  • Nope, didn't work. Same thing as above even with -F and --retry -- it doesn't display contents of new file (when it is shorter results). Just stops at the File Truncate message and doesn't return to terminal until I command+C. I find it very weird that it always works when the results are longer than the previous results. It's always truncating the file, regardless.
    – ProGrammar
    May 10, 2017 at 15:34
  • 1
    Mac systems don't have --retry May 10, 2017 at 15:39
  • Is this just not possible then with Mac systems?
    – ProGrammar
    May 10, 2017 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .