I am using curl to download the file from a url. Currently I am able to download the file as it is in the current directory using the curl command like

curl -O http://path/to/url/17.01.24/smcfs-17.01.24.ear

This downloads the file alright but saves it in the current directory by the name smcfs-17.01.24.ear as expected. However I would like to download and save this file by creating a directory inside the current directory with bash script like

if [[ ! -d "$4" ]]; then
    mkdir "$4"
    curl -O http://path/to/url/$4/smcfs-$4.ear   #I would like curl to fucntion like I said here    

NOTE: In above code snippet I am taking the version number i.e 17.01.24 as 4rth argument of script

The expected output will be that curl download the file smcfs-17.01.24.ear by the name smcfs.ear in the directory 17.01.24 which is the subdirectory of the current diretory.

I would like to achieve this without going into the subdirectory, using curl to download and then coming back to the current directory.


I used the following to download a file into a directory with a given name

curl "http://path/to/url/$4/smcfs-$4.ear" --output "$4/smcfs.ear"
| improve this answer | |

You can obtain the behaviour you’re after by dropping the -O:

curl "http://path/to/url/$4/smcfs-$4.ear" > "$4/smcfs.ear"

(after the mkdir). By default curl outputs the content it downloads to its standard output; -O tells it to save it to a file matching the remote name.

| improve this answer | |

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.