在 OSX 中从文件创建/修改日期中减去时间

发布于 2024-12-14 08:21:08 字数 233 浏览 1 评论 0原文

我正在尝试将一系列文件的日期移动 9 小时。我已经达到了这样的程度:

for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done

这应该适用于最近的系统,但 OSX 中的 touch 命令似乎有点过时,并且不支持 -d 开关。

我用的是雪豹。关于使用单行命令执行此操作的最佳选择有什么想法吗?我不想为此创建脚本。

I am trying to shift the dates of a series of files by 9 hours. I've reached as far as this:

for i in *.MOV; do touch -r "$i" -d "-9 hours" "$i"; done

This should work in recent systems, but the touch command in OSX seems to be a bit outdated and not to support the -d switch.

I'm using Snow Leopard. Any idea on the best option for doing this with a single line command? I don't want to create a script for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜尕妞 2024-12-21 08:21:09

好吧,整理一下。 OSX 附带了一个 gtouch 命令,该命令知道 -d 开关。它是 GNU coreutils 的一部分。有关特定 MacOS 版本的可用性的信息,请参阅下面的评论。

有关通过 -d 开关使用相对日期的更多信息,请参阅 手册

Ok, sorted it out. OSX comes with a gtouch command, that knows the -d switch. It's part of GNU coreutils. See the comments below for information regarding availability on specific MacOS versions.

For more information on using relative dates with the -d switch see the manual.

心是晴朗的。 2024-12-21 08:21:09

查看 Touch 维基百科页面,看来您已经习惯了 GNU触摸版。哪个 MacOS 没有使用。

对于您想要执行的操作,请查看“SetFile”命令,该命令随 XCode 工具一起安装。您有 -d 和 -m 选项,它们会重置创建和修改日期及时间。次。

http://developer.apple。 com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html

Looking at the Wikipedia Page for Touch, it appears you're accustomed to the GNU version of Touch. Which MacOS isn't using.

For what you want to do, look into the "SetFile" command, which gets installed with XCode tools. You have -d and -m options, which reset the Created and Modified dates & times respectively.

http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/SetFile.1.html

剩一世无双 2024-12-21 08:21:09

Donno OS X,但它应该很容易,

get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command

所有这些当然都可以在命令行上的单个 for 循环中完成。

以下是来自 WikiPedia 的简单示例,显示了来回转换。

# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879

# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009

# Or:
date -ud @1000000000
# Sun Sep  9 01:46:40 UTC 2001

# or: Haven't tested this but should work..
date -d @1000000000 +%y%m%d%%H%M%S
# 010909014640

Donno OS X, but it should be easy enough to

get curr time stamp on the file
convert it to seconds
subtract 9 hours (9*60*60 secs) from it
convert it back to the format accepted by touch's -t option
run touch command

All this of course can be done in a single for loop on command line.

Here are simple examples from WikiPedia showing back and forth conversion.

# To convert a specific time stamp to Unix epoch time (seconds since 1970-01-01):
date +"%s" -d "Fri Apr 24 13:14:39 CDT 2009"
# 1240596879

# To convert Unix epoch time (seconds since 1970-01-01) to a human readable format:
date -d "UTC 1970-01-01 1240596879 secs"
# Fri Apr 24 13:14:39 CDT 2009

# Or:
date -ud @1000000000
# Sun Sep  9 01:46:40 UTC 2001

# or: Haven't tested this but should work..
date -d @1000000000 +%y%m%d%%H%M%S
# 010909014640
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文