How To Download Files with curl Command on Linux

This post will guide you how to download remote files using curl command from the command line on your Linux system. How do I download multiple files with curl command on Linux or unix systems.

How To Download Files with curl Command on Linux 1

Curl Command


curl is a tool to transfer data from or to a server, using one of the supported protocols (DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP). The command is designed to work without user interaction.

curl offers a busload of useful tricks like proxy support, user authentication, FTP upload, HTTP post, SSL connections, cookies, file transfer resume, Metalink, and more. As you will see below, the number of features will make your head spin!

The syntax of the Curl command is as follows:

curl [options] [URL...]

Options:

-o, --output <file> Write output to <file> instead of stdout. If you are using {} or [] to fetch multiple documents, you can use ‘#’ followed by a number in the <file> specifier. That variable will be replaced with the current string for the URL being fetched. Like in: curl http://{one,two}.example.com -o “file_#1.txt”

-O, --remote-name Write output to a local file named like the remote file we get. The file will be saved in the current working directory. If you want the file saved in a different directory, make sure you change the current working directory before invoking curl with this option.

-u, --user <user:password> Specify the user name and password to use for server authentication. Overrides -n, –netrc and –netrc-optional.

-x, --proxy [protocol://]host[:port] Use the specified proxy. The proxy string can be specified with a protocol:// prefix. No protocol specified or http:// will be treated as HTTP proxy. Use socks4://, socks4a://, socks5:// or socks5h:// to request a specific SOCKS version to be used. (The protocol support was added in curl 7.21.7)

Downloading Single Files


If you want to download a sigle file from remote server on your Linux system, and you can use the curl command with -O option to download a file and save it in the current working directory with the remote file name, type:

$ curl -O https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz

Outptus:

[email protected]:~$ curl -O https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 162 100 162 0 0 99 0 0:00:01 0:00:01 --:--:-- 99

[email protected]:~$ ls -al linux-5.4-rc2.tar.gz
-rw-rw-r-- 1 devops devops 162 Oct 9 22:25 linux-5.4-rc2.tar.gz

Downloading Single Files with a Different File Name


If you want to specifiy a new file name when download a single file using curl command from remote server, and you can pass the -o option to the curl command.

For example, you want to save the downloaded file to a specific directory “/tmp” with a given name “mycurltest.tar.gz“, type:

$ curl -o /tmp/mycurltest.tar.gz https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz

outputs:

[email protected]:~$ curl -o /tmp/mycurltest.tar.gz https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 162 100 162 0 0 92 0 0:00:01 0:00:01 --:--:-- 92
[email protected]:~$ ls -al /tmp/mycurltest.tar.gz
-rw-rw-r-- 1 devops devops 162 Oct 9 22:29 /tmp/mycurltest.tar.gz

Downloading Multiple Files


You can also download multiple files from multiple remote servers using curl command.

For example, you want to download two tar.gz file from kernel.org site and save those two file in the current direcotry with same file names. type:

$ curl -O https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.148.tar.xz -O https://cdn.kernel.org/pub/linux/kernel/v3.x/linux-3.16.75.tar.xz

Outputs:

[email protected]:~$ curl -O https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.14.148.tar.xz -O https://cdn.kernel.org/pub/linux/kernel/v3.x /linux-3.16.75.tar.xz
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 96.4M 100 96.4M 0 0 2940k 0 0:00:33 0:00:33 --:--:-- 3284k
100 77.3M 100 77.3M 0 0 4769k 0 0:00:16 0:00:16 --:--:-- 5355k
[email protected]:~$ ls linux-*
linux-3.16.75.tar.xz linux-4.14.148.tar.xz

Downloading Files with Authentication


If you want to download a password protected file from remote server using curl command, and you need to pass the -u option with username and password to the curl command.

Downloading a file from ftp server with authentication,type:

$ curl -u username:password -O ftp://myserver.test/mytest.tar

Downloading a file from Http server with authentication, tyep:

$ curl -u username:password -O https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz

Downloading Fiels Using a Proxy Server


If you need to use a proxy server to download remote files from the remote server using curl command, and you need to pass the -x option to the curl command, type:

$ curl -x 192.168.3.50:3128 -O https://git.kernel.org/torvalds/t/linux-5.4-rc2.tar.gz

Conclusion


You should know that how to download a single or multiple files using curl command from the command line in your CentOS or RHEL or Ubuntu Linux.

More Curl Examples: How to Install and Use cURL Command on Ubuntu or CentOS Linux

 

You might also like:

Sidebar



back to top