CentOS. Need to find files modified in last 30 days to see if any of them have been infected with malware.
I tried this:
root@server [/home/someuser/public_html/]# find . -mtime +30 -exec ls -l {} > last30days.txt \;
But instead of the last 30 days, it seems to have found everything. 22,000 files.
Use -mtime option with the find command to search files based on modification time followed by the number of days. Number of days can be used in two formats.
/directory/path/ is the directory path where to look for files that have been modified. Replace it with the path of the directory where you want to look for files that have been modified in the last N days. -mtime -N is used to match files that had their data modified in the last N days.
Finding Files Modified on a Specific Date in Linux: You can use the ls command to list files including their modification date by adding the -lt flag as shown in the example below. The flag -l is used to format the output as a log. The flag -t is used to list last modified files, newer first.
A couple of issues
>
in -exec
without something like bash -c '... > ...'
. Though the >
will overwrite the file, so you want to redirect the entire find
anyway rather than each -exec
. +30
is older
than 30 days, -30
would be modified in last 30 days.-exec
really isn't needed, you could list everything with various -printf
options. Something like below should work
find . -type f -mtime -30 -exec ls -l {} \; > last30days.txt
Example with -printf
find . -type f -mtime -30 -printf "%M %u %g %TR %TD %p\n" > last30days.txt
This will list files in format "permissions owner group time date filename". -printf
is generally preferable to -exec
in cases where you don't have to do anything complicated. This is because it will run faster as a result of not having to execute subshells for each -exec
. Depending on the version of find
, you may also be able to use -ls
, which has a similar format to above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With