How do I download a file to a newly created directory with curl on OS X?
Asked Answered
S

1

34

I am trying to download my Heroku backups to a folder.

Downloading to the current folder like this works:

curl -o latest.dump `heroku pg:backups public-url`

But when I tried adding a folders path to latest.dump it looks like this:

$ curl -o /db-bkups/latest.dump `heroku pg:backups public-url`
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0 44318    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0Warning: Failed to create the file 
Warning: /db-bkups/latest.dump: No such file 
Warning: or directory
 36 44318   36 16384    0     0   9626      0  0:00:04  0:00:01  0:00:03  9626
curl: (23) Failed writing body (0 != 16384)

Ideally, I would like it be saved and downloaded like this:

/db-bkups/nov-1-2016/timestamp-db.dump

Where the folder nov-1-2016 is created dynamically when the cron is run, and the filename is the timestamp when the bkup was run.

Slinky answered 1/11, 2016 at 19:37 Comment(1)
I'm guessing that /db-bkups/ does not exsists? Like any other tool, is given path does not exists, you'll get an error. Try with --create-dirs argument like said downthere, or just specify path that exists. You can create db-bkups dir and it should work.Staff
J
82

You could try using the --create-dirs argument which was added in curl 7.10.3:

Here is an example that will create the directory hierarchy, (if it doesn't already exist), and will name the subdirectory you require renamed with the output of the datecommand:

curl -o /db-bkups/$(date +"%b-%d-%Y")/timestamp-db.dump --create-dirs http://www.w3schools.com/xml/simple.xml

The result is a file stored in a directory like so /db-bkups/Nov-04-2016/timestamp-db.dump.

Jaynajayne answered 4/11, 2016 at 14:57 Comment(3)
Tried this with cygwin curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL with no joy. Luckily I am able to create the parent directories, so there is a work around ... it is a pain though!!Osburn
Works with the current official native Windows builds of curl.Marrissa
for ftp or sftp connections the key is --ftp-create-dirs. For example curl -T path/to/local_file.txt ftp://1.2.3.4/my_new_folder/ --ftp-create-dirs -u username:passwordTray

© 2022 - 2024 — McMap. All rights reserved.