arbitron Command in Linux



curl Command in Linux

curl is a command-line tool that is used for transferring data between a server and your computer. It supports several protocols, such as FTP, HTTP, HTTPS, and more. With curl command line utility, you can download files, send data or interact with the web APIs directly from the terminal. The curl command is versatile and has the ability to handle complex tasks like cookies, authentication and proxies.

If you are a developer or a system administrator, curl is an essential tool that simplifies the process of managing web requests and data transfers efficiently.

Table of Contents

Here is a comprehensive guide to the options available with the curl command −

curl Command Installation

By default, the curl command-line utility may not be installed on your Linux distributions. However, you can use your default system package manager to install the curl utility within a few seconds.

Ubuntu/Debian

If you are using Linux distributions like Ubuntu or Debian, you can use the below-given apt command to install curl utility on your system −

sudo apt install curl

RHEL/CentOS/Fedora

For Linux systems like RHEL, CentOS and Fedora, you can use the yum package manager to install curl utility −

sudo yum install curl

OpenSUSE

The OpenSUSE operating system users can install the curl command utility using the following command −

sudo zypper install curl

Arch Linux

On Arch Linux system, curl utility can be installed through the below-given command −

sudo pacman -Sy curl

To verify the curl installation, you can type the following command the terminal −

curl --version

Syntax of curl Command

The syntax for the curl command on Linux is as follows −

curl [options] [URL]

Here, the [options] are various flags you can use with curl command. [URL] is the URL you want to retrieve or interact with; it can be an HTTP/HTTPS URL, an FTP URL, or any other supported protocol.

curl Command Options

The following are some options described in the table that you can use with the curl command −

Option Description
-A, --user-agent <name> Specify the User-Agent string to send to the server.
-d, --data <data> Send data using HTTP post.
-f, --fail Exit immediately without output if an HTTP error occurs.
-h, --help <category> Display help information for commands.
-i, --include Include the response headers in the output.
-o, --output <file> Save the output to a file instead of displaying it.
-O, --remote-name Save the output to a file named after the remote file.
-s, --silent Adds custom headers to the request.
-T, --upload-file <file> Upload a local file to the destination.
-u, --user user: password Provide a username and password for server authentication.
-v, --verbose Increase the amount of detail in the operation’s output.
-V, --version Display the version number and exist.

Examples of curl Command in Linux

Let’s explore a few examples of curl command on Linux system −

  • HTTP GET Request
  • Download File from URL
  • HTTP POST Request with Data
  • Limit Download Speed
  • Download Multiple Files Concurrently
  • FTP File Transfer

HTTP GET Request

One of the basic functions of the curl command on Linux is to allow users to retrieve data from a URL using an HTTP GET request. It is like opening a web page in your terminal. This is pretty useful for accessing APIs, fetching web pages, and downloading data from a specific URL directly to your system.

For example,

curl https://www.example.com

In the above example, the curl command is used to send an HTTP GET request to https://www.example.com and the server responds by providing the content (usually HTML) associated with that URL.

Note − You can replace https://www.example.com with any valid URL you want to retrieve data on your system.

HTTP GET Request

Download File from URL

You can also use the curl command to download a file from a specific URL directly to your local system. It is ideal in case you want to grab files without opening a web browser or using a graphical download manager.

For example, the following command will save with file with the original filename on your Linux system −

curl -O https://www.example.com/file.txt

The above command will download the file from https://www.example.com/file.txt and save it with the same name (file.txt) on your system.

Download File from URL 1

You can also specify the name of your output file according to your choice, it can be done using the below-given command −

curl -o localfile.zip https://www.example.com/file.zip

In this variation, the -o option will allow you to specify a custom local filename (localfile.zip) for the downloaded content.

Note − The -O option (uppercase) saves the file with its original name, while the -o option (lowercase) lets you choose a different local filename. You can adjust the URL and filename as needed for your specific use case.

Download File from URL 2

HTTP POST Request with Data

With curl command, it is also possible to send an HTTP POST request with specific data. It is like filling out a form and submitting it to a website. This process is useful in case you want to interact with APIs that require data submission, like creating new records or updating existing ones.

For example,

curl -X POST -d 'param1=value1&param2=value2' https://example.com/api/endpoint

You can replace “param1=value1&param2=value2” with your actual data and “https://example.com/api/endpoint” with the target URL.

In the above example, the -X POST flag is used to specify that you are making an HTTP POST request. While the -d option will indicate the data to be sent in the request body. You can customize the data and URL presented in the above command according to your specific use case.

Limit Download Speed

You can also use the curl command on Linux to limit your download speed and prevent excessive bandwidth usage during large downloads. For that purpose, --limit option is used with the curl command.

For example,

curl --limit-rate 1M -O https://www.example.com/file.iso

You can replace “https://www.example.com/largefile.iso” with the actual URL of the file you want to download. The 1M in the above example specifies a download rate of 1 megabyte per second. This rate can be adjusted according to your specific requirement.

When you execute the above command, the curl will download the file while limiting the download speed to 1 megabyte per second.

Limit Download Speed

Download Multiple Files Concurrently

You can also use the curl command to concurrently download multiple files on your Linux system. This will save your time by downloading multiple files simultaneously.

For example,

curl -O https://www.example.com/file1.zip -O https://www.example.com/file2.tar.gz

You can replace the URLs with the actual files you want to download.

Download Multiple Files Concurrently

FTP File Transfer

The curl command can also be used to perform file transfer through FTP protocol. With curl, you can upload a file to an FTP server by using the following example command −

curl -T local_file.txt ftp://ftp.example.com/remote/path/

In the above command, -T flag specifies the local file to be uploaded. You can replace local_file.txt, ftp.example.com, and /remote/path/ with your actual file, server address, and remote path.

The curl command offers many options to fine-tune your FTP transfers, such as −

  • Authentication -u username:password
  • Passive mode -p
  • Transfer mode -B for ASCII, default is binary
  • Verbose output -v
  • SSL/TLS (FTPS) --ssl-reqd

The following is the basic syntax of curl command for FTP file transfer with authentication −

curl -u user:password ftp://ftp.example.com/file.txt -o local_file.txt

You must replace user, password, and ftp.example.com/file.txt with your credentials and the specific file you want to download.

That’s how you can use the curl command on your Linux system.

Conclusion

The curl command line utility is a powerful and versatile tool that can be used for multiple purposes on Linux systems. You can download files from the Internet, limit your download speed, perform HTTP GET requests, FTP file transfer and more. In this guide, we have discussed the installation of curl command on Linux, along with its syntax and different options or flags. Further, a few examples are also provided to help users in getting enough guidance on how to use the curl command effectively. Make sure to customize the examples according to your needs and explore more options to enhance your experience.

Advertisements