首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >curl下载文件保留服务器端文件名和时间戳

curl下载文件保留服务器端文件名和时间戳

原创
作者头像
skystone
发布2026-01-18 13:44:29
发布2026-01-18 13:44:29
1040
举报
文章被收录于专栏:Linux DocLinux Doc

有时候,我们通过curl下载文件时,希望文件保存时使用该文件在服务器端的文件名和时间戳,那么可以使用-O和-R两个选项。如下:

代码语言:txt
复制
curl -R -O https://mirrors.tencent.com/tlinux/4.4/images/qcow2/x86_64/TencentOS-Server-GenericCloud-4.4-20251223.0.x86_64.qcow2

可以通过man curl命令查看帮助文档,了解这2个选项的含义和用法。

-O选项,保存到本地文件,文件名使用服务器端的文件名。

代码语言:txt
复制
       -O, --remote-name
              Write output to a local file named like the remote file we get. (Only the file part of the remote file is used, the path is cut
              off.)

              The  file  is saved in the current working directory. If you want the file saved in a different directory, make sure you change
              the current working directory before invoking curl with this option or use --output-dir.

              The remote file name to use for saving is extracted from the given URL, nothing else, and if it already exists it is  overwrit‐
              ten.  If you want the server to be able to choose the file name refer to -J, --remote-header-name which can be used in addition
              to this option. If the server chooses a file name and that name already exists it is not overwritten.

              There is no URL decoding done on the file name. If it has %20 or other URL encoded parts of the name, they end up as-is as file
              name.

              You may use this option as many times as the number of URLs you have.

              -O, --remote-name can be used several times in a command line

              Example:
               curl -O https://example.com/filename

              See also --remote-name-all, --output-dir and ‐J, --remote-header-name.

-R选项,保存到本地文件,使用服务器端的文件时间戳。

代码语言:txt
复制
       -R, --remote-time
              Makes curl attempt to figure out the timestamp of the remote file that is getting downloaded, and if that is available make the
              local file get that same timestamp.

              Providing -R, --remote-time multiple times has no extra effect.  Disable it again with --no-remote-time.

              Example:
               curl --remote-time -o foo https://example.com

              See also ‐O, --remote-name and ‐z, --time-cond.

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档