Thursday, July 7, 2016

Find Files Based on Access / Modification / Change Time

The basic syntax of the find command is:

find path options

where path is the path to search and options are the options given to find command.
You can find files based on following three file time attribute.

    • -amin  when the file was accessed in minutes
    • -atime when the file was accessed in days
    • -cmin when the file was created in minutes
    • -ctime when the file was created in days
    • -mmin when the file was modified in minutes
In all our below examples, the path is our current directory and hence we use .(dot).

1. To find files modified in the last 5 days:
find . -mtime -5
 
2. To find files modified before 5 days:
find . -mtime +5

 Note: Developers, be aware. + is not default in find. If you omit the '+', it has a different meaning. It means to find files modified exactly before 5 days.
3. To find files modified in the last 40mins:
find . -mmin -40 


4. To find files modified before 40mins:
find . -mmin +40

 
The above commands will find both files and directories modifying the criteria. 
 
If you want to find only files, use the -type option.
find . -type f -mmin -40
 
This will find only the files modified in the last 40 mins, not directories.
 
5. Find files whose content got updated within last 1 hour


# find . -mmin -60
In the same way, following example finds all the files (under root file system /) that got updated within the last 24 hours (1 day).

# find / -mtime -1
Example 2: Find files which got accessed before 1 hour
# find -amin -60
 
In the same way, following example finds all the files (under root file system /) that got accessed within the last 24 hours (1 day).


# find / -atime -1
Example 3: Find files which got changed exactly before 1 hour
# find . -cmin -60
 
In the same way, following example finds all the files (under root file system /) that got changed within the last 24 hours (1 day).


# find / -ctime -1
Example 9: ls -l in find command output. Long list the files which are edited within the last 1 hour.
# find -mmin -60


No comments: