The man curl entry for curl -O says:

If you want the file saved in a different directory, make sure you change cur‐ rent working directory before you invoke curl with the -O, --remote-name flag!

Why it isn't possible to download the file to a specific directory and one has to do cd /example to download the file to a specific directory?

I know it's possible with wget this way:

wget -P /example https://example.com/file

But I do wonder why it seemingly isn't with curl.


This is because it is how curl is designed.

You can nonetheless achieve writing to output directory via shell redirection. Example:

$ curl http://example.com/file > /path/to/destination/file
| improve this answer | |

Why it isn't possible to download the file to a specific directory and one has to do cd /example to download the file to a specific directory?

Because curl has been designed that way. Since it is free software, you could download its source code (perhaps with git clone https://github.com/curl/curl.git) and study it. You'll find out that the chdir(2) system call is never used in it (it only appears in testing scripts coded in Perl).

If you don't want to explicitly cd in your shell before using curl, you might use something else, or patch the curl source code to add such an option (e.g. you could improve its code to accept some --chdir dirname program option, and then call the chdir syscall; in my opinion it is not worth the effort). Remember that each process has its own working directory (inherited from its parent process). See also credentials(7) and read some Linux programming book (perhaps the old, but freely downloadable, ALP; also intro(2) & syscalls(2))

Of course you could use curl with absolute paths only (consider using realpath(1) in your shell scripts, or realpath(3) in your C programs, to get one)..

You could also and much more simply wrap curl in some shell script doing what you want; maybe some incurl script as simple as:

# this is your incurl shell script
cd "$1"
exec curl "$@"

that you'll use e.g. with incurl /tmp/ -O foo http://example.com/path/foobar.txt (to get some /tmp/foo from that URL).

You might also use the libcurl library, perhaps from some scripting language (like Guile, Python, Lua, Perl, etc ...), or from a program written in a language implemented by compilers (Rust, Go, C++, C, Ocaml, ....). You could even find other HTTP client libraries (and HTTP server libraries too).

As to why the curl designers did not think of more features, bells and whistles, read about the Unix philosophy. curl is a command line software which does one thing, but does it well. You'll compose or wrap it with other utilities (perhaps as simple as the incurl script above) to get more features. And sadly, curl does not make your morning coffee either.

PS. I am supposing you are using some POSIX or Unix-like OS such as Linux, MacOSX, AIX, ...

| improve this answer | |

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