安装软件

sudo apt-get update
sudo apt-get install parallel
sudo apt-get install pigz

建立一个可执行文件,如compress_dirs.sh

#!/bin/bash

# Function to compress a directory using pigz
compress_dir() {
    local dir=$1
    tar -cf - "$dir" | pigz > "${dir}.tar.gz"
}

export -f compress_dir

# Check if at least one argument (the root directory) is provided
if [ "$#" -lt 1 ]; then
    echo "Usage: $0 root_directory [exclude_folder1 exclude_folder2 ...]"
    exit 1
fi

# Get the root directory
root_directory=$1
shift

# Get the folders to exclude
exclude_folders=("$@")

# Build the find command with exclusion criteria
find_command="find \"$root_directory\" -maxdepth 1 -type d -not -path '$root_directory'"
for folder in "${exclude_folders[@]}"; do
    find_command+=" -not -name \"$(basename "$folder")\""
done

# Execute the find command and compress the directories using parallel with progress bar and increased parallelism
eval "$find_command" | parallel --bar compress_dir

使用方法

./compress_dirs.sh /path/of/root/need/compress/dirs /path/of/exec/dir1 /path/of/exec/dir2 ...

例子

~/Documents/test$ ls
1  2  3
~/Documents/test$ ~/Documents/compress_dirs_2.sh . 1
~/Documents/test$ ls
1  2  2.tar.gz  3  3.tar.gz