6

I have a script that collects the size of a file that is being constantly fed. I'm echoing its size to a log file (echo 'filesize is $size' > log.txt) so I only have the last size information. So, only one line.

Now, in another terminal, I wanted to tail that log file to see its size increasing in real time. It turns out, tail -f path/to/file gives me the output I want but it keeps jumping to the next line (as expected, I guess).

So, the output is something like:

$ tail -F log.txt 2>/dev/null
filesize is 1.658 GB
filesize is 1.659 GB
filesize is 1.659 GB
filesize is 1.660 GB

I wanted something more like the command "less" in which you don't have the cursor back. Maybe a better example would be "mtr", that keeps updating the information on the screen without going to next line (as opposed to traceroute).

Thank you,

4
  • 3
    If you just want to monitor the file size then try watch du -skh filename.
    – UserASR
    Jan 20, 2017 at 5:16
  • Though this a well explored question, StackOverflow community is solving problems programmatically and request for general software utilities tools are more relevant at superuser.com
    – Inian
    Jan 20, 2017 at 5:20
  • 1
    @Inian questions about Bash and shell programming are perfectly on-topic on Stack Overflow. Even for ones that aren't, unix.stackexchange.com is the better place to redirect people.
    – dimo414
    Jan 20, 2017 at 8:10
  • @ASR, it works great, thanks. Jan 28, 2017 at 1:51

4 Answers 4

9

Use this command.

watch tail -n 1 log.txt
2
  • What will that do exactly? Man page says "-n number The location is number lines"
    – clearlight
    Jan 20, 2017 at 8:20
  • 1
    tail -n 1 shows you the last line of a file, watch tail -n 1 shows you the last line of the file every 2 seconds, and clears the screen before doing so Feb 1, 2017 at 19:14
6
while [ 1 ]; do sleep 1; clear; tail log.txt; done

This does not have the drawback of passing command and arguments to watch (sometimes you need to hop extra loops to make it correctly), and it clears the terminal.

1
  • that's very interesting, using the clear gives the impression the screen around the updated area is not changing. Even though, you could scroll up and see the content repeating, it's very practical. Thanks, Jan 28, 2017 at 2:01
5

You can watch command to monitor the file changes/difference (-d) every n seconds

watch -n 5 -d cat log.txt
15
  • This solution didn't work for me. I am getting log.txt - command not found error. Jan 20, 2017 at 5:32
  • Pretty sure this doesn't work - you still need to run a command. Jan 20, 2017 at 5:33
  • 1
    watch -n 5 -d cat log.txt
    – bishop
    Jan 20, 2017 at 5:33
  • Hmm. "$(<log.txt)" interprets the content of the file as commands. Jan 20, 2017 at 5:36
  • 1
    @codeforester: seems cat is useful for once, thanks!
    – Inian
    Jan 20, 2017 at 5:41
0

The fanciest solution to receive realtime information on a file ist to use inotify

Which is a linux kernel feature to receive notification when a specific file changes. You can either write your own c program which uses the functionality or you simply build a script with the inotify-wait or inotify-watch command. You probably need to install it though. But both are well documented. New versions of tail also use this linux kernel functionality

EDIT: keep in mind that this only helps you with monitoring file events. What you do when such an event occours is not my cup of tea.

PS. Have you considered that the process that writes the file MAYBE only flushes its writing buffer when a line break is present

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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