How do I list only files that were modified within a specified time period using the ls command in Linux?
Published on Aug. 22, 2023, 12:17 p.m.
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.