0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use curl Command in Linux: Syntax and Examples

The curl command in Linux is used to transfer data from or to a server using various protocols like HTTP or FTP. You use it by typing curl [options] [URL] in the terminal to fetch or send data easily.
📐

Syntax

The basic syntax of the curl command is:

  • curl: The command itself.
  • [options]: Optional flags to modify behavior, like -X to specify HTTP method.
  • [URL]: The web address or server location you want to interact with.
bash
curl [options] [URL]
💻

Example

This example shows how to use curl to download the homepage of example.com and display it in the terminal.

bash
curl https://example.com
Output
<!doctype html>\n<html>\n<head>\n <title>Example Domain</title>\n ...</html>
⚠️

Common Pitfalls

Common mistakes include forgetting to specify the URL, using incorrect options, or expecting curl to save output to a file without the -o option.

For example, running curl https://example.com prints output to the terminal, but to save it you must use curl -o filename.html https://example.com.

bash
curl https://example.com

# Wrong: output goes to terminal

curl -o example.html https://example.com

# Right: output saved to example.html
📊

Quick Reference

OptionDescription
-o [file]Save output to a file instead of terminal
-IFetch only HTTP headers
-X [method]Specify HTTP method like GET, POST, PUT
-d [data]Send data in POST request
-LFollow redirects automatically

Key Takeaways

Use curl [options] [URL] to transfer data from or to a server.
Add -o filename to save output to a file instead of printing it.
Use -X to specify HTTP methods like POST or PUT.
Follow redirects with -L to get the final response.
Check headers only with -I to see server response info.