我有一个web目录,我存储一些配置文件。我想使用wget将这些文件拉下来并保持它们当前的结构。例如,远程目录看起来像:

http://mysite.com/configs/.vim/

.vim包含多个文件和目录。我想用wget在客户端复制它。似乎无法找到正确的wget标志组合来完成这项工作。什么好主意吗?


当前回答

递归下载一个目录,该目录拒绝index.html*文件,下载时不包含主机名、父目录和整个目录结构:

wget -r -nH --cut-dirs=2 --no-parent --reject="index.html*" http://mysite.com/dir1/dir2/data

其他回答

Wget 1.18可能工作得更好,例如,我被1.12版本的bug咬了,其中…

wget --recursive (...)

...只检索index.html而不是所有文件。

解决方法是注意到一些301重定向,并尝试新的位置-给定新的URL, wget得到目录中的所有文件。

下面的选项似乎是处理递归下载时的完美组合:

wget -nd -n -P -P /dest/dir -充值http://url/dir1 dir2

为方便起见,手册页中的相关片段:

   -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.

对于其他有类似问题的人。Wget遵循robots.txt,这可能不允许您抓取站点。不用担心,你可以把它关掉:

wget -e robots=off http://www.example.com/

http://www.gnu.org/software/wget/manual/html_node/Robot-Exclusion.html

您必须将-np/——no-parent选项传递给wget(当然,除了-r/——recursive之外),否则它将遵循我的站点上目录索引中的链接到父目录。所以命令看起来是这样的:

wget --recursive --no-parent http://example.com/configs/.vim/

为了避免下载自动生成的index.html文件,使用-R/——reject选项:

wget -r -np -R "index.html*" http://example.com/configs/.vim/

如果——没有家长帮助,你可以使用——include选项。

目录结构:

http://<host>/downloads/good
http://<host>/downloads/bad

你想下载的是downloads/good而不是downloads/bad目录:

wget --include downloads/good --mirror --execute robots=off --no-host-directories --cut-dirs=1 --reject="index.html*" --continue http://<host>/downloads/good