지난 n 초 동안 변경된 파일을 찾기위한 Linux 명령 파일을 찾기 위해 Linux 명령을 원합니다 . 명령

지난 n몇 초 동안 변경된 파일을 찾기 위해 Linux 명령을 원합니다 .

명령 행 인터페이스 또는 GUI에서 실행할 수있는 쉘 스크립트 또는 기타 도구가 있습니까?



답변

다음과 같이 find 명령을 사용하십시오.

find . -name "*.txt" -mtime -60s

*.txt지난 60 초 동안 수정 된 모든 파일 을 찾습니다 .


답변

mtime으로 초를 지정하는 솔루션은find --version == 사용하는 Linux 시스템에서 작동하지 않습니다 find (GNU findutils) 4.4.2.

다음과 같은 오류가 발생합니다.

mycomputer:~/new$ find . -mtime -60s
find: missing argument to `-mtime'
mycomputer:~/new$ find . -mtime -60seconds
find: missing argument to `-mtime'

그러나 -mmin(마지막 m 분 동안 수정)을 사용할 수 있으며 십진 인수를 취할 수 있습니다. 예를 들어, 다음은 지난 30 초 동안 수정 된 파일을 찾습니다.

find . -mmin 0.5

예를 들어; 지난 120 초 동안 마지막으로 수정 된 1, 6, 11, … 파일을 작성하면이 명령은 다음을 찾습니다.

mycomputer:~/new$ for i in $(seq 1 5 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
mycomputer:~/new$ find . -mmin 0.5
.
./last_modified_1_seconds_ago
./last_modified_26_seconds_ago
./last_modified_11_seconds_ago
./last_modified_16_seconds_ago
./last_modified_21_seconds_ago
./last_modified_6_seconds_ago

따라서 몇 초 안에 실제로 필요한 경우 다음과 같이 할 수 있습니다.

localhost:~/new$ for i in $(seq 1 1 120); do touch -d "-$i seconds" last_modified_${i}_seconds_ago ; done
localhost:~/new$ N=18; find . -mmin $(echo "$N/60"|bc -l)
./last_modified_1_seconds_ago
./last_modified_9_seconds_ago
./last_modified_14_seconds_ago
./last_modified_4_seconds_ago
./last_modified_12_seconds_ago
./last_modified_13_seconds_ago
./last_modified_8_seconds_ago
./last_modified_3_seconds_ago
./last_modified_5_seconds_ago
./last_modified_11_seconds_ago
./last_modified_17_seconds_ago
./last_modified_16_seconds_ago
./last_modified_7_seconds_ago
./last_modified_15_seconds_ago
./last_modified_10_seconds_ago
./last_modified_6_seconds_ago
./last_modified_2_seconds_ago


답변

glenn이 제안한 것과 유사하게 설치 프로그램 프로세스가 실행되는 시간에 수정 된 모든 것을 찾으려면 다음과 같은 작업을 수행하는 것이 더 쉬울 수 있습니다.

touch /tmp/checkpoint
<do installer stuff>
find / -newer /tmp/checkpoint

그런 다음 시간 계산을 수행 할 필요가 없습니다. 검사 점 파일 다음에 변경된 사항 만 발견하면됩니다.


답변

지원하지 않는 find 버전이 있다면 -mtime -60s더 나은 해결책은

touch -d '-60 seconds' /tmp/newerthan
find . -name "*.txt" -newer /tmp/newerthan


답변

이를 수행하는 가장 간단한 방법은 다음과 같습니다.

find . -name "*.txt" -newermt '6 seconds ago'

-mtime -60s의 많은 버전에서 작동하지 않습니다 답변에서 언급 한 옵션, find심지어 2016 년은 -newermt우리에게 더 나은 옵션입니다. 다양한 날짜 및 시간 형식을 구문 분석 할 수 있습니다.

사용하는 다른 방법 mmin은 다음과 같습니다.

find . -name "*.txt" -mmin -0.5

# Finds files modified within the last 0.5 minute, i.e. last 30 seconds

이 옵션은 모든 find버전에서 작동하지 않을 수 있습니다 .


답변

파일 변경 사항에 대한 디렉토리를 모니터링하는 경우 무한 폴링 루프 대신 inotify-tools 를 사용하고 싶을 것입니다 .


답변

내 버전 find과 같이 초 또는 실제 값을 허용하지 않는 경우을 사용 -mmin하고 0을 지정하면 1 분 안에 모든 파일이 수정됩니다.

$ touch test; find . -type f -mmin 0
./test