682

I have a text document that contains a bunch of URLs in this format:

URL = "sitehere.com"

What I'm looking to do is to run curl -K myfile.txt, and get the output of the response cURL returns, into a file.

How can I do this?

Syscall
  • 19,327
  • 10
  • 37
  • 52
Tony
  • 8,681
  • 7
  • 36
  • 55

11 Answers11

943
curl -K myconfig.txt -o output.txt 

Writes the first output received in the file you specify (overwrites if an old one exists).

curl -K myconfig.txt >> output.txt

Appends all output you receive to the specified file.

Note: The -K is optional.

Alex2php
  • 10,337
  • 1
  • 16
  • 22
  • 1
    Sorry maybe I need to clarify - the doc with all my URL's in the format about is called myfile.txt so I do curl -K myfile.txt and it runs though each one but I don't get the output into any file. – Tony Dec 06 '12 at 00:48
  • 42
    I use the redirect for my command lines: `curl url > destfile.x` – kodybrown Apr 20 '16 at 13:42
  • 6
    When I do either of these the output still displays in the terminal, not in the file – kris Nov 26 '17 at 06:01
  • 19
    @kris you probably have an ampersand in the url. put the url in double quotes and then try – jglouie Apr 13 '18 at 20:11
  • It works without the -K. With it, I get "No URL specified." – Arya Pourtabatabaie Nov 01 '18 at 18:47
  • thank you. i was thinking about using wget and then find the solution to ignore the ssl like curl. turned out that we need to only using -o like wget instead of -k like curl :) – Jacky Supit Jul 07 '21 at 08:21
  • This only captures the last line, not the whole `curl` output. – Chris F Jul 15 '23 at 19:05
275

For a single file you can use -O instead of -o filename to use the last segment of the URL path as the filename. Example:

curl http://example.com/folder/big-file.iso -O

will save the results to a new file named big-file.iso in the current folder. In this way it works similar to wget but allows you to specify other curl options that are not available when using wget.

Greg Bray
  • 14,929
  • 12
  • 80
  • 104
  • 7
    for multiple files use `--remote-name-all` https://unix.stackexchange.com/a/265819/171025 – qwr Aug 11 '19 at 18:44
  • 1
    To follow redirect, add `-L` option. – lk_vc Jan 30 '22 at 13:28
  • 1
    Another addition for qwr's comment: the argument need to be put before the URLs like `curl --remote-name-all https://example.tld/resource{1,2,3}`. See: https://curl.se/docs/manpage.html#--remote-name-all – Little_Ye233 Jun 13 '22 at 03:37
  • Exactly what I was looking for, this nice and useful`-O` option. Thanks! – BiBi Dec 05 '22 at 14:33
  • 1
    `-O` - that's a "letter Oooh" as in "Output" (not a zero) – Abdull Jul 21 '23 at 14:13
75

There are several options to make curl output to a file

 # saves it to myfile.txt
curl http://www.example.com/data.txt -o myfile.txt

# The #1 will get substituted with the url, so the filename contains the url
curl http://www.example.com/data.txt -o "file_#1.txt" 

# saves to data.txt, the filename extracted from the URL
curl http://www.example.com/data.txt -O 

# saves to filename determined by the Content-Disposition header sent by the server.
curl http://www.example.com/data.txt -O -J 
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151
19

Either curl or wget can be used in this case. All 3 of these commands do the same thing, downloading the file at http://path/to/file.txt and saving it locally into "my_file.txt".

Note that in all commands below, I also recommend using the -L or --location option with curl in order to follow HTML 302 redirects to the new location of the file, if it has moved. wget requires no additional options to do this, as it does this automatically.

# save the file locally as my_file.txt

wget http://path/to/file.txt -O my_file.txt  # my favorite--it has a progress bar
curl -L http://path/to/file.txt -o my_file.txt
curl -L http://path/to/file.txt > my_file.txt

Alternatively, to save the file as the same name locally as it is remotely, use either wget by itself, or curl with -O or --remote-name:

# save the file locally as file.txt

wget http://path/to/file.txt
curl -LO http://path/to/file.txt
curl -L --remote-name http://path/to/file.txt

Notice that the -O in all of the commands above is the capital letter "O".

The nice thing about the wget command is it shows a nice progress bar.

You can prove the files downloaded by each of the sets of 3 techniques above are exactly identical by comparing their sha512 hashes. Running sha512sum my_file.txt after running each of the commands above, and comparing the results, reveals all 3 files to have the exact same sha hashes (sha sums), meaning the files are exactly identical, byte-for-byte.

References

  1. I learned about the -L option with curl here: Is there a way to follow redirects with command line cURL?

See also: wget command to download a file and save as a different filename

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
7

For those of you want to copy the cURL output in the clipboard instead of outputting to a file, you can use pbcopy by using the pipe | after the cURL command.

Example: curl https://www.google.com/robots.txt | pbcopy. This will copy all the content from the given URL to your clipboard.

Linux version: curl https://www.google.com/robots.txt | xclip

Windows version: curl https://www.google.com/robots.txt | clip

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Md Mazedul Islam Khan
  • 5,318
  • 4
  • 40
  • 66
  • pbcopy is only available on MacOS. However `xclip` can be used in it's place for [Linux see this question](https://stackoverflow.com/questions/5130968/how-can-i-copy-the-output-of-a-command-directly-into-my-clipboard). However I would in most cases prefer `curl http://example.com -o example_com.html & cat example_com.html | pbcopy` So you wouldn't need to curl again if you accidently clear your clipboard. – lacostenycoder Dec 04 '19 at 10:38
  • Also this should be used with caution if you're unsure of the size of the payload. For example you probably wouldn't want to paste this into a text editor, but opening it in vim no problem. `curl http://www.textfiles.com/etext/FICTION/fielding-history-243.txt | pbcopy` maybe don't try this! – lacostenycoder Dec 04 '19 at 10:51
6

You need to add quotation marks between "URL" -o "file_output" otherwise, curl doesn't recognize the URL or the text file name.

Format

curl "url" -o filename

Example

curl "https://en.wikipedia.org/wiki/Quotation_mark" -o output_file.txt

Example_2

curl "https://en.wikipedia.org/wiki/Quotation_mark" > output_file.txt  

Just make sure to add quotation marks.

AlexPixel
  • 389
  • 7
  • 17
  • 1
    Thank you. I was looking around for a while before I found your comment. Now I don't have to watch 59Mb of text scroll up the screen to nowhere any more :-) – Spuggiehawk Sep 27 '22 at 11:55
4

Use --trace-ascii output.txt to output the curl details to the file output.txt.

Paolo
  • 20,112
  • 21
  • 72
  • 113
yumingtao
  • 81
  • 4
  • Thanks, the man page mentions that this also outputs the "descriptive information" that `-vv` displays (SSL info, HTTP verb, headers, ...), which I wanted to store. None of the other answers write that to a file. – Thor Galle May 13 '22 at 18:12
3

If you want to store your output into your desktop, follow the below command using post command in git bash.It worked for me.

curl https://localhost:8080
    --request POST 
    --header "Content-Type: application/json" 
    -o "C:\Desktop\test.json"
user3313834
  • 7,327
  • 12
  • 56
  • 99
1

A tad bit late, but I think the OP was looking for something like:

curl -K myfile.txt --trace-ascii output.txt
Paolo
  • 20,112
  • 21
  • 72
  • 113
lca25er
  • 39
  • 2
0

My favorite is lwp-download, which can be found here: https://metacpan.org/dist/libwww-perl/view/bin/lwp-download

You can use it like this:

lwp-download http://www.perl.com/CPAN/src/latest.tar.gz

This will store the file as "latest.tar.gz" in your current directory, so no further option is needed.

Marco
  • 3,470
  • 4
  • 23
  • 35
-2

Writes the first output received in the file you specify (overwrites if an old one exists).

curl -K myconfig.txt >> output.txt
Cudox
  • 11
  • 3
  • The `>>` appends, it does not overwrite. If you would like to overwrite, use a single `>` – wybe Jul 05 '22 at 15:46