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

How to change filename of a file downloaded with wget?

1个答案

1

When using wget to download files, you may often encounter situations where you need to change the filename of the downloaded file. wget provides a convenient parameter -O (capital O, representing Output), which allows users to specify the filename for the downloaded file.

Using the -O Parameter to Change the Filename

Suppose you want to download a PDF file from a URL, where the original filename is document.pdf, but you wish to save it as mydocument.pdf. You can use the following command:

bash
wget -O mydocument.pdf http://example.com/document.pdf

This command instructs wget to download the file from the specified URL and use the -O parameter to specify the filename for the downloaded file as mydocument.pdf.

Practical Application Example

For instance, if you need to automate downloading daily reports in your work, and the report URL is fixed, but you need to name the report based on the download date, you can combine it with date functions in shell scripting:

bash
wget -O report-$(date +%Y-%m-%d).pdf http://example.com/dailyreport.pdf

This command uses the current date (e.g., 2023-03-15) to form the filename, saving the downloaded PDF report as report-2023-03-15.pdf.

Important Notes

  • Ensure that when using the -O parameter, the specified filename includes the correct file extension (e.g., .pdf, .jpg), which is crucial for subsequent use of the file.
  • If the specified filename already exists, wget will overwrite the file unless other wget parameters are used to prevent this.

Using the -O parameter is a straightforward and effective method to control the naming of files downloaded with wget, making it highly suitable for scenarios requiring custom naming of downloaded files.

2024年7月30日 00:17 回复

你的答案