如何使用wget指定下载位置?

923

我需要将文件下载到 /tmp/cron_test/ 下。我的wget代码如下:

wget --random-wait -r -p -nd -e robots=off -A".pdf" -U mozilla http://math.stanford.edu/undergrad/

那么有没有某个参数可以指定目录呢?


1
如果使用“-i”或“--input-files”,人们很可能希望使用“-x”,以强制将其下载到与每个URL模式匹配的相应本地目录中。 - Andrew
5个回答

1200

从手册页面获取:

-P prefix
--directory-prefix=prefix
           Set directory prefix to prefix.  The directory prefix is the
           directory where all other files and sub-directories will be
           saved to, i.e. the top of the retrieval tree.  The default
           is . (the current directory).

因此,您需要向您的命令添加-P /tmp/cron_test/(缩写形式)或--directory-prefix=/tmp/cron_test/(长格式)。还要注意,如果该目录不存在,则会创建该目录。


8
/P tmp/cron_test/ 不起作用,但是去掉 / 就可以工作了,甚至可以创建不存在的目录。 - Kangarooo
13
袋鼠,它可能无法正常工作,因为你的系统上没有/tmp/cron_test/目录。 :P - Sameer Alibhai
75
手册的描述让这个选项很难搜索。我不认为我想要保存东西的位置是一个“目录前缀”。谢谢分享! - Iain Samuel McLean Elder
8
根据http://serverfault.com/questions/354792/downloading-from-wget-folder-issue,您可以通过选项`--no-host-directories`或`-nH`来删除根文件夹。 - Alien Life Form
4
嗯,"-P"选项在我这里(在18.04上)不能正常工作,至少与"-O"选项一起不能。我需要注意什么其他细节吗? - artu-hnrq
显示剩余5条评论

667

-O 是指定要下载到的文件路径的选项:

wget <uri> -O /path/to/file.ext

-P 是一个前缀,它将把文件下载到该目录中:

wget <uri> -P /path/to/folder

18
我会添加一个斜杠,使其变成 /path/to/folder/ - Louis Maddox
63
感谢您还特别指明了 -O 选项,虽然我不需要它,但这让我更加自信地知道 -P 才是我需要的。 - WORMSS
根据我的机器上的man页面, 默认前缀是“.”(即当前目录)。在“目录前缀”后添加尾随斜杠会导致双斜杠错误(例如,my/favorite/dir/prefix//)。此外,考虑到语义,“目录前缀”不需要尾随斜杠。 - Timothy L.J. Stewart
2
@TimothyL.J.Stewart 没有双斜杠错误。 - alephreish
4
NB:“-O”会覆盖“-P”,因此您不能仅指定输出目录(请考虑dirname和仅输出文件名(请考虑basename)。对于此操作,请只使用“-O”并指定完整的文件路径。 - murla

10

确保您下载的内容的URL是正确的。首先,包含像?等字符的URL无法被解析和解决。这会使cmd命令行混淆,并接受任何未解决为源URL名称的字符作为您要下载到的文件名。

例如:

wget "sourceforge.net/projects/ebosse/files/latest/download?source=typ_redirect"

将下载的文件命名为 ?source=typ_redirect

如你所见,了解 URL 对于理解 wget 有所帮助。

我正在使用 hirens 光盘引导计算机,并且只能使用 Linux 2.6.1 作为资源(无法使用 import os)。解决我的问题并将 ISO 下载到物理硬盘上的正确语法是:

wget "(source url)" -O (directory where HD was mounted)/isofile.iso" 

通过查找 wget 下载到名为 index.html(默认文件)的文件的位置,并使用以下命令显示所需文件的正确大小/其他属性,可以确定正确的 URL:

wget "(source url)"

当URL和源文件正确,并下载到index.html时,您可以停止下载 (ctrl+z) 并使用以下命令更改输出文件:

-O "<specified download directory>/filename.extension"

在源URL之后。

在我的情况下,这将导致下载一个ISO文件并将其存储为二进制文件,命名为isofile.iso,希望它能够挂载。


7
"-P" 是正确的选项,请继续阅读获取更多相关信息:
使用 "wget -nd -np -P /dest/dir --recursive http://url/dir1/dir2" 命令即可将网站目录下载到本地指定目录。
以下是方便起见,摘自 man 手册的相关摘录:
   -P prefix
   --directory-prefix=prefix
       Set directory prefix to prefix.  The directory prefix is the directory where all other files and subdirectories will be saved to, i.e. the top of the retrieval tree.  The default is . (the current directory).

   -nd
   --no-directories
       Do not create a hierarchy of directories when retrieving recursively.  With this option turned on, all files will get saved to the current directory, without clobbering (if a name shows up more than once, the
       filenames will get extensions .n).


   -np
   --no-parent
       Do not ever ascend to the parent directory when retrieving recursively.  This is a useful option, since it guarantees that only the files below a certain hierarchy will be downloaded.

-1

man wget: -O 文件名 --output-document=文件名

wget "url" -O /tmp/cron_test/<file>

2
这不是一个回答,因为 -o 选项会将下载的文件放入指定的文件中。但问题是“如何将文件放入文件夹中”。而且已经有重复的答案存在。 - Anton Gorbunov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接