1

Say I have a bunch of URLs like this:

https://domain.com/foo/bar/2d9ec0354908e055acf372

From the URL I don't know what the extension is, and I don't have any other "frontend" information to tell me what kind of file it is.

But I do know what the title of the file is on the frontend. So I would like to just hardcode a title in when saving the file, and then append on whatever extension the real file has. So if the title of the file is "File A of N", I would like to call it "file-a-of-n" (I would just write that out). Then I would like the final result to be:

file-a-of-n.zip

Or whatever the extension ended up being. Maybe instead of .zip this particular file was .tar.gz, so it would end up being:

file-a-of-n.tar.gz

Whatever the extension ended up being, that's fine, want to append that to the file. But I don't want this:

2d9ec0354908e055acf372.zip

So wondering how to do this. Something with curl along the lines of:

curl -o file-a-of-n.(extension) https://domain.com/foo/bar/2d9ec0354908e055acf372

And the end result would be a file like:

file-a-of-n.zip
  • 1
    why not combine curl -o dwnld-file.X ..., then file dwnld-file.X to get downloaded file properties ? – Archemar Nov 7 '18 at 7:23
  • It seems you already know how to do what you want, so what question are you actually asking for help with? – roaima Nov 7 '18 at 7:28
  • I know what I want to do but not how to do it. I don't know how to append the extension on the file, or how to even get it. – Lance Pollard Nov 7 '18 at 7:36
1

curl doesn't support what you're trying to do directly, you can either pass it a new filename where to store the results, or let it determine the filename (and possibly extension) from the HTTP headers.

So you should do this in two steps. I recommend letting curl determine the name of the file, therefore downloading 2d9ec0354908e055acf372.zip, which you can then rename to file-a-of-n.zip, preserving the ".zip" part of it.

To make your job easier, create a temporary directory in which to work, and then create subdirectories for the a-of-n's and download the file into that subdirectory, that way you'll have a single file to handle in each subdirectory (easy to tell which one you need to process), which you can then move and rename, while preserving the extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.