Linux 如何在不创建父目录的情况下使用 wget 镜像目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5043239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:22:16  来源:igfitidea点击:

How do I mirror a directory with wget without creating parent directories?

linuxftpwgetmirror

提问by Tom Boutell

I want to mirror a folder via FTP, like this:

我想通过 FTP 镜像文件夹,如下所示:

wget --mirror --user=x --password=x ftp://ftp.site.com/folder/subfolder/evendeeper

But I do not want to create a directory structure like this:

但我不想创建这样的目录结构:

ftp.site.com -> folder -> subfolder -> evendeeper

ftp.site.com -> 文件夹 -> 子文件夹 -> evendeeper

I just want:

我只是想:

evendeeper

更深的

And anything below it to be the resulting structure. It would also be acceptable for the contents of evendeeperto wind up in the current directory as long as subdirectories are created for subdirectories of evendeeperon the server.

和它下面的任何东西都是最终的结构。evendeeper只要为evendeeper服务器上的子目录创建了子目录,的内容也可以在当前目录中结束。

I am aware of the -npoption, according to the documentation that just keeps it from following links to parent pages (a non-issue for the binary files I'm mirroring via FTP). I am also aware of the -ndoption, but this prevents creating any directory structure at all, even for subdirectories of evendeeper.

我知道这个-np选项,根据文档,它只是阻止它跟随到父页面的链接(对于我通过 FTP 镜像的二进制文件来说不是问题)。我也知道该-nd选项,但这根本无法创建任何目录结构,即使是evendeeper.

I would consider alternatives as long as they are command-line-based, readily available as Ubuntu packages and easily automated like wget.

我会考虑替代方案,只要它们是基于命令行的,可作为 Ubuntu 软件包随时使用,并且像 wget 一样易于自动化。

采纳答案by vs_inf

For a path like: ftp.site.com/a/b/c/d

对于像这样的路径: ftp.site.com/a/b/c/d

-nHwould download all files to the directory a/b/c/din the current directory, and -nH --cut-dirs=3would download all files to the directory din the current directory.

-nH将所有文件下载到当前目录a/b/c/d中的目录中,并将-nH --cut-dirs=3所有文件下载到当前目录d中的目录中。

回答by Marc B

-np(no parent) option will probably do what you want, tied in with -L 1(I think, don't have a wget install before me), which limits the recursion to one level.

-np(no parent) 选项可能会做你想做的事情,与-L 1(我认为,在我之前没有 wget 安装),这将递归限制在一个级别。

EDIT. ok. gah... maybe I should wait until I've had coffee.. There is a --cutor similar option, which allows you to "cut" a specified number of directories from the output path, so for /a/b/c/d, a cut of 2 would force wget to create c/don your local machine

编辑。好的。gah...也许我应该等到我喝了咖啡..有一个--cut或类似的选项,它允许您从输出路径中“剪切”指定数量的目录,因此对于/a/b/c/d2 的剪切将强制 wgetc/d在本地机器上创建

回答by Tony TCG

Instead of using:

而不是使用:

-nH --cut-dirs=1

use:

用:

-nH --cut-dirs=100

This will cut more directories and no folders will be created.

这将剪切更多目录,并且不会创建任何文件夹。

Note: 100 = the number of folders to skip creating. You can change 100 to any number.

注意:100 = 要跳过创建的文件夹数。您可以将 100 更改为任意数字。

回答by pr-pal

I had a similar requirement and the following combination seems to be the perfect choice:

我有类似的要求,以下组合似乎是完美的选择:

In the below example, all the files in http://url/dir1/dir2(alone) are downloaded to local directory /dest/dir

在下面的例子中,http://url/dir1/dir2(单独)中的所有文件都下载到本地目录/dest/dir

wget  -nd -np -P /dest/dir --recursive http://url/dir1/dir2

Thanks @ffledgling for the hint on "-nd"

感谢@ffledgling 关于“-nd”的提示

For the above example:

对于上面的例子:

wget -nd -np --mirror --user=x --password=x ftp://ftp.site.com/folder/subfolder/evendeeper

Snippets from manual:

手册摘录:

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