#!/bin/bash
# Where to store Tars? We don't want in Images directory
COMPRESSED_DIR="$HOME/Downloads"
# TARs created with name below + "_999.tar"
TAR_NAME="CompressedImages"
# Directory Images are located in
FILES="$HOME/Pictures/*"
FileCount=50 # Number of files compressed into one Tar
TarCount=0 # Count of Tars created
i=0
for f in $FILES
do
if [[ $(( i % FileCount )) -eq 0 ]] ; then
let TarCount++
TarName="$COMPRESSED_DIR/$TAR_NAME$TarCount.tar"
tar -cvpf "$TarName" "$f" # create .tar & add first file
else
tar -rvpf "$TarName" "$f" # Add to existing tar
fi
let i++
done
运行CompressImages脚本后检查结果:
$ ll -h ~/Downloads/*.tar
-rw-rw-r-- 1 rick rick 14M Jun 18 16:55 Downloads/CompressedImages10.tar
-rw-rw-r-- 1 rick rick 48M Jun 18 16:55 Downloads/CompressedImages11.tar
-rw-rw-r-- 1 rick rick 16M Jun 18 16:55 Downloads/CompressedImages12.tar
-rw-rw-r-- 1 rick rick 55M Jun 18 16:55 Downloads/CompressedImages13.tar
-rw-rw-r-- 1 rick rick 45M Jun 18 16:55 Downloads/CompressedImages14.tar
-rw-rw-r-- 1 rick rick 43M Jun 18 16:55 Downloads/CompressedImages15.tar
-rw-rw-r-- 1 rick rick 37M Jun 18 16:55 Downloads/CompressedImages16.tar
-rw-rw-r-- 1 rick rick 38M Jun 18 16:55 Downloads/CompressedImages17.tar
-rw-rw-r-- 1 rick rick 44M Jun 18 16:55 Downloads/CompressedImages18.tar
-rw-rw-r-- 1 rick rick 47M Jun 18 16:55 Downloads/CompressedImages19.tar
-rw-rw-r-- 1 rick rick 180M Jun 18 16:55 Downloads/CompressedImages1.tar
-rw-rw-r-- 1 rick rick 53M Jun 18 16:55 Downloads/CompressedImages20.tar
-rw-rw-r-- 1 rick rick 33M Jun 18 16:55 Downloads/CompressedImages21.tar
-rw-rw-r-- 1 rick rick 13M Jun 18 16:55 Downloads/CompressedImages22.tar
-rw-rw-r-- 1 rick rick 26M Jun 18 16:55 Downloads/CompressedImages23.tar
-rw-rw-r-- 1 rick rick 1.4M Jun 18 16:55 Downloads/CompressedImages24.tar
-rw-rw-r-- 1 rick rick 13M Jun 18 16:55 Downloads/CompressedImages2.tar
-rw-rw-r-- 1 rick rick 15M Jun 18 16:55 Downloads/CompressedImages3.tar
-rw-rw-r-- 1 rick rick 23M Jun 18 16:55 Downloads/CompressedImages4.tar
-rw-rw-r-- 1 rick rick 11M Jun 18 16:55 Downloads/CompressedImages5.tar
-rw-rw-r-- 1 rick rick 12M Jun 18 16:55 Downloads/CompressedImages6.tar
-rw-rw-r-- 1 rick rick 20M Jun 18 16:55 Downloads/CompressedImages7.tar
-rw-rw-r-- 1 rick rick 7.4M Jun 18 16:55 Downloads/CompressedImages8.tar
-rw-rw-r-- 1 rick rick 21M Jun 18 16:55 Downloads/CompressedImages9.tar
假设您的图像存储在
~/Pictures
其中/home/USER_ID/Pictures
. 将以下命令放在您的主目录中名为CompressImages
. 使用以下命令使文件可执行:使用以下命令调用脚本:
将以下脚本复制到文件中
~/CompressImages
:运行
CompressImages
脚本后检查结果:您可以使用
parallel -l 50
:-iname "*.jpg"
如果还有其他文件,请添加或类似 find 命令。如果您的图像已经被压缩(例如,就是这种情况),请忽略
z
选项tar
并命名它。.tar
tar.gz
jpg
说明:
find -print0
发送空分隔的数据parallel -0
读取空分隔的数据-l 50
每次执行读取 50 行-j 1
并行执行一项任务,您可以将其更改为最适合您的任务(取决于您拥有的 CPU 线程数和 I/O 速度)。{#}
将替换为序列号 (0-n){}
将被 50 个文件名替换。然后,您可以移动/重命名
*.tar.gz
或*.tar
根据自己的喜好。这比调用
tar
将每个文件添加到现有 tar 中要快得多。