How to Use curl in Linux

Master the curl command in Linux for data transfer and HTTP requests with this guide.

The curl command in Linux is a powerful utility that’s used to transfer data from or to a network server, leveraging a wide array of protocols like HTTP, HTTPS, FTP, and SFTP.

In simpler terms, it’s a tool that helps you transfer data over the internet directly from your command line. For instance, you can use curl to download a file from a URL, or upload a file to a server.

This command-line tool is not just about transferring files. You can also use curl to make HTTP requests, such as GET and POST, from your terminal. This is extremely useful when testing or interacting with web services and APIs. With curl, you can even inspect the content of a web page without actually opening it in a browser, as it can fetch and display the raw data.

Here are some different ways to use the curl command:

Basic Usage

The most straightforward way to use curl is to type curl followed by the URL of the page you want to access.

curl http://example.com

When you run the command curl http://example.com in the terminal, it sends a GET request to the server located at http://example.com. The output that you see on the terminal is the server’s response to this GET request, typically the HTML content of the webpage.

1. Save Output to a File

By default, curl outputs the source of the page that you requested. If you want to save this output to a file, you can use the -o or -O option.

curl -o output.html http://example.com

The -o option allows you to specify a filename, while -O will use the filename from the URL.

2. Follow Redirects

If the URL you’ve requested redirects to another page, curl won’t follow the redirect unless you include the -L option.

curl -L http://example.com
3. Send POST Requests

You can use curl to send POST requests. This is often used when submitting forms.

curl -d "param1=value1¶m2=value2" -X POST http://example.com/form

The -d option tells curl that you’re going to send POST data, and the -X option specifies the request method to use.

4. Send GET Requests with Parameters

You can send a GET request with parameters by appending them to the URL.

curl http://example.com/search?query=example
5. Send Headers

You can add headers to your request with the -H option.

curl -H "Accept: application/json" http://example.com/api
6. Use Cookies

You can tell curl to send cookies with the -b option, and to store cookies with the -c option.

curl -b "name=value" -c cookies.txt http://example.com
7. Upload Files

You can use curl to upload files to a server with the -F option. This sends a POST request and sets the Content-Type to multipart/form-data.

curl -F "file=@path/to/local/file" http://example.com/upload
8. Download Files

You can use curl to download files using FTP, SFTP, HTTP, and many other protocols.

curl -O ftp://example.com/pub/file.zip
9. Authentication

If you need to authenticate, you can use the -u option followed by the username and password separated by a : (colon).

curl -u username:password http://example.com
More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail