wget命令下载文件并另存为不同的文件名

noo*_*der 491 wget download

我正在使用该wget命令下载文件.但是当它下载到我的本地机器时,我希望它保存为不同的文件名.

例如:我正在下载文件 www.examplesite.com/textfile.txt

我想用来wget将文件保存textfile.txt在我的本地目录中newfile.txt.我使用的wget命令如下:

wget www.examplesite.com/textfile.txt
Run Code Online (Sandbox Code Playgroud)

nau*_*cho 768

使用该-O file选项.

例如

wget google.com
...
16:07:52 (538.47 MB/s) - `index.html' saved [10728]
Run Code Online (Sandbox Code Playgroud)

wget -O foo.html google.com
...
16:08:00 (1.57 MB/s) - `foo.html' saved [10728]
Run Code Online (Sandbox Code Playgroud)

  • 手册页显示:"使用-O并不意味着简单地'使用名称文件而不是URL中的名称文件;' 相反,它类似于shell重定向:``wget -O文件http:// foo``的工作方式类似于``wget -O-http:// foo> file``;文件将被立即截断,并且所有下载的内容都将写在那里." (18认同)
  • @CraigJacobs你可以在控制台输出`wget`并将其附加到文件中.例如`wget -O - -o/dev/null http://google.com >> foo.html`.[参考文献](http://serverfault.com/questions/25779/how-do-i-pipe-a-downloaded-file-to-standard-output-in-bash) (4认同)
  • 这种方法会使 --timestamping 无效 (3认同)

mou*_*mer 88

还要注意命令行上的参数顺序.至少在某些系统上(例如CentOS 6):

wget -O FILE URL
Run Code Online (Sandbox Code Playgroud)

作品.但:

wget URL -O FILE
Run Code Online (Sandbox Code Playgroud)

不起作用.


Gab*_*les 30

在这种情况下可以使用或curlwget所有这 3 个命令都执行相同的操作,从 http://path/to/file.txt 下载文件并将其本地保存到“my_file.txt”中。

请注意,在下面的所有命令中,我还建议使用-L--location选项,curl以便遵循 HTML 302 重定向到文件的新位置(如果文件已移动)。wget不需要额外的选项来执行此操作,因为它会自动执行此操作。

# save the file locally as my_file.txt

wget http://path/to/file.txt -O my_file.txt  # my favorite--it has a progress bar
curl -L http://path/to/file.txt -o my_file.txt
curl -L http://path/to/file.txt > my_file.txt
Run Code Online (Sandbox Code Playgroud)

或者,要将文件在本地保存为与远程相同的名称,请wget单独使用,或curl-Oor 一起使用--remote-name

# save the file locally as file.txt

wget http://path/to/file.txt
curl -LO http://path/to/file.txt
curl -L --remote-name http://path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

请注意,-O上面所有命令中的 都是大写字母“O”。

该命令的好处wget是它显示了一个漂亮的进度条。

您可以通过比较 sha512 哈希值来证明上述 3 种技术中每组下载的文件完全相同。运行sha512sum my_file.txt上述每个命令并比较结果后,会发现所有 3 个文件都具有完全相同的 sha 哈希值(sha 和),这意味着这些文件逐字节完全相同。

参考

  1. 我在此处了解了该-L选项curlIs there a way to follow redirects with command line cURL?

另请参阅:如何将 cURL 输出捕获到文件?


ioM*_*rix 23

您将使用列出的Mechanical蜗牛命令.注意要使用的大写O. Full命令行可以是:

wget www.examplesite.com/textfile.txt --output-document=newfile.txt
Run Code Online (Sandbox Code Playgroud)

要么

wget www.examplesite.com/textfile.txt -O newfile.txt
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.


小智 8

使用CentOS Linux我发现最简单的语法是:

wget "link" -O file.ext
Run Code Online (Sandbox Code Playgroud)

"link"您要保存的网址在哪里,是"file.ext"您选择的文件名和扩展名.

  • 我不明白两年后用同样的答案来回答一个已经回答过的问题。 (3认同)

Lak*_*aka 8

wget -O yourfilename.zip remote-storage.url/theirfilename.zip
Run Code Online (Sandbox Code Playgroud)

会为你做的伎俩。

笔记:

a) 它是大写的 O。

b)wget -O filename url只会起作用。放在-O最后不会。