marcamillion
marcamillion

Reputation: 33755

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

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.

Upvotes: 35

Views: 25920

Answers (1)

Zlemini
Zlemini

Reputation: 4963

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.

Upvotes: 83

Related Questions