乐闻世界logo
搜索文章和话题

How to download a file into a directory using curl or wget?

1个答案

1

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:

bash
curl -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:

bash
cd /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:

bash
wget -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:

bash
cd /downloads wget https://example.com/image.jpg

Summary

  • With curl, specify the full filename including the path using -o, or use -O to download to the current directory.
  • With wget, specify the download directory using -P, or directly use wget in 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.

2024年8月13日 22:43 回复

你的答案