When checking if a file exists on a remote server, curl is a standard and effective tool, but there are alternatives that may be more appropriate depending on your use case. Here's a breakdown of options and how they compare:
✅ 1. curl with -I (HEAD Request)
This is the most efficient and lightweight method for checking file existence. It sends a HEAD request (which only retrieves headers, not the file content), and you can check the HTTP status code (e.g., 200 for success, 404 for not found).
Example:
curl -s -I https://hakunamatata5.org/hls_race_12/1080/1080_.m3u8 | grep "HTTP/1.1 200"
Pros:
- Efficient: No data is downloaded, only headers.
- Standard: Widely supported and easy to integrate.
- Flexible: Can check for specific headers or status codes.
Cons:
- Requires the server to support
HEAD requests (which is common).
- If the server blocks
HEAD, this may not work.
✅ 2. wget with --spider
wget can also check if a file exists without downloading it using the --spider option.
Example:
wget --spider https://hakunamatata5.org/hls_race_12/1080/1080_.m3u8 2>&1 | grep "200 OK"
Pros:
- Simple syntax: Easy to read and use.
- No data transfer: Only checks the existence of the file.
- Logs detailed output: Useful for debugging.
Cons:
- Less efficient than
curl for simple checks.
- Output parsing can be trickier (e.g., requires
grep or awk).
✅ 3. httpie (Alternative HTTP Client)
If you're open to using a more modern HTTP client, httpie is a user-friendly tool for sending HTTP requests.
Example:
http HEAD https://hakunamatata5.org/hls_race_12/1080/1080_.m3u8 | grep "200 OK"
Pros: