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
The most straightforward way to use
curl is to type
curl followed by the URL of the page you want to access.
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
curl outputs the source of the page that you requested. If you want to save this output to a file, you can use the
curl -o output.html http://example.com
-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
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
-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.
5. Send Headers
You can add headers to your request with the
curl -H "Accept: application/json" http://example.com/api
You can tell
curl to send cookies with the
-b option, and to store cookies with the
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
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
If you need to authenticate, you can use the
-u option followed by the username and password separated by a
curl -u username:password http://example.com
More Linux commands:
|File System Operations||
|Search and Text Processing||
|System Information and Management||
|User and Session Management||