데이터베이스를 백업 할 폴더가 있고 오전 11시에서 오후 3시 사이에 생성 된 모든 백업을 삭제하고 싶습니다. 요일이 무엇이든 상관 없습니다.
이 명령이 매우 도움이되는 것으로 나타 났지만 사용 사례에서는 그렇지 않습니다.
find . -type f -newermt '01 nov 2018 00:00:00' -not -newermt '10 nov 2018 00:00:00' -delete
그러나 여기에서는 두 날짜 사이의 간격을 강요합니다! 특정 두 시간 사이에 생성 된 백업 만 삭제하고 싶습니다.
간단히 말해서 linux 태그를 지정 했다면 stat
파일의 수정 시간을 추출하는 date
명령 과 주어진 시간에서 시간을 추출하는 GNU 명령을 사용할 수 있습니다.
find . -type f -exec sh -c '
h=$(date -d @$(stat -c %Y "$1") +%-H); [ "$h" -ge 11 ] && [ "$h" -lt 15 ]' \
sh {} \; -ls
결과가 올 바르면 다음을 수행하십시오.
find . -type f -exec sh -c '
h=$(date -d @$(stat -c %Y "$1") +%-H); [ "$h" -ge 11 ] && [ "$h" -lt 15 ]' \
sh {} \; -delete
다음은 -ls
버전을 사용한 테스트 실행입니다 .
$ touch -d 'Wed Sep 12 11:00:01 EDT 2018' 11am
$ touch -d 'Wed Sep 12 12:00:02 EDT 2018' 12pm
$ touch -d 'Wed Sep 12 15:00:03 EDT 2018' 303pm
$ find . -type f -exec sh -c 'h=$(date -d @$(stat -c %Y "$1") +%-H); [ "$h" -ge 11 ] && [ "$h" -lt 15 ]' sh {} \; -ls
1705096 0 -rw-r--r-- 1 user group 0 Sep 12 2018 ./11am
1705097 0 -rw-r--r-- 1 user group 0 Sep 12 2018 ./12pm
내가 따랐던 훌륭한 답변을 작성한 Kusalananda에게 감사드립니다. 'find'의 -exec 옵션 이해
우리가주의 하지 원하는 {} +
버전을 find
우리가 원하는대로, 여기 -exec
결과는 당 파일 수, 그래서 시간 범위를 일치 우리 만 삭제 파일.
포함 된 셸 스크립트에는 두 가지 주요 부분이 있습니다. 파일의 "시간"타임 스탬프를 확인한 다음 범위에 따라 성공 또는 실패를 반환합니다. 첫 번째 부분은 그 자체로 두 부분으로 이루어집니다. 변수에는 명령 대체 결과가 할당됩니다. 명령은 완전히 읽어야합니다.
$(stat -c %Y "$1")
-- this (second) command-substitution calls stat
on the $1
parameter of the embedded shell script; $1
was assigned by find
as one of the pathnames it found. The %Y option to the stat
command returns the modification time in seconds-since-the-epoch.date -d @ ... +%-H
-- this takes the seconds-since-the-epoch from the above command substitution and asks date
to give us the Hours portion of that time; the @
syntax tells date
that we're giving it seconds-since-the-epoch as an input format. With the -
option in the date output format, we tell GNU date to not pad the value with any leading zeroes. This prevents any octal misinterpretation later.Once we have the $h
Hour variable assigned, we use bash's conditional operator [[
to ask whether that value is greater-than-or-equal to 11 and also strictly less than 15.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다