Skip to content

Instantly share code, notes, and snippets.

@shiumachi
Created December 14, 2024 22:25
Show Gist options
  • Save shiumachi/a9aaf3e0647278355d5c98b961f411ee to your computer and use it in GitHub Desktop.
Save shiumachi/a9aaf3e0647278355d5c98b961f411ee to your computer and use it in GitHub Desktop.
txtzipper: A simple command-line tool to zip text files in a directory.

txtzipper

A simple command-line tool to zip text files in a directory.

Overview

txtzipper is a shell script that finds all .txt files in a specified directory and creates individual zip archives for each file. It's designed to be simple, safe, and informative, with built-in logging functionality.

Features

  • Recursively processes all .txt files in the specified directory
  • Creates individual zip files with naming pattern: originalfile.zip
  • Skips existing zip files to prevent overwrites
  • Provides detailed logging of all operations
  • Handles files with spaces in their names
  • Performs directory permission checks
  • Creates detailed process logs

License

MIT

#!/bin/bash
# 使用方法を表示する関数
usage() {
echo "Usage: $0 directory"
echo " directory: Directory containing .txt files to be zipped"
exit 1
}
# エラーハンドリング関数
error_exit() {
echo "ERROR: $1" >&2
exit 1
}
# ログ出力関数
log_message() {
local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
echo "[${timestamp}] $1" | tee -a "$LOG_FILE"
}
# 引数チェック
if [ $# -ne 1 ]; then
usage
fi
DIRECTORY="$1"
# ディレクトリの存在チェック
if [ ! -d "$DIRECTORY" ]; then
error_exit "Directory '$DIRECTORY' does not exist"
fi
# ディレクトリのアクセス権チェック
if [ ! -r "$DIRECTORY" ] || [ ! -w "$DIRECTORY" ]; then
error_exit "Directory '$DIRECTORY' is not accessible (read/write permissions required)"
fi
# ログファイルの設定
LOG_FILE="${DIRECTORY}/zip_process.log"
log_message "Starting zip process"
log_message "Working directory: $DIRECTORY"
# zipコマンドの存在確認
if ! command -v zip >/dev/null 2>&1; then
error_exit "zip command not found. Please install zip package."
fi
# 作業ディレクトリに移動
cd "$DIRECTORY" || error_exit "Cannot change to directory '$DIRECTORY'"
# txtファイルの処理
find . -name "*.txt" -type f | while read -r file; do
# ファイルの存在と読み取り権限をチェック
if [ ! -r "$file" ]; then
log_message "ERROR: Cannot read file '$file' (permission denied)"
continue
fi
# ファイルサイズをチェック
file_size=$(stat -f %z "$file" 2>/dev/null || stat -c %s "$file" 2>/dev/null)
if [ "$?" -ne 0 ]; then
log_message "ERROR: Cannot get file size for '$file'"
continue
fi
log_message "Processing file: $file (size: $file_size bytes)"
# 拡張子を除いたファイル名を取得して.zipを付加
zip_file="${file%.txt}.zip"
# 既存のzipファイルチェック
if [ -f "$zip_file" ]; then
log_message "SKIP: Zip file already exists for '$file'"
continue
fi
# zip圧縮の実行(詳細なエラー出力を取得)
zip_output=$(zip -v "$zip_file" "$file" 2>&1)
if [ "$?" -eq 0 ]; then
compression_ratio=$(echo "$zip_output" | grep "compression ratio" | tail -n 1)
log_message "SUCCESS: Zipped '$file' to '$zip_file' ($compression_ratio)"
else
log_message "ERROR: Failed to zip '$file' to '$zip_file'"
log_message "Error details: $zip_output"
# ファイルシステムの空き容量チェック
df_output=$(df -h . 2>&1)
log_message "Filesystem status:\n$df_output"
# ファイルの文字エンコーディングチェック
file_encoding=$(file -i "$file" 2>&1)
log_message "File encoding: $file_encoding"
fi
done
# 処理結果の要約
total_files=$(find . -name "*.txt" -type f | wc -l)
total_zipped=$(find . -name "*.zip" -type f | wc -l)
log_message "Process completed. Total files: $total_files, Successfully zipped: $total_zipped"
# システム情報の記録
log_message "System information:"
log_message "OS: $(uname -a)"
log_message "Disk space:"
df -h . | tee -a "$LOG_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment