UNIX find: opposite of -newer option exists?
If you only need files that are older than file "foo" and not foo itself, exclude the file by name using negation:
find . ! -newer foo ! -name foo
You can just use negation:
find ... \! -newer <reference>
You might also try the -mtime
/-atime
/-ctime
/-Btime
family of options. I don't immediately remember how they work, but they might be useful in this situation.
Beware of deleting files from a find
operation, especially one running as root; there are a whole bunch of ways an unprivileged, malicious process on the same system can trick it into deleting things you didn't want deleted. I strongly recommend you read the entire "Deleting Files" section of the GNU find manual.
You can use a !
to negate the -newer operation like this:
find . \! -newer filename
If you want to find files that were last modified more then 7 days ago use:
find . -mtime +7
UPDATE:
To avoid matching on the file you are comparing against use the following:
find . \! -newer filename \! -samefile filename
UPDATE2 (several years later):
The following is more complicated, but does do a strictly older than match. It uses -exec
and test -ot
to test each file against the comparison file. The second -exec
is only executed if the first one (the test
) succeeds. Remove the echo
to actually remove the files.
find . -type f -exec test '{}' -ot filename \; -a -exec echo rm -f '{}' +