Using fetch to see if a file exists

I'm trying to verify if a URL for downloading a file is correct, but don't actually want to download that file.

I thought I would be able to use fetch for that but don't see such an option.

Any suggestions?
 
Send the output file to /dev/null?

Code:
     -o file, --output=file
                 Set the output file name to file.  By default, a ``pathname''
                 is extracted from the specified URI, and its basename is used
                 as the name of the output file.  A file argument of ‘-’
                 indicates that results are to be directed to the standard
                 output.  If the file argument is a directory, fetched file(s)
                 will be placed within the directory, with name(s) selected as
                 in the default behaviour.
fetch(1)
 
Would still download the file though. Maybe HEAD(1) from www/libwww might work? That only fetches the headers, not the actual file. Or just some clever usage of nc(1) could fetch the headers too.

Edit: printf "HEAD /somefile.txt HTTP/1.1\r\nHost: www.example.com\r\n\r\n" | nc www.example.com 80
 
Can't you use the -s (--print-size) option?
fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/meta
If the size is unknown, it will be displayed as Unknown.
 
With ftp/curl which natively support HEAD request, it's pretty trivial:
Bash:
curl -s -o /dev/null -I -w "%{http_code}" "http://example.com/file" | grep -q '^2' && echo true || echo false

A simplier one which outputs HTTP headers:
curl -I "http://example.com/file"

If you really want to rely only on fetch(1) (if 0 = file exists):
fetch -q -o /dev/null "http://example.com/file"
 
Yeah,
Can't you use the -s (--print-size) option?
fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/meta
If the size is unknown, it will be displayed as Unknown.

That doesn't do it because the URL might exist and be downloadable, but the http server might not give the length in advance.
 
That doesn't do it because the URL might exist and be downloadable, but the http server might not give the length in advance.
Not 'Unknown'?
Is it other than these?
Code:
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/
Unknown
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/All/
fetch: Forbidden
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/Alle
fetch: Not Found
 
Not 'Unknown'?
Is it other than these?
Code:
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/
Unknown
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/All/
fetch: Forbidden
$ fetch -s http://pkg.freebsd.org/FreeBSD:15:amd64/latest/Alle
fetch: Not Found

Oh that's right. I forgot (and I wrote the -s option :)).
 
Back
Top