Find Files (Strictly) Older than Another File

jwa

When I deploy my software, I ship a zipped file to the target server and extract it's contents. In addition to this, at the same time I also place a metadata file in the directory, detailing what was deployed.

If I want to find any files that have been changed since I deployed the software, I can simply find files that have a new modification time than the metadata file:

find . -newer deployment_metadata.txt

That's nice and straight-forward.

Now, I'd like to also find files that are old than the deployment metadata file. One would assume you could use the bang symbol to negate the "newer" check

find . ! -newer deployment_metadata.txt

But 'not newer' is not quite equivalent to 'older', as any files with the same timestamp are also "not newer" — so the command also includes all the files that I just deployed!

So, I was wondering if I was missing a trick when it comes to finding (strictly) old files?

My current solution is to create a new file (in the temp dir) using touch which has a modification time of one minute before the deployment_metadata.txt file. Then I am able to use the following arguments: ! -newer /var/tmp/metadtata_minus_1.

This works, but seems like a waste of time to have to create, and then clean up, the file in the temp dir - especially as different users may be using my script to check for this (don't want file ownership problems, so I actually go as far as appending ${USER} to the filename.

Daniel Andersson

One way is to (ab)use epoch time. Here is a test run where I first create seven files in sequence in an empty directory, where the c# files get "the same" ctime as far as find will be concerned:

$ for i in a b "c1 c2 c3" d e; do touch $i; sleep 1; done
$ find -newer c2
.
./d
./e
$ find -not -newer c2
./c3
./c2
./a
./b
./c1
$ find -newerct @$(($(stat -c %Z c2)-1))
.
./c3
./d
./c2
./e
./c1
$ find -not -newerct @$(($(stat -c %Z c2)-1))
./a
./b

This should represent all possible sets of ctime relative to c2:

  1. ctime > c2
  2. ctimec2
  3. ctimec2
  4. ctime < c2

with somewhat fuzzy matching, at least.

The third command gets epoch ctime for the file c2, subtracts 1 via shell arithmetic and feeds this as reference to -newerct (the @ is needed for find to interpret is as such a timestamp) to find all files with ctime newer than this interpreted timestamp (see -newerXY in man find). The fourth command negates this match, and should in practice do what you want if I've understood the question correctly, if you put your reference file as c2 in my example.

Note that the "1 second" offset is somewhat arbitrary (which is what I meant by "fuzzy matching"), and one could imagine a situation where a bug could be constructed. However, timestamps of files are not "definite" anyway and can not be trusted to be, so I can't imagine it to generate either security or practical problems in real situations.

Actually, in practice you might even want to increase the 1 second offset (I see in your question that you use 1 minute right now), but that is an implementation detail.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Find all files older than one minute

분류에서Dev

How to delete all files that match a pattern (or older than..) except the newest file that matches the pattern?

분류에서Dev

Find a list of files that contains a set of consecutive lines defined in another file

분류에서Dev

How to list files older than a day with "2015" in filename?

분류에서Dev

Powershell script to delete files older than x days, but not folders

분류에서Dev

Accidentally deleted all files older than 7 days

분류에서Dev

Script to find files less permissive than 750?

분류에서Dev

Deleting older log files

분류에서Dev

Find first number greater than x and return another value in that row

분류에서Dev

Files getting reverted to older version

분류에서Dev

Bash - Find - move files bigger than certain size

분류에서Dev

javascript unix timestamp older than x

분류에서Dev

Android JAVA $_FILE returms empty at files bigger than ~ 50 mb

분류에서Dev

ClearCase: Find all versions of a file newer than labelled version

분류에서Dev

Program in Java to read any file and find a substring, and replace it with another string

분류에서Dev

find only certain strings (domain) extract another file

분류에서Dev

How to find/detect hidden files inside JPEG file?

분류에서Dev

Batch file in windows 7 to find files not matching pattern

분류에서Dev

Compare two file listings to find identical files but ignore extensions

분류에서Dev

where I can find change log file in cakephp framework files?

분류에서Dev

Find the number of files that contains specific attributes assigned to more than one element

분류에서Dev

Validates if a person is older than 18 or have 18 based on birthday

분류에서Dev

How to check if database entry is older than 1 day in javascript

분류에서Dev

Why is the PPA version of LibreOffice-fresh older than the one on the site?

분류에서Dev

Find a string in a file and then find the first line above containing another string with bash

분류에서Dev

Make all files in a dir executable (non-recursively) while strictly-ensuring non-recursivness

분류에서Dev

Windows 7/8/10 cmd or powershell find lines in a txt file that are longer or smaller than X characters

분류에서Dev

use powershell to copy files and folders to archive folder older 30 minutes

분류에서Dev

Use just Month and Year in date strings, to find which combination is older

Related 관련 기사

  1. 1

    Find all files older than one minute

  2. 2

    How to delete all files that match a pattern (or older than..) except the newest file that matches the pattern?

  3. 3

    Find a list of files that contains a set of consecutive lines defined in another file

  4. 4

    How to list files older than a day with "2015" in filename?

  5. 5

    Powershell script to delete files older than x days, but not folders

  6. 6

    Accidentally deleted all files older than 7 days

  7. 7

    Script to find files less permissive than 750?

  8. 8

    Deleting older log files

  9. 9

    Find first number greater than x and return another value in that row

  10. 10

    Files getting reverted to older version

  11. 11

    Bash - Find - move files bigger than certain size

  12. 12

    javascript unix timestamp older than x

  13. 13

    Android JAVA $_FILE returms empty at files bigger than ~ 50 mb

  14. 14

    ClearCase: Find all versions of a file newer than labelled version

  15. 15

    Program in Java to read any file and find a substring, and replace it with another string

  16. 16

    find only certain strings (domain) extract another file

  17. 17

    How to find/detect hidden files inside JPEG file?

  18. 18

    Batch file in windows 7 to find files not matching pattern

  19. 19

    Compare two file listings to find identical files but ignore extensions

  20. 20

    where I can find change log file in cakephp framework files?

  21. 21

    Find the number of files that contains specific attributes assigned to more than one element

  22. 22

    Validates if a person is older than 18 or have 18 based on birthday

  23. 23

    How to check if database entry is older than 1 day in javascript

  24. 24

    Why is the PPA version of LibreOffice-fresh older than the one on the site?

  25. 25

    Find a string in a file and then find the first line above containing another string with bash

  26. 26

    Make all files in a dir executable (non-recursively) while strictly-ensuring non-recursivness

  27. 27

    Windows 7/8/10 cmd or powershell find lines in a txt file that are longer or smaller than X characters

  28. 28

    use powershell to copy files and folders to archive folder older 30 minutes

  29. 29

    Use just Month and Year in date strings, to find which combination is older

뜨겁다태그

보관