Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all files modified in last 30 days in a directory

Tags:

find

centos

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.

like image 500
Buttle Butkus Avatar asked Apr 14 '14 20:04

Buttle Butkus


People also ask

Where is the list of files modified in the last 30 days Linux?

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.

Where can I find file modified in the last 10 days?

/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.

How do I find last 10 modified files in Linux?

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.


1 Answers

A couple of issues

  • You're not limiting it to files, so when it finds a matching directory it will list every file within it.
  • You can't use > 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.

like image 76
Reinstate Monica Please Avatar answered Sep 17 '22 17:09

Reinstate Monica Please