월별로 파일을 그룹화하고 bash에 보관

월별로 파일을 그룹화하고 bash에 보관

내 디렉토리에는 여러 달에 생성된 파일이 많이 있습니다. 특정 달에 생성된 파일을 필터링하고 tar 기능을 사용하여 이러한 파일을 보관하고 싶습니다. 지금까지 시도한 코드는 다음과 같습니다.

months="Dec-2021"
decfiles=find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"
echo $decfiles
          

위 코드에서 다음과 같은 오류가 발생합니다.

files.sh: 3: .: Illegal option -n
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now

나는 이것을 알아낼 수 없는 것 같고 나는 또한 bash를 처음 접했습니다. 이 문제를 해결하는 데 도움을 주시면 대단히 감사하겠습니다.

답변1

당신은 이것에 익숙하지 않기 때문에 나는 당신에게 대답을 제공할 뿐만 아니라 이러한 유형의 문제를 해결하기 시작하는 방법을 설명하려고 노력할 것입니다.

먼저, 이러한 문제가 발견되면 이를 더 작은 명령으로 분리하고 격리해 보십시오.

예를 들어 다음과 같이 결과를 파일에 직접 저장하려고 시도하지 마세요.

decfiles=find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"

명령을 단독으로 실행해 보세요 find.

find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"

곧 다음 내용을 확인하실 수 있습니다.

실수 #1

find: I cannot figure out how to interpret ‘01-Dec-2021 +1 month-1 sec | tar cz decemberfiles.tar.gz’ as a date or time

실제로 읽으면 따옴표 안에 파이프를 실행하고 있음을 알 수 있습니다.

"01-$months +1 month-1 sec | tar cz decemberfiles.tar.gz"

시도 #2

이제 find 명령이 다음과 같아야 하는지 확인해야 합니다.

find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec"

이제 시작인 파일 및 디렉터리 목록이 표시됩니다. 그냥 인쇄하시는 걸 추천드려요문서, 디렉토리 대신 -type f.

find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f

이제 우리는 어딘가로 가고 있습니다.

다음으로 다음과 같은 것을 시도하고 싶다고 가정합니다.

find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |tar cz decemberfiles.tar.gz

그러면 다음이 발생합니다.

실수 #2

tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now

이는 옵션이 누락되었음을 의미 -f하므로 매뉴얼 페이지를 보고 아카이브를 제공하는 데 사용한 이름을 tar확인합니다 . -f ARCHIVE파일 이름을 제공했지만 아카이브라고 말하지 않았습니다. 이제 다음을 시도해 보세요.

시도 #3

find . -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |tar czf decemberfiles.tar.gz

실수 #3

이제 새로운 오류가 발생합니다.

tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.

빈 아카이브를 생성하려고 한다고 말하는 이유는 무엇입니까? 매뉴얼을 다시 보면 tar아래와 같이 파일 목록을 제공해야 함을 알 수 있습니다.논쟁:

tar -c [-f ARCHIVE] [OPTIONS] [FILE...]

tar 명령의 STDIN으로 파일을 파이프하려고 합니다. 따라서 다음과 같아야 합니다.

tar czf decemberfiles.tar.gz <file1> <file2> <file3> ...

이 명령은 파일 목록을 생성하는 중입니다 find. tar이 파일을 명령 에 어떻게 제공할 수 있나요? 몇 가지 옵션은 설명하지 않고 직접 읽어보도록 하겠습니다.

옵션 1
tar czf decemberfiles.tar.gz $(find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f)
옵션 #2
tar czf decemberfiles.tar.gz `find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f`

팁: 수행 중인 작업을 더 잘 이해하려면 명령 앞에 echo를 추가하여 어떻게 보이는지 확인해 보세요(여기서는 출력을 쓰지 않으므로 직접 찾아보시도록 하겠습니다).

echo tar czf decemberfiles.tar.gz `find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f`
옵션 #3

명령 에 익숙해지기 위한 마지막 옵션은 다음과 같습니다 xargs.

find ./ -newermt "01-$months -1 sec" -and -not -newermt "01-$months +1 month-1 sec" -type f |xargs tar czf decemberfiles.tar.gz

이제 최소한 저장한 내용이 있어야 시작됩니다. 지금쯤이면 아직 배울 것이 많다는 것을 알게 될 것입니다.

부품과 같이 극복해야 할 몇 가지 문제가 아직 남아 있습니다 decfiles=.... 더 작은 명령을 다시 실행하고 내용을 살펴보고 명령 출력을 bash의 변수에 할당하는 방법을 검색해 보세요. Google에서 검색할 수 있기 때문에 여기서 설명하지는 않겠습니다. 하지만 이제는 bash 학습을 시작하고 장애물을 단계별로 극복하는 방법에 대한 더 나은 아이디어를 가지셨기를 바랍니다.

관련 정보