When using curl or wget to download files to a specified directory, first verify that these tools are installed on your system. If installed, follow these steps to download files using these tools.
Using curl to Download Files
curl is a powerful tool for transferring data from servers, supporting various protocols including HTTP, HTTPS, and FTP. To download a file to a specific directory using curl, use the -o or -O option.
Example:
Suppose you want to download an image and save it to the /downloads directory with the filename image.jpg:
bashcurl -o /downloads/image.jpg https://example.com/image.jpg
Here, specify the full path to save the file using the -o option. To have curl use the last part of the URL as the filename, use -O (capital O), and first change to the target directory using cd:
bashcd /downloads curl -O https://example.com/image.jpg
Using wget to Download Files
wget is another popular command-line tool for downloading files, supporting HTTP, HTTPS, and FTP protocols. Similar to curl, wget can easily download files to a specified directory.
Example:
If you want to download the same image.jpg file and save it to the /downloads directory:
bashwget -P /downloads https://example.com/image.jpg
The -P option lets you specify the directory for saving the downloaded file. Alternatively, you can first change to the target directory and then execute the download:
bashcd /downloads wget https://example.com/image.jpg
Summary
- With
curl, specify the full filename including the path using-o, or use-Oto download to the current directory. - With
wget, specify the download directory using-P, or directly usewgetin the target directory.
These tools are both highly effective for downloading files, and you can choose which one to use based on your needs and preferences.