If you have ever used cURL to retrieve the output of a file, believe me, saving that output to a file only takes a few more characters.
You can use the
option to save the output to a local file in the current working directory using the remote file name.
$ curl -O https://example.com/files/README
In this example, the
file will be created or overwritten.
You can use the
option followed by a file name you want to save the output.
$ curl -o my-readme-file https://example.com/files/README $ curl -o ~/save/to/other/path https://example.com/files/README
Similar to the above example, the output file will be created or overwritten.
In Linux, you can use the output redirection operator
to write the output to a specific file.
$ curl https://example.com/files/README > ~/save/to/specific/path
If you want to append the output to that file, you can use the output append operator
If you want to download multiple files, you can add more
$ curl \ -O https://example.com/files/file-1 \ -o file-2 https://example.com/files/file-2 \ -o file-3 https://example.com/files/file-3
If the target server responded with the HTTP redirection code (3xx) for the requested file, the local file you downloaded would be empty. In this case, you have to add the
option to tell cURL to follow the redirects.
$ curl -L -O https://example.com/files/README
You can use the
option to specify the maximum number of redirects will be followed to avoid infinite redirection-followings:
$ curl -L --max-redirs 10 -O https://example.com/files/README
In this example, after 10 attempts, cURL will throw an error and abort the download process.
If you want to limit the downloading speed, you can use the
option to set the maximum number of bytes downloaded per second. The number of bytes can be abbreviated by appending a suffix: kilobytes (k or K), megabytes (m or M), and gigabytes (g or G).
To limit 512 bytes/second and 2 megabytes/second:
$ curl --limit-rate 512 -O https://example.com/files/README $ curl --limit-rate 2M -O https://example.com/files/README
cURL allows you to download a part of a file by using the
For example, to get the first 500 bytes of a file:
$ curl -r 0-500 -O https://example.com/files/README
To get the last 300 bytes of a file:
$ curl -r -300 -O https://example.com/files/README
What would you do if the download process was interrupted, for example, you pressed
Ctrl + C
Do not! Because cURL supports resuming a file downloading process at the given offset by the
option. If you do not know the exact value of the offset, do not worry, you just need to specify
to tell cURL to automatically find out that value.
$ curl -C - -O https://example.com/files/README
If the requested file requires basic authentication, you need to use the
option followed by the credential which is in the form of
$ curl -u user:password -O https://example.com/files/README
To read more about basic authentication, you can read the article 3 methods to use basic authentication with cURL.
If you want to use a proxy to download a file, you need to use
option followed by the proxy address, say
$ curl -x http://22.214.171.124:8080 -O https://example.com/files/README
To see more examples, you can read the article How to use a proxy with cURL.
If you do not want cURL to show the progress meter or error messages, you can use the
$ curl -s -O https://example.com/files/README
Previously published at https://trubavuong.com/articles/curl-download-file/