- Supported protocols include DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, and TFTP
- Authentication and encryption
- Proxy implementation
- Resume interrupted transfers
While focusing only on a few widely used protocols, this tutorial describes how to implement all features previously listed. All steps described below include screenshots. Thus, making it easy for all users to understand and follow them.
How to Install cURL in Linux
To begin with, install cURL by running the following command for Debian-based Linux distributions (Including Ubuntu):
To install cURL on CentOS or Fedora Linux distributions, use the following command:
All About Downloading Files Using cURL
The first command shows how to download a file using cURL, keeping the original file name.
To do it, run cURL and add the -O parameter followed by the file path. The proper syntax is shown below, where <File address> must be replaced with the full URL or path of the file to download:
The syntax is shown in the example below, in which I downloaded the robots.txt file from the LinuxHint site:
You can download multiple files using cURL; just add a -O flag followed by each file you want to download, as shown in the following example in which the robots.txt file is fetched from linuxhint.com, and a logo is fetched from the site named argexchanger:
The command shown in the previous screenshot is long, while the following syntax is:
The previous flag (-O with upper case) saves the file keeping the original name. That’s the most widely used flag when using cURL.
By default, cURL downloads files in the working directory. You also can define a custom name or path for the file by implementing the -o flag (Lower case) followed by the name or path you want to define.
In the following example, I download the file robots.txt from Linux Hint, but I save it as CustomName, where CustomName is arbitrary and can include the file extension:
In the following screenshot, I used the -o flag to define a custom name and a custom path for the file stored in the test subdirectory of the home directory under the name CustomName:
Another useful cURL feature is the -C- flag to resume interrupted downloads. In the following screenshot, I showed how I resume a download previously interrupted by executing cURL followed by flags -C-, -O and the target,
cURL also supports transferring files through proxy servers.
To implement a proxy, you need to add the -x flag followed by the proxy address and port. The proper syntax is:
Where <ProxyIP> must be replaced with the proxy IP address or host, <ProxyPort> must be replaced with the proxy port, and <File URL/Path> with the file address.
In the following example, I downloaded the robots.txt file using the proxy with IP address 22.214.171.124 through port 80:
Uploading Files Using cURL in Linux
The previous section described how to download files in Linux using cURL. The current section explains how to upload files, both through HTTP and FTP.
Uploading files through FTP without credentials (Anonymous) using cURL is pretty simple. Just use the -T flag followed by the file you want to upload and the FTP address.
The syntax is:
Using credentials doesn’t make the task harder. You can define a username only, and you will be required to fill in the password during the connection process. This option is better than typing the password in the command to avoid a plain text password.
To upload a file to an FTP server requiring login, use the -u flag followed by the username. Some server configurations like the one I’m using requires including the server host or IP address after the username, as shown in the screenshot below, in which the -u flag is used to define the user and host ([email protected]) and the -T flag is used to define the file to upload (zippedfile.zip).
After executing the following syntax, you will be required to type the password.
Note: Replace [email protected] with your actual username and replace ftp://argexchanger.com with your actual FTP server.
You also can include the password in the command, avoiding being required to type the password after running the command. The syntax is the same as shown above. You do not need additional flags; just add a colon followed by the password as shown in the screenshot below, where YourpasswordHere is the password:
To finish this tutorial, let’s see how to upload files through the HTTP protocol. For this purpose, I will use the https://transfer.sh free service, which allows you to upload files using HTTP.
The flag used in this example is –upload-file followed by the file you want to upload. In this case, a file named linuxhintfile and the HTTP server are shown in the following image:
By learning all the cURL commands explained previously, you will be able to use this tool productively.
As you can see, using the cURL command is pretty easy. cURL is a command line but user-friendly tool anyone dealing with consoles must be able to use. This command is especially useful because it is cross-platform and can be found on Unix like macOS and Microsoft Windows operating systems. This is excellent for fetching and sharing files with only a command execution. You can use free FTP and HTTP file-sharing services to test all instructions explained in this article.
Thank you for reading our tutorial on cURL. I hope it was useful for you. Keep reading Linux Hint for more Linux professional tutorials.