IT TIP

주어진 시간보다 새로운 모든 파일을 재귀 적으로 찾습니다.

itqueen 2020. 11. 25. 21:50
반응형

주어진 시간보다 새로운 모든 파일을 재귀 적으로 찾습니다.


주어진 time_t :

⚡ date -ur 1312603983
Sat  6 Aug 2011 04:13:03 UTC

모든 파일을 최신으로 나열하는 bash 한 줄짜리를 찾고 있습니다. 비교시 시간대를 고려해야합니다.

같은 것

find . --newer 1312603983

그러나 파일 대신 time_t를 사용합니다.


이것은 touch원시 time_t값을 취하지 않기 때문에 약간 회로 적이지만 스크립트에서 작업을 꽤 안전하게 수행해야합니다. ( MacOS X에는 -rto 옵션 있습니다 date. 저는 GNU를 다시 확인하지 않았습니다.) 'time'변수는 touch명령 줄 에서 직접 명령 대체를 작성하여 피할 수 있습니다 .

time=$(date -r 1312603983 '+%Y%m%d%H%M.%S')
marker=/tmp/marker.$$
trap "rm -f $marker; exit 1" 0 1 2 3 13 15
touch -t $time $marker
find . -type f -newer $marker
rm -f $marker
trap 0

마지막 날에 생성 / 수정 된 모든 파일을 찾을 수 있습니다. 다음 예제를 사용하십시오.

find /directory -newermt $(date +%Y-%m-%d -d '1 day ago') -type f -print

지난주에 모든 것을 찾으려면 '1 주 전'또는 '7 일 전'을 사용하십시오.


누군가 그것을 사용할 수 있습니다. 특정 시간 프레임 내에 재귀 적으로 수정 된 모든 파일을 찾고 다음을 실행하십시오.

find . -type f -newermt "2013-06-01" \! -newermt "2013-06-20"

의 유닉스 타임 스탬프 (epoch 이후 초)가 주어지면 다음을 1494500000수행하십시오.

find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)"

"foo"에 대한 파일을 grep하려면 다음을 수행하십시오.

find . -type f -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d @1494500000)" -exec grep -H 'foo' '{}' \;

마커 파일 없이도이 작업을 수행 할 수 있습니다.

현재까지의 % s 형식은 에포크 이후 초입니다. find의 -mmin 플래그는 인수를 분 단위로 취하므로 초 단위의 차이를 60으로 나눕니다. age 앞에 "-"는 마지막 수정이 age 미만인 파일을 찾는 것을 의미합니다.

time=1312603983
now=$(date +'%s')
((age = (now - time) / 60))
find . -type f -mmin -$age

최신 버전의 gnu에서는 -newermt를 사용할 수 있습니다.


그래서 다른 방법이 있습니다 (그리고 어느 정도 이식 가능합니다 _

(python <<EOF
import fnmatch
import os
import os.path as path
import time

matches = []
def find(dirname=None, newerThan=3*24*3600, olderThan=None):
    for root, dirnames, filenames in os.walk(dirname or '.'):
        for filename in fnmatch.filter(filenames, '*'):
            filepath = os.path.join(root, filename)
            matches.append(path)
            ts_now = time.time()
            newer = ts_now - path.getmtime(filepath) < newerThan
            older = ts_now - path.getmtime(filepath) > newerThan
            if newerThan and newer or olderThan and older: print filepath
    for dirname in dirnames:
        if dirname not in ['.', '..']:
            print 'dir:', dirname
            find(dirname)
find('.')
EOF
) | xargs -I '{}' echo found file modified within 3 days '{}'

최신 릴리스를 가정하면 find -newermt강력합니다.

find -newermt '10 minutes ago' ## other units work too, see `Date input formats`

또는 time_t( epoch 이후 초) 를 지정하려는 경우 :

find -newermt @1568670245

For reference, -newermt is not directly listed in the man page for find. Instead, it is shown as -newerXY, where XY are placeholders for mt. Other replacements are legal, but not applicable for this solution.

From man find -newerXY:

Time specifications are interpreted as for the argument to the -d option of GNU date.

So the following are equivalent to the initial example:

find -newermt "$(date '+%Y-%m-%d %H:%M:%S' -d '10 minutes ago')" ## long form using 'date'
find -newermt "@$(date +%s -d '10 minutes ago')" ## short form using 'date' -- notice '@'

The date -d (and find -newermt) arguments are quite flexible, but the documentation is obscure. Here's one source that seems to be on point: Date input formats

참고URL : https://stackoverflow.com/questions/6964747/recursively-find-all-files-newer-than-a-given-time

반응형