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.
Example #1: Saving a file with the remote file name
You can use the
--remote-name 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
./README file will be created or overwritten.
Example #2: Saving a file with another file name
You can use the
--output 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.
Example #3: Saving a file using the output redirection operator
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
Example #4: Downloading multiple files with a single command
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
Example #5: Dealing with the HTTP redirection
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
--location option to tell cURL to follow the redirects.
$ curl -L -O https://example.com/files/README
You can use the
--max-redirs <number> 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.
Example #6: Speed limiting
If you want to limit the downloading speed, you can use the
--limit-rate <speed> 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
Example #7: Downloading a part of a file
cURL allows you to download a part of a file by using the
-r <range> or
--range <range> option.
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
Example #8: Resuming downloading a file
What would you do if the download process was interrupted, for example, you pressed
Ctrl + C? Re-download?
Do not! Because cURL supports resuming a file downloading process at the given offset by the
-C <offset> option. If you do not know the exact value of the offset, do not worry, you just need to specify
-C - to tell cURL to automatically find out that value.
$ curl -C - -O https://example.com/files/README
Example #9: Using basic authentication
If the requested file requires basic authentication, you need to use the
-u 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.
Example #10: Using a proxy
If you want to use a proxy to download a file, you need to use
-x option followed by the proxy address, say
$ curl -x http://126.96.36.199:8080 -O https://example.com/files/README
To see more examples, you can read the article How to use a proxy with cURL.
Example #11: Entering the silent mode
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