Create Parent Directories with Curl Using the --create-dirs Flag
This can save you from writing a file to a temporary directory and then moving it after using mkdir.
Let’s say you want to download a file to a specific directory that doesn’t exist yet and also make it executable. I do this a lot with curl.
Not using --create-dirs
:
curl https://raw.githubusercontent.com/nickjj/notes/master/notes \
--output /tmp/notes \
&& chmod +x /tmp/notes \
&& mkdir -p /tmp/custom/dir/path \
&& mv /tmp/notes /tmp/custom/dir/path
Using --create-dirs
:
curl https://raw.githubusercontent.com/nickjj/notes/master/notes \
--create-dirs --output /tmp/custom/dir/path/notes \
&& chmod +x /tmp/custom/dir/path/notes
Since the last argument of the above command is the path of the file we can
even improve on that by using $_
which contains the last argument of the
previous command.
This way we can avoid duplicating the path without creating a custom variable:
curl https://raw.githubusercontent.com/nickjj/notes/master/notes \
--create-dirs --output /tmp/custom/dir/path/notes \
&& chmod +x "${_}"
If any of the directories exist then they won’t be created but the command will
still work. The directories get created with 0750
permissions.
--create-dirs
has been available since curl 7.10.3 (January 2003).
# Demo Video
Timestamps
- 0:32 – Saving a file with curl
- 1:15 – Saving a file to a directory that doesn’t exist, the hard way
- 2:46 – Simplifying things with the –create-dirs flag
- 3:39 – Using the shell to our advantage to make it better
- 4:46 – If the directories exist, curl is smart enough to still work
- 5:08 – This flag has been around for 20+ years
References
- https://github.com/nickjj/notes
- https://nickjanetakis.com/blog/why-you-should-put-braces-around-your-variables-when-shell-scripting
When did you learn about this flag? Let me know below!