Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
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.) –  grawity Aug 18 '13 at 12:42

1 Answer 1

up vote 8 down vote accepted

First get the size of the remote file in bytes:

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

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

$ ssh user@host 'tail -c 5000 FILENAME' > DESTINATION
share|improve this answer
    
Excellent. :) Does stat -c%s have any advantage over du -b? –  n.st Aug 18 '13 at 11:15
1  
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
1  
@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 .... –  grawity Aug 18 '13 at 12:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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