Bootstrap FreeKB - Curl - Download a remote file
Curl - Download a remote file

Updated:   |  Curl articles

The curl command no options or flags can be used to display the contents of a file on a remote system. In this example, the contents of the index.html file on the www.example.com web server is displayed.

~]# curl http://www.example.com/index.html
<html>
  <body>
    Hello World
  </body>
</html>

 

In this example, the contents of the foo.json file on the www.example.com web server is displayed.

~]# curl http://www.example.com/foo.json
[
 {
  "foo":"bar"
 }
]

 

By default, curl uses the GET method, thus there is no need to use the -X or --request option when using the GET method. The following commands will both return the content of the index.html page. The -X or --request option must be used when using any other method (POST, PUT, DELETE). 

curl http://www.example.com/index.html
curl -X GET http://www.example.com/index.html

 

  • The -o or --output option can be used to redirect the content of the remote file to a file on your local system allowing you to specify the name and location of the file that will be created on your local system
  • The -O or --remote-name option can be used to redirect the content of the remote file to the present working directory on your local system using the same name as the remote file

 

In this example, the contents of the index.html file on the www.example.com web server is redirected to a file named foo.html on your local system.

curl --output /path/to/foo.html http://www.example.com/index.html

 

In this example, the contents of the index.html file on the www.example.com web server will be downloaded to a file named index.html in your present working directory on your local system.

curl --remote-name http://www.example.com/index.html

 

Or, redirection can be used as well.

curl http://www.example.com/index.html > index.html

 

If the remote system requires basic authentication, the -u or --user option can be used to include the username and password in the request.

curl --user "username:password" --output "/tmp/index.html" "http://www.example.com/index.html"

 

It is often a good idea to also used the -w or --write-out option to determine if you are able to GET the file from the remote system.

curl --write-out "%{http_code}" --output "/tmp/index.html" "http://www.example.com/index.html"

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 27122c in the box below so that we can be sure you are a human.