Programing

파일을 찾아 tar (공백 포함)

crosscheck 2020. 8. 7. 07:58
반응형

파일을 찾아 tar (공백 포함)


좋아요, 여기에 아주 간단한 문제입니다. 저는 간단한 백업 코드를 작성 중입니다. 파일에 공백이있는 경우를 제외하고는 정상적으로 작동합니다. 이것이 내가 파일을 찾고 tar 아카이브에 추가하는 방법입니다.

find . -type f | xargs tar -czvf backup.tar.gz 

문제는 tar가 파일이 폴더라고 생각하기 때문에 파일 이름에 공백이있는 경우입니다. 기본적으로 find 결과 주위에 따옴표를 추가 할 수있는 방법이 있습니까? 아니면 이것을 고치는 다른 방법?


이것을 사용하십시오 :

find . -type f -print0 | tar -czvf backup.tar.gz --null -T -

그것은 :

  • 공백, 개행, 선행 대시 및 기타 재미있는 파일 처리
  • 무제한의 파일 처리
  • 많은 수의 파일이있을 때 tar -cwith xargs사용 하는 것처럼 backup.tar.gz를 반복적으로 덮어 쓰지 않습니다.

참조 :


원하는 것을 달성하는 다른 방법이있을 수 있습니다. 원래,

  1. find 명령을 사용하여 찾고있는 파일의 경로를 출력합니다. stdout 을 선택한 파일 이름으로 리디렉션 합니다.
  2. 그런 다음 파일 위치 목록을 가져올 수있는 -T 옵션을 사용하여 tar (방금 find!로 만든 위치)

    find . -name "*.whatever" > yourListOfFiles
    tar -cvf yourfile.tar -T yourListOfFiles
    

달리기를 시도하십시오.

    find . -type f | xargs -d "\n" tar -czvf backup.tar.gz 

왜 안 되는가 :

tar czvf backup.tar.gz *

물론 find와 xargs를 사용하는 것이 영리하지만 어려운 방식으로 수행하고 있습니다.

업데이트 : Porges는 내 답변 또는 다른 답변보다 더 나은 답변이라고 생각하는 찾기 옵션으로 댓글을 달았습니다. find -print0 ... | xargs -0 ....


여러 파일 또는 디렉토리가 있고이를 독립적 인 *.gz파일 로 압축하려는 경우이 작업을 수행 할 수 있습니다. 선택 과목-type f -atime

find -name "httpd-log*.txt" -type f -mtime +1 -exec tar -vzcf {}.gz {} \;

이것은 압축됩니다

httpd-log01.txt
httpd-log02.txt

httpd-log01.txt.gz
httpd-log02.txt.gz

다음과 같은 것을 시도해보십시오. tar cvf scala.tar `find src -name *.scala`


여기에 표시된 또 다른 솔루션 :

find var/log/ -iname "anaconda.*" -exec tar -cvzf file.tar.gz {} +

가장 좋은 해결책은 다른 소스를 사용하고 목록으로 다른 작업을 수행 할 수 있으므로 파일 목록을 만든 다음 파일을 보관하는 것 같습니다.

예를 들어이 목록을 사용하여 아카이브되는 파일의 크기를 계산할 수 있습니다.

#!/bin/sh

backupFileName="backup-big-$(date +"%Y%m%d-%H%M")"
backupRoot="/var/www"
backupOutPath=""

archivePath=$backupOutPath$backupFileName.tar.gz
listOfFilesPath=$backupOutPath$backupFileName.filelist

#
# Make a list of files/directories to archive
#
echo "" > $listOfFilesPath
echo "${backupRoot}/uploads" >> $listOfFilesPath
echo "${backupRoot}/extra/user/data" >> $listOfFilesPath
find "${backupRoot}/drupal_root/sites/" -name "files" -type d >> $listOfFilesPath

#
# Size calculation
#
sizeForProgress=`
cat $listOfFilesPath | while read nextFile;do
    if [ ! -z "$nextFile" ]; then
        du -sb "$nextFile"
    fi
done | awk '{size+=$1} END {print size}'
`

#
# Archive with progress
#
## simple with dump of all files currently archived
#tar -czvf $archivePath -T $listOfFilesPath
## progress bar
sizeForShow=$(($sizeForProgress/1024/1024))
echo -e "\nRunning backup [source files are $sizeForShow MiB]\n"
tar -cPp -T $listOfFilesPath | pv -s $sizeForProgress | gzip > $archivePath

@Steve Kehlet 게시물에 댓글을 추가 할 수 있지만 50 명의 담당자 (RIP)가 필요합니다.

수많은 인터넷 검색을 통해이 게시물을 찾은 사람을 위해 특정 시간 범위에서 특정 파일을 찾을 수있을뿐만 아니라 tarring 오류를 일으킬 수있는 상대 경로 나 공백도 포함하지 않는 방법을 찾았습니다. (정말 STEVE 감사합니다.)

find . -name "*.pdf" -type f -mtime 0 -printf "%f\0" | tar -czvf /dir/zip.tar.gz --null -T -
  1. . 상대 디렉토리

  2. -name "*.pdf" PDF (또는 모든 파일 형식)를 찾습니다.

  3. -type f 찾을 유형은 파일입니다.

  4. -mtime 0 지난 24 시간 동안 생성 된 파일 찾기

  5. -printf "%f\0"정규 -print0OR -printf "%f"는 나를 위해 작동하지 않았습니다. man 페이지에서 :

This quoting is performed in the same way as for GNU ls. This is not the same quoting mechanism as the one used for -ls and -fls. If you are able to decide what format to use for the output of find then it is normally better to use '\0' as a terminator than to use newline, as file names can contain white space and newline characters.

  1. -czvf create archive, filter the archive through gzip , verbosely list files processed, archive name

Edit 2019-08-14: I would like to add, that I was also able to use essentially use the same command in my comment, just using tar itself:

tar -czvf /archiveDir/test.tar.gz --newer-mtime=0 --ignore-failed-read *.pdf

Needed --ignore-failed-read in-case there were no new PDFs for today.

참고URL : https://stackoverflow.com/questions/5891866/find-files-and-tar-them-with-spaces

반응형