如何更改文件的修改/创建日期?

sne*_*nej 325 file-properties timestamp

有没有办法更改修改/创建文件的日期(在 Nautilus 中或使用 ls -l 命令显示)?理想情况下,我正在寻找一个命令,它可以将一大堆文件的日期/时间戳更改为更早或更晚的特定时间(例如 +8 小时或 -4 天等)。

Jam*_*dge 437

只要您是文件的所有者(或 root),您就可以使用以下touch命令更改文件的修改时间:

touch filename
Run Code Online (Sandbox Code Playgroud)

默认情况下,这会将文件的修改时间设置为当前时间,但有许多标志,例如-d选择特定日期的标志。因此,例如,要将文件设置为在现在前两小时被修改,您可以使用以下命令:

touch -d "2 hours ago" filename
Run Code Online (Sandbox Code Playgroud)

如果要相对于其现有修改时间修改文件,则应执行以下操作:

touch -d "$(date -R -r filename) - 2 hours" filename
Run Code Online (Sandbox Code Playgroud)

如果要修改大量文件,可以使用以下方法:

find DIRECTORY -print | while read filename; do
    # do whatever you want with the file
    touch -d "$(date -R -r "$filename") - 2 hours" "$filename"
done
Run Code Online (Sandbox Code Playgroud)

您可以将参数更改为find仅选择您感兴趣的文件。如果您只想更新相对于当前时间的文件修改时间,您可以将其简化为:

find DIRECTORY -exec touch -d "2 hours ago" {} +
Run Code Online (Sandbox Code Playgroud)

这种形式对于文件时间相对版本是不可能的,因为它使用 shell 来形成touch.

就创建时间而言,大多数 Linux 文件系统不会跟踪此值。有一个ctime与文件相关联,但它跟踪文件元数据上次更改的时间。如果文件从未更改其权限,则它可能会保留创建时间,但这是巧合。显式更改文件修改时间计为元数据更改,因此也会产生更新ctime.

  • 在 OSX 上,您可能希望通过 `brew install coreutils` 使用 `gtouch`。 (4认同)
  • 举个简单的例子,当所有文件都在同一个文件夹中时:`touch -d "2 hours ago" /path/*.txt`,例如。 (3认同)
  • 我还要补充一点,无法以任何标准方式更改 `ctime`。 (3认同)
  • 有关 `ctime` 作为元数据更改时间的信息来自 POSIX。我不知道我的答案中的 shell 片段是否适用于严格的 POSIX shell 实用程序。但它们肯定适用于 Ubuntu,这是本网站上答案的上下文。 (2认同)
  • `touch: 非法选项 -- d` (2认同)

Jad*_*eye 65

最简单的方法 - 访问和修改将是相同的:

touch -a -m -t 201512180130.09 fileName.ext
Run Code Online (Sandbox Code Playgroud)

在哪里:

-a = accessed
-m = modified
-t = timestamp - use [[CC]YY]MMDDhhmm[.ss] time format
Run Code Online (Sandbox Code Playgroud)

如果你想使用NOW只是删除-t和时间戳。

要验证它们是否相同: stat fileName.ext

见:男人触摸

  • 一起使用“-a”和“-m”可能是不必要的,“-a”是“仅更改访问时间”,“-m”是“仅更改修改时间”。如果您想更新两者,则无需使用任何选项。 (2认同)

小智 57

谢谢您的帮助。这对我有用:

在终端中转到日期编辑目录。然后输入:

find -print | while read filename; do
    # do whatever you want with the file
    touch -t 201203101513 "$filename"
done
Run Code Online (Sandbox Code Playgroud)

按下回车键后,您会看到“>”,最后一次除外 - >“完成”。

注意:您可能需要更改“201203101513”

"201203101513" = 是您希望此目录中所有文件的日期。


Xen*_*050 14

Touch 可以自己引用文件的日期,无需调用date或使用命令替换。这是触摸信息页面的一些内容:

`-r FILE' `--reference=FILE'
     Use the times of the reference FILE instead of the current time.
     If this option is combined with the `--date=TIME' (`-d TIME')
     option, the reference FILE's time is the origin for any relative
     TIMEs given, but is otherwise ignored.  For example, `-r foo -d
     '-5 seconds'' specifies a time stamp equal to five seconds before
     the corresponding time stamp for `foo'.  If FILE is a symbolic
     link, the reference timestamp is taken from the target of the
     symlink, unless `-h' was also in effect.
Run Code Online (Sandbox Code Playgroud)

例如,将 8 小时添加到文件的日期(file在空格等情况下引用的文件名):

touch -r "file" -d '+8 hour' "file"
Run Code Online (Sandbox Code Playgroud)

对当前目录中的所有文件使用循环:

for i in *; do touch -r "$i" -d '+8 hour' "$i"; done
Run Code Online (Sandbox Code Playgroud)

我听说使用 a*并让for选择文件名本身更安全,但 usingfind -print0 | xargs -0 touch ... 应该处理疯狂的字符,如文件名中的换行符、空格、引号、反斜杠。(PS。首先尽量不要在文件名中使用疯狂的字符)。

例如,要查找文件thatdir名以 开头的所有文件s,并在这些文件的修改时间戳中添加一天,请使用:

find thatdir -name "s*" -print0 | xargs -0 -I '{}' touch -r '{}' -d '+1 day' '{}'
Run Code Online (Sandbox Code Playgroud)


小智 5

这个小脚本至少对我有用:

#!/bin/bash

# find specific files
files=$(find . -type f -name '*.JPG')

# use newline as file separator (handle spaces in filenames)
IFS=$'\n'

for f in ${files}
do
 # read file modification date using stat as seconds
 # adjust date backwards (1 month) using date and print in correct format 
 # change file time using touch
 touch -t $(date -v -1m -r $(stat -f %m  "${f}") +%Y%m%d%H%M.%S) "${f}"
done
Run Code Online (Sandbox Code Playgroud)

  • 根据图像中的元信息调整图像的日期将非常有用。可以使用ImageMagick的名称。例如 'identify -verbose <image> |grep -i date'、'identify -format %[exif:DateTime] <image>' 可能会显示 '2015:01:08 10:19:10' (并非所有图像都有 exif 数据)。这有效(使用 sed 将日期转换为触摸可以处理的格式): 'touch -d $(identify -format %[exif:DateTime] $f|sed -r 's/:/-/;s/:/-/ ;') $f' (3认同)