I have a relatively large log file (46M) on a vServer, of which I'm only interested in the most recent part.

How can I download only the last 10% of the file?

The server is running Debian and I'm using Ubuntu locally.

  • 3
    You tagged the post with rsync. Why don't you just download the whole file and keep using rsync to periodically update it from the server? (Log files also compress well, so with rsync -z you'll just need to download 4-5 MB for the entire log file.)
    – user1686
    Aug 18 '13 at 12:42

First get the size of the remote file in bytes:

$ ssh user@host 'stat -c%s FILENAME'    

Calculate 10% of that number, and copy the last ten percent:

$ ssh user@host 'tail -c 5000 FILENAME' > DESTINATION
  • 1
    Excellent. :) Does stat -c%s have any advantage over du -b?
    – n.st
    Aug 18 '13 at 11:15
  • 2
    I guess one could even combine those commands to decrease readability and save one SSH connection: ssh user@host 'f=FILENAME; tail -c $(($(stat -c%s $f)/10)) $f' > DESTINATION
    – n.st
    Aug 18 '13 at 11:43
  • 2
    @n.st: They both end up using the same stat() syscall, which is fast, although du also examines all mounted filesystems using stat() before the actual file. But that's not a difference in practice. ⫽ However, if you need to get just the bytes (like in the one-liner in your last comment), then running stat will be easier because it doesn't output the unnecessary filename, making the one-liner shorter than it would have been with du -b | sed ....
    – user1686
    Aug 18 '13 at 12:42
  • Can similar thing be done for upload ? Like upload only some bytes to server, instead of entire file ?
    – Simplecode
    Aug 6 at 14:41

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.