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.
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
:
ctime
> c2
ctime
≤ c2
ctime
≥ c2
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] 삭제
몇 마디 만하겠습니다