How do I list only files that were modified within the last day using the ls command in Linux?
Published on Aug. 22, 2023, 12:17 p.m.
The ls
command is not the best tool to use for listing files modified within a certain time frame, as it only displays basic information about files in the current directory. A better tool to use for this purpose is the find
command, which allows you to search for files that match a certain criteria, such as files modified within a certain time frame.
To find all files modified within the last day in the current directory and its subdirectories, you can use the following command:
find . -type f -mtime 0
Here:
.
specifies the current directory as the starting point for the search.-type f
only matches regular files (excluding directories and other special files).-mtime 0
matches files whose modification time is within the last 24 hours (0 days).
This command will list all regular files in the current directory and its subdirectories that have been modified within the last day.
If you want to use the ls
command instead, you can combine it with find
:
find . -type f -mtime 0 -exec ls -l {} +
This command will find all regular files modified within the last day, and then use ls -l
to list their information in long format. The +
at the end of the find
command is used to group multiple results into a single command, to speed up the process if there are a large number of files.
Note that the above commands assume that your system’s timezone is set correctly. If your system’s timezone is not correct, you can adjust the command accordingly by adding or subtracting the time difference in hours.