在 UNIX 中递归地查找最近更新的文件

发布于 2024-09-13 22:26:19 字数 422 浏览 3 评论 0原文

对于我正在开发的网站,我希望能够在每晚进行 git 提交时自动更新页脚中的“此页面上次修改时间:”部分。本质上,我计划编写一个在每晚午夜运行的 shell 脚本,这将完成我所有的常规服务器维护工作。大多数这些任务我已经知道如何自动化,但我有一个文件(footer.php),它包含在每个页面中,并显示网站上次更新的日期。我希望能够递归地浏览我的网站并检查每个文件的时间戳,然后如果其中任何一个在 footer.php 中的日期之后进行了编辑,我想更新此日期。

我所需要的只是一个 UNIX 命令,该命令将递归地迭代我的文件并仅返回上次修改的日期。我不需要文件名或进行了哪些更改,我只需要知道最近更新的文件被更改的一天(希望是时间)。

我知道使用“ls -l”和“cut”我可以迭代每个文件夹来执行此操作,但我希望有一个运行速度更快且更简单的命令。最好是单行 shell 命令(可能带有 -R 参数)

For a website I'm working on I want to be able to automatically update the "This page was last modified:" section in the footer as I'm doing my nightly git commit. Essentially I plan on writing a shell script to run at midnight each night which will do all of my general server maintenance. Most of these tasks I already know how to automate, but I have a file (footer.php) which is included in every page and displays the date the site was last updated. I want to be able to recursively look through my website and check the timestamp on every file, then if any of these were edited after the date in footer.php I want to update this date.

All I need is a UNIX command that will recursively iterate through my files and return ONLY the date of the last modification. I don't need file names or what changes were made, I just need to know a single day (and hopefully time) that the most recently updated file was changed.

I know using "ls -l" and "cut" I could iterate through every folder to do this, but I was hoping for a quicker-running and easier command. Preferably a single-line shell command (possibly with a -R parameter)

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

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

发布评论

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

评论(3

灼痛 2024-09-20 22:26:19

find 以 Unix 格式输出所有访问时间,然后排序并取最大的。
转换为所需的任何日期格式留给读者作为练习:

find /path -type f -iname "*.php" -printf "%T@" | sort -n | tail -1

The find outputs all the access times in Unix format, then sort and take the biggest.
Converting into whatever date format is wanted is left as an exercise for the reader:

find /path -type f -iname "*.php" -printf "%T@" | sort -n | tail -1
悸初 2024-09-20 22:26:19

GNU find

find /path -type -f -iname "*.php" -printf "%T+"

检查 find 手册页以使用其他 -printf 说明符。

GNU find

find /path -type -f -iname "*.php" -printf "%T+"

check the find man page to play with other -printf specifiers.

纸短情长 2024-09-20 22:26:19

您可能想要查看 inotify 脚本,该脚本会在每次修改任何其他文件时更新页脚,而不是在整个文件系统中查找新的更新。

You might want to look at a inotify script that updates the footer every time any other file is modified, instead of looking all through the file system for new updates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文