获取目录中最新条目(文件或目录)的日期
我有一堆目录,其中包含同一 C++ 项目的不同修订版。我想把这些目录移动到以 YYYY.MM.DD 模式命名的父目录中,以解决问题。其中
YYYY.MM.DD
是目录中最新条目(文件或目录)的日期。
如何递归查找特定目录中最新条目的日期?
更新
以下是执行此操作的方法之一:
find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1
甚至:
find . -not -type d -printf "%TY.%Tm.%Td\n" | sort -n | tail -1
I have a bunch of directories with different revisions of the same c++ project within. I'd like to make things sorted out moving each of these directories to a parent directory named by pattern of YYYY.MM.DD
. Where YYYY.MM.DD
is the date of the most recent entry (file or directory) in a directory.
How can I recursively find the date of the most recent entry in a particular directory?
Update
Below is one of the ways to do it:
find . -not -type d -printf "%T+ %p\n" | sort -n | tail -1
Or even:
find . -not -type d -printf "%TY.%Tm.%Td\n" | sort -n | tail -1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 ls -t|head -n 1 列出按修改日期排序的文件并仅显示第一个。日期将采用您的区域设置定义的格式(即 YYYY-MM-DD)。
例如,
将遍历所有文件并为每个修改数据创建一个目录并将文件移动到那里(注意:必须注意包含空格的文件名)。现在使用在源树的根目录中查找 -type d ,以递归方式列出您现在拥有的所有目录(遗憾的是现在有一些开销):
这不会递归地遍历树,而是获取完整树的所有目录,然后迭代它们。如果您需要源代码树之外的日期目录(假设如此),只需相应地编辑 awk 脚本中的两个 system() 调用即可。
已编辑:修复脚本,添加更多说明
Try using
ls -t|head -n 1
to list files sorted by modification date and show only the first. The date will be in the format defined by your locale (ie YYYY-MM-DD).For example,
will go through all files and create a directory for every modification data and move the file there (beware: care must be taken for filenames containing whitespace). Now use
find -type d
in the root directory of the source tree to recursively list all the directories. Combined with the above you have now (sadly there is some overhead now):This does not go recursively through the tree, but takes all directories of the complete tree and then iterates over them. If you need the date-directories outside of the source tree (suppose so), just edit the two system() calls in the awk script accordingly.
Edited: fix script, add more description
另一种选择是将您的解决方案与之前的答案混合:
它也显示目录。
Another option, mixing your solution with the previous answer:
It shows directories as well.