深度阅读

How do I list only files that were modified within a specified time period using the ls command in Linux?

作者
作者
2023年08月22日
更新时间
12.36 分钟
阅读时间
0
阅读量

To list only files that were modified within a specified time period using the ls command in Linux, you can use the -ltr options to list the files in reverse order of modification time, and then use the grep command to filter the results based on the modification time. For example, to list all files modified within the last 24 hours, you can use the following command:

ls -ltr | grep "$(date -d 'now - 1 day' '+%b %e')"

Here, ls -ltr lists all files in reverse order of modification time, with the most recently modified files listed last. The grep command searches for lines that match the modification time of files modified within the last 24 hours, using the date command to generate the date range string.

You can adjust the time range by changing the argument passed to the -d option of the date command. For example, to list all files modified within the last 7 days, you can use the following command:

ls -ltr | grep "$(date -d 'now - 7 days' '+%b %e')"

Note that this method is not as reliable as using the find command, as it relies on the output of the ls command, which can be affected by factors such as the current timezone and differences in file system mounting options.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。