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

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

您可以使用touch命令更改文件的修改时间:

 touch filename 

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

 touch -d "2 hours ago" filename 

如果您想要相对于其现有修改时间修改文件,以下应该可以做到这一点:

 touch -d "$(date -R -r filename) - 2 hours" filename 

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

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

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

 find DIRECTORY -exec touch -d "2 hours ago" {} + 

文件时间相对版本无法使用此表单,因为它使用shell来形成要touch的参数。

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

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

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

 find -print | while read filename; do # do whatever you want with the file touch -t 201203101513 "$filename" done 

点击进入后,你会看到一个“>”,最后一次执行 – >“完成”。

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

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

查看我的网页

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

 touch -a -m -t 201512180130.09 fileName.ext 

哪里:

 -a = accessed -m = modified -t = timestamp - use [[CC]YY]MMDDhhmm[.ss] time format 

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

validation它们是否完全相同: stat fileName.ext

见: 触摸男人

Touch可以单独引用文件的日期,无需调用date或使用命令替换。 这里有点来自touch的信息页面:

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

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

 touch -r "file" -d '+8 hour' "file" 

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

 for i in *; do touch -r "$i" -d '+8 hour' "$i"; done 

我听说使用*并让我们选择文件名本身更安全 ,但使用find -print0 | xargs -0 touch ... find -print0 | xargs -0 touch ...应该处理大多数疯狂的字符,如文件名中的换行符,空格,引号,反斜杠。 (PS。首先尝试不在文件名中使用疯狂的字符)。

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

 find thatdir -name "s*" -print0 | xargs -0 -I '{}' touch -r '{}' -d '+1 day' '{}' 

这个小脚本至少对我有用

 #!/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 

自从我编写任何类型的Unix程序以来已经很长时间了,但是我不小心在一堆圣诞节照片上设置了年份,我知道如果我没有将日期从2015年更改为2014年,那么以后就会出现问题。

也许,这是一项简单的任务,但我找不到任何简单的方法。

我修改了一个我在这里找到的脚本,最初用于修改日期减去一个月。

这是原始脚本:

 #!/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 

这是我修改过的脚本,强制日期为“2014”:

 #!/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 $* 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 +1y -r $(stat -f %m "${f}") +2014%m%d%H%M.%S) "${f}" done 

我现在意识到我可以做一个更通用的版本:

 #!/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 $* 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 (+1y adds a year "-1y" subtracts a year) # Below line subtracts a year touch -t $(date -v -1y -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}" # Below line adds a year # touch -t $(date -v +1y -r $(stat -f %m "${f}") +%Y%m%d%H%M.%S) "${f}" done 

要使用此文件,您需要编写它

 chmod +x fn 

执行:

 ./fn files-to-change 

FN =您的文件名称,也就是说,是 – 你的命令

 ./fn *.JPG 

将在您所在的目录中将日期更改为减去一年。

只需在设置中更改日期和时间。 然后保存您的文件,它会自动更改