|
Softpanorama |
May the source be with you, but remember the KISS principle ;-)
Softpanorama Search
|
Find permits selection of files based on Unix mtime, ctime, and atime attributes. The standard unit is 24 hour periods (a day). GNU find also permits using minutes for the period, for example:
find / -mmin -10
Standard predicates that work with age of the file in file are : "-atime/-ctime/-mtime" [+|-]n and they use 24 hour periods (a day). Each compares with value provided with the Unix timestamps: the last time a files's "access time", "file change time, to be more exact, the inode change time" and "content modification time".
n is time interval -- an integer with optional sign. It is measured in 24-hour periods (days) or minutes counted from the current moment.
Examples:
find $HOME -mtime -1
find $HOME -mtime -7
find
$HOME -mtime +365
find . -mtime -7 -name "*.html" -print
Note: If you use the number 7 (without a hyphen), find will match only html files that were modified exactly seven 24-hour periods (days) ago:
find . -mtime 7 -name "*.html" -print
find . -mtime +7 -name "*.html" -print
Unix keeps track of three timestamps. Of them atime is the simplest the non-controversial: it stands for access time which is when the file was last read.
It is important to understand the precise meaning of ctime and mtime timestamps. The most common misconception here is to view ctime as file "creation time". It is actually "change time". Here are more formal explanations:
For a given file ctime and
mtime can be different depending on if
you just modified the inode or the contents of the file (which updates ctime
as well). Commands like chown,
chmod, and
ln change only ctime.
Touch command change only mtime. For
example if you need to change the date to Jun 21, 2008 9AM to example.txt,
then you can go (-t parameter in touch
has format [[CC]YY]MMDDhhmm[.SS]):
touch -t 200907210900
example.txt
The second important thing to understand that unless -daystart option is used [Gnu find only], time in Unix find is measured in 24 hour periods (fractions are allowed in GNU find) from the current moment.
| Unless -daystart option is used [Gnu find only], time in Unix find is measured in 24 hour periods from the current moment |
Those 24 hours periods are usually called "days" but the definition of "days" used in find is different from common usage (calendar days are typically understood as 24 hour periods starting at midnight). The "day" in "find language" is interpreted as "24 hour periods starting from the current time". Here is how working with time ranges described in GNU find documentation (Finding Files)
2.3.1 Age Ranges
These tests are mainly useful with ranges (+n and -n).
Test: -atime n
Test: -ctime n
Test: -mtime n Test: -amin nTrue if the file was last accessed (or its status changed, or it was modified) n*24 hours ago. The number of 24-hour periods since the file's timestamp is always rounded down; therefore 0 means less than 24 hours ago, 1 means between 24 and 48 hours ago, and so forth. Fractional values are supported but this only really makes sense for the case where ranges (+n and -n) are used.
Test: -cmin n
Test: -mmin n Option: -daystartTrue if the file was last accessed (or its status changed, or it was modified) n minutes ago. These tests provide finer granularity of measurement than -atime et al., but rounding is done in a similar way (again, fractions are supported). For example, to list files in /u/bill that were last read from 2 to 6 minutes ago:
find /u/bill -amin +2 -amin -6Measure times from the beginning of today rather than from 24 hours ago. So, to list the regular files in your home directory that were modified yesterday, do
find ~/ -daystart -type f -mtime 1The -daystart option is unlike most other options in that it has an effect on the way that other tests are performed. The affected tests are -amin, -cmin, -mmin, -atime, -ctime and -mtime. The -daystart option only affects the behavior of any tests which appear after it on the command line.
Previous: Age Ranges, Up: Time2.3.2 Comparing Timestamps
Test: -newerXY reference
Succeeds if timestamp X of the file being considered is newer than timestamp Y of the file reference. The letters X and Y can be any of the following letters:
- a
- Last-access time of reference
- B
- Birth time of reference (when this is not known, the test cannot succeed)
- c
- Last-change time of reference
- m
- Last-modification time of reference
- t
- The reference argument is interpreted as a literal time, rather than the name of a file. See Date input formats, for a description of how the timestamp is understood. Tests of the form -newerXt are valid but tests of the form -newertY are not.
For example the test
-newerac /tmp/foosucceeds for all files which have been accessed more recently than /tmp/foo was changed. Here X is a and Y is c.Not all files have a known birth time. If Y is b and the birth time of reference is not available,
findexits with an explanatory error message. If X is b and we do not know the birth time the file currently being considered, the test simply fails (that is, it behaves like-falsedoes).Some operating systems (for example, most implementations of Unix) do not support file birth times. Some others, for example NetBSD-3.1, do. Even on operating systems which support file birth times, the information may not be available for specific files. For example, under NetBSD, file birth times are supported on UFS2 file systems, but not UFS1 file systems.
There are two ways to list files in /usr modified after February 1 of the current year. One uses -newermt:
find /usr -newermt "Feb 1"The other way of doing this works on the versions of find before 4.3.3:
touch -t 02010000 /tmp/stamp$$ find /usr -newer /tmp/stamp$$ rm -f /tmp/stamp$$ Test: -anewer file
Test: -cnewer file
Test: -newer file Test: -used nTrue if the file was last accessed (or its status changed, or it was modified) more recently than file was modified. These tests are affected by -follow only if -follow comes before them on the command line. See Symbolic Links, for more information on -follow. As an example, to list any files modified since /bin/sh was last modified:
find . -newer /bin/shTrue if the file was last accessed n days after its status was last changed. Useful for finding files that are not being used, and could perhaps be archived or removed to save disk space.
Expressions can be use to select files created or modified during contain intervals, for example files that are at least one week old (7 days) but less then 30 days old. You can combine the predicates like this:
find . -mtime +30 -a -mtime -7 -print0
Note: If you use parameters with find command in scripts be careful when -mtime parameter is equal zero ( -mtime +0 ). Earlier versions of GNU find incorrectly interpret the following expression:
find -mtime +0 -mtime -1which should be equivalent to
find -mtime -1but does not produce any files...
Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
Disclaimer:
Created: May 16, 1997; Last modified: August 25, 2009