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.
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
见:男人触摸
小智 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)
| 归档时间: |
|
| 查看次数: |
630908 次 |
| 最近记录: |