
오전 내내 시도한 후에도 여전히 알 수 없습니다. 아마도 당신도 알고 있을 것이다.
많은 디렉토리를 zip 파일로 압축하여 각 디렉토리가 자체 zip 파일이 되도록 하고 싶습니다. 폴더에 1000개가 넘는 파일이 있는 경우 각각 1000개 파일로 구성된 여러 개의 zip 파일로 분할하세요.
보관할 때 최대 파일 수를 설정하고 그에 따라 분할하는 것이 가능합니까? 각 파일을 독립적으로 사용할 수 있도록 많은 파일을 관리 가능한 덩어리로 압축하려고 합니다.
내가 지금 가지고 있는 해결책은 다음과 같습니다.
# make each folder into its own archive
for i in */; do zip -r "${i%/}.zip" "$i"; done
# make archives of 500 files per piece
find . ! -name '*.zip' -type f | xargs -n 500 | awk '{system("zip myarch"NR".zip "$0)}'
원천:파일 수가 제한된 Zip 아카이브그리고여러 디렉터리를 별도의 zip 파일로 압축하는 명령
내가 놓치고 있는 것은 find의 파일을 해당 파일이 발견된 하위 디렉터리로 제한하는 방법입니다. 500개의 파일이 있으면 이 500개의 파일만 디렉터리에 넣어야 합니다. 2400개의 파일이 있으면 3개의 파일로 나누어야 하며, 그 중 처음 두 개는 1000개의 파일을 포함하고 마지막 파일은 포함해야 합니다. 파일은 400개에 불과합니다.
나는 이런 맥락에서 뭔가가 효과가 있어야 한다고 생각합니다. 하지만 여전히 뭔가 잘못되었습니다.
#for all subfolders, find all files, take them in 500 name chunks, and zip them up into numbered archives.
for i in */; do find "${i%/}" -type f | xargs -n 500| awk '{system("zip ${i%/}"NR".zip "$0)}'; done
도움을 주셔서 감사합니다. 감사합니다!
고쳐 쓰다. 이 bash 스크립트가 작동해야 한다고 생각하지만 왜 zip 파일이 여전히 acro인지 이해가 안 됩니다.
#!/bin/bash
for i in */; do
printf $i
find "${i%/}" -type f | xargs -n 500 | awk '{system("zip marych${i%/}"NR".zip "$0)}';
업데이트 2:
수리하다. 일부 인용 문제가 문제입니다.
for i in */; do find "${i%/}" -type f| xargs -n 500 | awk '{system("zip '${i%/}'"NR".zip "$0)}'; done
done
답변1
저작권 제임스 다니엘 마스 리치(James Daniel Mars Rich). 이 자료는 '아카이브에 최대 1000개의 파일이 포함된 여러 디렉터리를 압축합니다.', 하지만 '에서도 얻을 수 있습니다.https://snippetly.blogspot.com/2019/12/zip-files-recursively-from-directories.html' 다음 라이선스의 조건에 따라: Comprehensive Open License 3.0(https://jamesdanielmarrsritchey.blogspot.com/2019/06/compressive-open-license-30.html), MIT(https://opensource.org/licenses/MIT).
PHP를 사용하여 zip 파일을 만들 수 있습니다. 여기서 각 zip 파일에는 폴더당 지정된 수의 파일이 포함됩니다. 먼저 처리해야 하는 모든 디렉터리가 포함된 배열을 가져옵니다. 다음으로 각 디렉터리를 반복하여 파일 배열을 만듭니다. 이제 각 배열 값에 X 파일이 포함된 이러한 파일의 새 배열을 만듭니다. 그런 다음 7zip을 사용하여 배열을 반복하고 7zip이 이러한 모든 파일을 압축할 수 있도록 값을 전달합니다.
아래 코드는 zip당 5개의 파일을 설정합니다. 원하는 번호로 변경할 수 있습니다. p7zip, find 및 php7을 설치해야 합니다.
암호:
<?php
$top_directory = '/home/user1/files_to_zip';
$destination = '/home/user1/zips';
#Get a list of folders, including the top directory
$dirs = shell_exec("find $top_directory -type d");
#Deal with each directory separately
$dirs = explode("\n", $dirs);
$dirs = array_filter($dirs);
foreach ($dirs as $dir){
$zip_name = basename($dir);
#Get a list of files within one directory
$files = shell_exec("find $dir -maxdepth 1 -type f");
#Split list of files for one directory into array
$files = explode("\n", $files);
$files = array_filter($files);
if (empty($files) === TRUE){
goto end;
}
#Create a new array which holds 5 files per value (each value should be formatted as expected by the ZIP utility for a list of files)
$n = 0;
$list = array('');
foreach ($files as $file){
$n++;
if ($n <= 5){
$keys = array_keys($list);
$key = end($keys);
$list[$key] = $list[$key] . " " . $file;
} else {
$n = 1;
$list[] = '';
$keys = array_keys($list);
$key = end($keys);
$list[$key] = $list[$key] . " " . $file;
}
}
#Create the zip archives of files for the one directory
$n = 0;
foreach ($list as $value){
$n++;
$zip_name_2 = "{$zip_name}_{$n}.zip";
shell_exec("cd $destination && 7z a $zip_name_2 $value");
}
end:
}
?>
답변2
나는 이것을 위해 tar를 사용합니다. 많은 디렉토리가 있는 폴더가 있을 때마다 tar -czvf filename.tar 디렉토리를 실행하면 모든 폴더가 단일 tar 파일로 압축됩니다.
바라보다:https://www.howtogeek.com/248780/how-to-compress-and-extract-files-using-the-tar-command-on-linux/