dataviews
dataviews

Reputation: 3090

return filename after downloading file with curl

I would like to capture the filename to a variable of a file that is downloaded using curl. I am using the following flag to preserve the filename as in the below using --remote-name

My code:

file1=$(curl -O --remote-name 'https://url.com/download_file.tgz')
echo $file1

Upvotes: 2

Views: 1583

Answers (2)

Davide Madrisan
Davide Madrisan

Reputation: 2300

You can use the -w|--write-out switch of curl:

file1="$(curl -O --remote-name -s \
    -w "%{filename_effective}" "https://url.com/download_file.tgz")"
echo "$file1"

Upvotes: 4

Jeff Holt
Jeff Holt

Reputation: 3190

file1=download_file.tgz
url=https://url.com/$file1 #encoding this might be necessary
curl -O --remote-name $url
echo $file1

If, to construct the URL, you need to know the filename that you want to download, then you don't need anything from curl to identify the file that it downloaded unless there is not a 1:1 relationship between the basename of the URL and file that was downloaded.

Upvotes: 1

Related Questions