Skip to main content

How to Save cURL Output to a File?

Learn to save the curl command output to a file for later usage in this quick tutorial.

Sagar Sharma

Warp Terminal

While the curl command was created to make things work without any human interaction, you can still use it to save output to a file.

And the easiest way to save a curl output to a file is to redirect the data stream to the file using > as shown:

curl > file.txt

But there are multiple ways to save the output and how you want the file to be treated. Want to know how? Here you have it.

Save a cURL output to a file in Linux

Before I walk you through the how-to guide, it is important to know that, unlike wget, curl does not come pre-installed in most of the distros.

So if you're using Ubuntu/Debian base, then use the following command:

sudo apt install curl

For Fedora/RHEL base:

sudo dnf install curl

For Arch Linux:

sudo pacman -S curl

Once done, you can use the curl command with the -o flag to save the output to the file as shown:

curl -o file.txt

For example, here, I downloaded an image from Pixabay having filename of Image.jpg:

curl -o Image.jpg
Save file using the curl command

Save the file with the original filename

In the previous example, you had to give the filename by yourself that may or may not correspond to the original filename.

And there are times when you have to have the original filename.

In that case, you can skip adding a filename and use the -O as shown:

curl -O

For example, here, I saved an image file without appending its filename:

curl -O

Save multiple files using the cURL command

If you want to save multiple files at once, then, you can use the cURL in the following manner:

curl -o File1 -o File2 -o File3

In simple terms, you place your links first, then use the -o flag and add the name of each file accordingly.

For example, here, I saved 3 images:

curl -o Image1.jpg -o Image2.jpg -o Image3.jpg 
Save multiple files using the cURL command in Linux

Save the file to a specific directory using cURL

By default, the curl command will save the file in the current working directory but that's not what we always want.

And with a little tweak to the previous command, you can save it to the desired directory. For that, you have to do is add a relative or absolute path before naming the file:

curl /path/to/filename.txt

For example, here, I saved an image file in a different directory (inside the home directory):

curl -o ~/NewTest/Image.jpg
Save files to different directory using the curl command

Here's how to use cURL to its max potential

The curl command has tonnes of features making it a go-to choice for many advanced users. And if you learn how to, you'll fall into the same category.

For that purpose, we made a detailed guide on how to use the curl command in Linux:

Curl Command in Linux [21 Practical Examples]
Learn some essential and some pro tips and tricks about using the Curl command in Linux.

I hope you will find this guide helpful.

Sagar Sharma