如果你启动一个终端,使用stat你可以得到所有文件的修改时间和对应的名字,用冒号分隔如下:
stat -f "%m:%N" *
样本输出
1476985161:1.png
1476985168:2.png
1476985178:3.png
1476985188:4.png
...
1476728459:Alpha.png
1476728459:AlphaEdges.png
您现在可以对其进行排序并取第一行,然后删除时间戳,以便您获得最新文件的名称:
stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:
样本输出
result.png
现在,您可以将它放在一个变量中,并使用touch 设置所有其他文件的修改时间以匹配其修改时间:
newest=$(stat -f "%m:%N" *png | sort -rn | head -1 | cut -f2 -d:)
touch -r "$newest" *
因此,如果您希望能够对任何给定的目录名称执行此操作,您可以在您的 HOME 目录中创建一个名为 setMod 的小脚本,如下所示:
#!/bin/bash
# Check that exactly one parameter has been specified - the directory
if [ $# -eq 1 ]; then
# Go to that directory or give up and die
cd "$1" || exit 1
# Get name of newest file
newest=$(stat -f "%m:%N" * | sort -rn | head -1 | cut -f2 -d:)
# Set modification times of all other files to match
touch -r "$newest" *
fi
然后使那个可执行文件,只需要一次,使用:
chmod +x $HOME/setMod
现在,您可以像这样设置/tmp/freddyFrog中所有文件的修改时间:
$HOME/setMod /tmp/freddyFrog
或者,如果您愿意,可以使用 Applescript 调用它:
do shell script "$HOME/setMod " & nameOfDirectory
nameOfDirectory 需要看起来像 Unix-y(如 /Users/mark/tmp)而不是 Apple-y(如 Macintosh HD:Users:mark:tmp)。