Exploring curl - Linux Commands

What is the curl Command in Linux?

The curl command in Linux is a versatile tool used to transfer data to or from a server, using a variety of protocols such as HTTP, HTTPS, FTP, and more. Unlike wget, which is mainly focused on downloading files, curl is designed to work with data transfers in both directions and is often used for testing APIs, uploading data, or interacting with web services.

Key features include:

  • Supports multiple protocols (HTTP, HTTPS, FTP, SMTP, SCP, SFTP, and more)
  • Can send custom HTTP headers and data (useful for APIs)
  • Supports HTTP methods like GET, POST, PUT, DELETE
  • Handles authentication (Basic, Digest, OAuth, etc.)
  • Supports cookies and session management
  • Ability to upload files or data
  • Shows detailed request/response headers for debugging
  • Works well in scripts and automation pipelines

Basic Syntax

Terminal window
curl [OPTIONS] URL

Example

Terminal window
curl https://example.com

This will fetch the content of the webpage and output it to the terminal.

Options

OptionWhat It DoesExample
-o FILESave output to a filecurl -o backup.zip [URL]
-OSave with original filenamecurl -O [URL]
-LFollow redirectscurl -L [URL]
-d DATASend POST datacurl -d "a=1&b=2" -X POST [URL]
-H HEADERAdd custom headercurl -H "Content-Type: application/json"
-X METHODSet request methodcurl -X POST [URL]
-u USER:PASSUse basic authcurl -u user:pass [URL]
-sSilent mode (no output except result)curl -s [URL]
-IShow only headerscurl -I [URL]

Common Use Cases

  • Download and save a file with a custom name
Terminal window
curl -o backup.zip https://example.com/file.zip
  • Download and save with the original remote file name
Terminal window
curl -O https://example.com/file.zip
  • Follow redirects (e.g., from shortened URLs)
Terminal window
curl -L http://bit.ly/some-short-url
  • Send POST form data to an API
Terminal window
curl -d "username=username&password=password" -X POST https://api.example.com/login
  • Send JSON data with a custom header
Terminal window
curl -H "Content-Type: application/json" -d '{"name":"Victoria"}' -X POST https://api.example.com/users
  • Use basic authentication
Terminal window
curl -u user:pass https://api.example.com/secure-data
  • Download a file silently (no progress output)
Terminal window
curl -s -O https://example.com/largefile.iso
  • Fetch only HTTP headers (e.g., for debugging)
Terminal window
curl -I https://example.com

Additional Help

To see all available options:

Terminal window
curl --help
# or
man curl

Recap

The curl command is a powerful, flexible tool for transferring data over a variety of protocols. Whether you need to download files, interact with APIs, upload data, or troubleshoot HTTP requests, curl provides the control and options to handle these tasks efficiently.

By mastering curl, you’ll improve your ability to automate web interactions and debug network services from the Linux command line.

Happy curling! 🌐🚀

Thank you

Big thanks for reading! You’re awesome, and I hope this post helped. Until next time!