You need to grab a favicon from a site, but you don't want to open a browser or use a GUI tool. Maybe you're automating an asset audit, or you just prefer the terminal. curl is the right tool for the job — if you know how to handle the quirks. This guide walks you through the exact commands to curl download favicon files, from the simple default path to parsing HTML for custom icons.

We'll assume you have curl installed (it's standard on macOS and Linux, and available for Windows). No other dependencies needed.

Step 1: Try the default favicon.ico path

Most sites still serve a file at /favicon.ico. The quickest way to grab it is:

curl -O https://example.com/favicon.ico

The -O flag saves the file with its remote name. If the site returns a 404, you'll get an HTML error page saved as favicon.ico — not what you want. Use -f to fail silently on HTTP errors:

curl -f -O https://example.com/favicon.ico

Now curl exits with an error if the file isn't there, so you can script around it.

Step 2: Follow redirects

Many sites redirect /favicon.ico to a CDN or a different path. Without -L, curl won't follow those redirects and you'll end up with a 301 response body. Add -L to follow:

curl -L -f -O https://example.com/favicon.ico

This is the single most common fix for "curl download favicon" attempts that return an empty or tiny file.

Step 3: Parse the HTML for custom icon links

Modern sites often declare icons via <link rel="icon"> tags in the <head>. The WHATWG HTML spec defines rel=icon as the standard way to associate an icon with a page (source). To find those URLs, fetch the page and grep for rel="icon":

curl -sL https://example.com | grep -o '<link[^>]*rel="[^"]*icon[^"]*"[^>]*>'

This prints the raw link tags. You'll see something like <link rel="icon" href="/assets/favicon-32.png" sizes="32x32">. Extract the href value and download it with curl:

curl -L -f -O https://example.com/assets/favicon-32.png

If the href is relative (starts with /), prepend the domain. For a more robust approach, use a tool like Python or PHP to parse the HTML properly.

Step 4: Handle multiple icon formats

Sites may declare several icons: SVG, PNG, ICO, and Apple Touch. You can loop over them. Here's a bash snippet that downloads all rel="icon" and rel="apple-touch-icon" links:

curl -sL https://example.com | grep -oE 'href="[^"]+\.(ico|png|svg)"' | cut -d'"' -f2 | while read -r path; do
  url="https://example.com${path}"
  curl -L -f -O "$url"
done

Adjust the domain and path handling as needed. This is handy for auditing a site's icon stack.

Common pitfalls

For a more automated approach, consider using a dedicated favicon downloader. Our guide to downloading favicons covers browser tricks and API methods that complement curl.

Conclusion

Using curl to download a favicon is straightforward once you account for redirects, error handling, and HTML parsing. Start with the default /favicon.ico path, add -L -f for reliability, and parse the HTML when you need specific formats. With these steps, you can script favicon downloads for any site.