Last active
March 9, 2025 01:49
-
-
Save kuluna/1bf403bd720a4bba5a94f1f69a4f1fb2 to your computer and use it in GitHub Desktop.
指定したディレクトリ内の特定の拡張子のみを圧縮
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# 引数チェック | |
if [ $# -lt 2 ]; then | |
echo "使用方法: $0 <対象ディレクトリ> <拡張子>" | |
exit 1 | |
fi | |
# 圧縮対象のディレクトリ(引数で指定) | |
TARGET_DIR="$1" | |
# 圧縮対象の拡張子 | |
EXTENSION="${2:-*}" | |
# 指定した拡張子のファイルを検索して個別に圧縮 | |
find "$TARGET_DIR" -type f -name "*.$EXTENSION" | while read FILE; do | |
# ファイルのあるディレクトリ | |
FILE_DIR=$(dirname "$FILE") | |
# 元のファイル名を取得(拡張子を含む) | |
BASENAME=$(basename "$FILE") | |
# ZIPファイル名(元のファイル名 + .zip) | |
ZIP_NAME="${FILE_DIR}/${BASENAME}.zip" | |
# ファイルを個別にZIP圧縮(元のファイル名をZIPのルートにする) | |
zip -j "$ZIP_NAME" "$FILE" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment