Saving a file downloaded with wget with a different name

Posted in Linux/Unix/BSD -

wget is a command line utility for downloading files from FTP and HTTP web servers. By default when you download a file with wget, the file will be written to the current directory,  with the same name as the filename in the URL. For example, if you were to download the little Tux penguin and BSD demon icon which is on this page, you would  use wget like so:

wget https://www.electrictoolbox.com/images/icons/linux-bsd.gif

This would save the icon file with the filename linux-bsd.gif into the current directory.

If you were to download a webpage with query string parameters in it (the ?foo=bar part of the webpage URL) then those will also be included in the filename. For example:

wget "http://www.example.com/somepage.html?foo=bar"

would be saved with the filename "somepage.html?foo=bar"

If there is already a file with the filename in the current directory, the default behaviour is to save it with the filename and append a .1 to the end. If there's already a .1 version it will be saved as .2, and so on as shown below:

$ wget https://www.electrictoolbox.com/images/icons/linux-bsd.gif
...
00:28:52 (393.04 KB/s) - `linux-bsd.gif' saved [805/805]

$ wget https://www.electrictoolbox.com/images/icons/linux-bsd.gif
...
00:28:53 (393.04 KB/s) - `linux-bsd.gif.1' saved [805/805]

$ wget https://www.electrictoolbox.com/images/icons/linux-bsd.gif
...
00:28:55 (1.64 MB/s) - `linux-bsd.gif.2' saved [805/805]

If you download a URL which ends in a / like the URL of this page (https://www.electrictoolbox.com/wget-save-different-filename/) then wget will save the file as index.html (or index.html.1, index.html.2 etc).

It is possible to save the downloaded file with a different filename using wget, by using the -O or --output-document options like so:

wget -O example.html https://www.electrictoolbox.com/wget-save-different-filename/

or

wget --output-document=example.html https://www.electrictoolbox.com/wget-save-different-filename/

It is possible to leave the = part out of the --output-document option so it looks like this instead, and it will still work:

wget --output-document example.html https://www.electrictoolbox.com/wget-save-different-filename/

Note that unlike the default behaviour when you don't specify a filename to save as, wget will not append .1, .2 and so on to the filename if the file already exists; it will simply overwrite the file.



Related posts:


Comments