beta.blog

Linux (Ubuntu)

Bash: Get Remote File Size

by on Jul.30, 2012, under Linux (Ubuntu)

Today I wrote a short script to synchronize some of my folders on my computer with my FTP space. I’m sometimes on a mobile connection which unfortunately is not always stable and it frequently happens that some of my uploaded files are corrupt because the upload didn’t finish.

In order to trick this annoyance I came up with the idea to determine the remote file size and compare it with the local file size in order to do a brief check on whether the full file was uploaded or not. In case the file size would be different I’d simply reupload the affected file and after doing some small tests I believe everything is working fine.

This is how I get the remote file size in my bash script:

size_remote=$(curl -sI "http://www.example.com/testfile.bin" | awk '/Content-Length/ { print $2 }')

(Copy/Paste field):

size_remote=$(curl -sI "http://www.example.com/testfile.bin" | awk '/Content-Length/ { print $2 }')

Later I was able to compare this variable with the size of my local file.

Leave a Comment more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!