简体   繁体   English

Linux-如何查找最近12个小时内未查找命令更改的文件

[英]Linux - How to find files changed in last 12 hours without find command

I need to find files modified in the last 12 hours. 我需要查找最近12个小时内修改过的文件。 However, the directory is quite large so using the usual find command takes way too long. 但是,目录很大,因此使用通常的find命令花费的时间太长。

Anyone have any ideas doing this quicker? 有人有更快的想法吗? I was thinking something like listing the files, then using head to get the top 20 and then check those files only. 我在想类似列出文件的方法,然后使用head获得前20名,然后仅检查那些文件。 But I'm not sure. 但是我不确定。

Any help? 有什么帮助吗?

UPDATE: Thanks to the help of the chosen answer, we have figured out that you can actually find a file without using the find command. 更新:感谢所选答案的帮助,我们发现您实际上可以在不使用find命令的情况下找到文件。 The trick is to timestamp the file names, then use the following code to get the latest one: 技巧是给文件名加上时间戳,然后使用以下代码获取最新的文件名:

ls -1 /directory/files*.txt | sort -nr | head -1

The file modification time is stored in its inode. 文件修改时间存储在其inode中。 So whatever command you use has to read inodes for all files in that directory. 因此,无论使用什么命令,都必须读取该目录中所有文件的inode。 You can make your own script for checking the mtime, but it won't be faster. 您可以创建自己的脚本来检查mtime,但不会更快。

Listing the directory contents (filenames only) is really fast, try with ls -1 ( ls minus one ).But listing it with file attributes like mtime is slow: ls -l ( ls minus little L ). 列出目录内容(仅文件名)是非常快的,请尝试使用ls -1ls减一个 ),但是使用诸如mtime之类的文件属性列出目录速度很慢: ls -lls减去小L )。

The file list in directory is read in "random" order by the filesystem (the order depends on many things, but is static). 文件系统以“随机”顺序读取目录中的文件列表(该顺序取决于许多因素,但是是静态的)。 So you can't use something like stopping after X number of files. 因此,您不能使用在X个文件后停止等操作。 ls -t lists the files sorted by mtime, but it has to read the mtime of all files in order to sort them. ls -t列出了按mtime排序的文件,但是它必须读取所有文件的mtime才能对其进行排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM