📝

短編:普段使ってる自作コマンド達を紹介

に公開1

今回は普段ターミナルで作業するときに活用している自作コマンド達を紹介しようと思います。なお、ここで定義しているコマンド達は一部claude codeなどの生成AIによって生成したものも含まれます。

自作コマンドの定義

自作コマンドは~/.config/zsh/hogehoge.zshの形で設定し、~/.zshrcでまとめて読み込むように書いています。

コマンド達

ffmpeg編

動画像を仕事で扱う時は時折ffmpegを利用していて、その中でよく利用するコマンドを別途定義しています。使い方は中盤-h/--helpの説明を参照してください。

  • 動画を一定間隔で分割する
function ffmpeg_segment(){
    local input_file=""
    local segment_time="10:00"
    local output_format="%d.mp4"
    local threads=4
    
    while [[ $# -gt 0 ]]; do
        case $1 in
            -i|--input)
                input_file="$2"
                shift 2
                ;;
            -t|--time)
                segment_time="$2"
                shift 2
                ;;
            -o|--output)
                output_format="$2"
                shift 2
                ;;
            --threads)
                threads="$2"
                shift 2
                ;;
            -h|--help)
                echo "Usage: ffmpeg_segment -i <input_file> [-t <segment_time>] [-o <output_format>] [--threads <num>]"
                echo ""
                echo "Options:"
                echo "  -i, --input     Input video file (required)"
                echo "  -t, --time      Segment duration (default: 10:00)"
                echo "  -o, --output    Output filename format (default: %d.mp4)"
                echo "  --threads       Number of threads (default: 4)"
                echo ""
                echo "Examples:"
                echo "  # 10分ごとに分割(デフォルト)"
                echo "  ffmpeg_segment -i movie.mp4"
                echo ""
                echo "  # 5分ごとに分割"
                echo "  ffmpeg_segment -i movie.mp4 -t 5:00"
                echo ""
                echo "  # 30秒ごとに分割、カスタム出力名"
                echo "  ffmpeg_segment -i movie.mp4 -t 30 -o \"segment_%03d.mp4\""
                echo ""
                echo "  # 1時間ごとに分割、8スレッド使用"
                echo "  ffmpeg_segment -i movie.mp4 -t 1:00:00 --threads 8"
                return 0
                ;;
            *)
                echo "Unknown option: $1"
                return 1
                ;;
        esac
    done
    
    if [[ -z "$input_file" ]]; then
        echo "Error: Input file is required"
        echo "Use -h or --help for usage information"
        return 1
    fi
    
    if [[ ! -f "$input_file" ]]; then
        echo "Error: Input file not found: $input_file"
        return 1
    fi
    
    ffmpeg -i "$input_file" -threads $threads -vcodec copy -f segment \
        -segment_time "$segment_time" -reset_timestamps 1 "$output_format"
}

  • 動画から指定した時間の範囲をクリップする
function ffmpeg_clip(){
    local input_file=""
    local start_time=""
    local end_time=""
    local output_file=""
    local duration=""
    local threads=4
    local codec="copy"
    
    while [[ $# -gt 0 ]]; do
        case $1 in
            -i|--input)
                input_file="$2"
                shift 2
                ;;
            -s|--start)
                start_time="$2"
                shift 2
                ;;
            -e|--end)
                end_time="$2"
                shift 2
                ;;
            -d|--duration)
                duration="$2"
                shift 2
                ;;
            -o|--output)
                output_file="$2"
                shift 2
                ;;
            -c|--codec)
                codec="$2"
                shift 2
                ;;
            --threads)
                threads="$2"
                shift 2
                ;;
            -h|--help)
                echo "Usage: ffmpeg_clip -i <input> -s <start> (-e <end>|-d <duration>) -o <output>"
                echo ""
                echo "Options:"
                echo "  -i, --input     Input video file (required)"
                echo "  -s, --start     Start time (required, format: HH:MM:SS or seconds)"
                echo "  -e, --end       End time (format: HH:MM:SS or seconds)"
                echo "  -d, --duration  Duration instead of end time (format: HH:MM:SS or seconds)"
                echo "  -o, --output    Output file (required)"
                echo "  -c, --codec     Video codec (default: copy)"
                echo "  --threads       Number of threads (default: 4)"
                echo ""
                echo "Examples:"
                echo "  # 1分30秒から5分まで切り取り(時間形式)"
                echo "  ffmpeg_clip -i video.mp4 -s 00:01:30 -e 00:05:00 -o clip.mp4"
                echo ""
                echo "  # 90秒から30秒間切り取り(秒数指定)"
                echo "  ffmpeg_clip -i video.mp4 -s 90 -d 30 -o clip.mp4"
                echo ""
                echo "  # 最初の1分を切り取り"
                echo "  ffmpeg_clip -i video.mp4 -s 0 -d 60 -o intro.mp4"
                echo ""
                echo "  # 10分から15分まで切り取り、H.264で再エンコード"
                echo "  ffmpeg_clip -i video.mp4 -s 10:00 -e 15:00 -o clip.mp4 -c libx264"
                echo ""
                echo "  # 高速処理のため8スレッド使用"
                echo "  ffmpeg_clip -i video.mp4 -s 00:30:00 -d 00:10:00 -o clip.mp4 --threads 8"
                return 0
                ;;
            *)
                echo "Unknown option: $1"
                return 1
                ;;
        esac
    done
    
    if [[ -z "$input_file" ]]; then
        echo "Error: Input file is required"
        echo "Use -h or --help for usage information"
        return 1
    fi
    
    if [[ ! -f "$input_file" ]]; then
        echo "Error: Input file not found: $input_file"
        return 1
    fi
    
    if [[ -z "$start_time" ]]; then
        echo "Error: Start time is required"
        echo "Use -h or --help for usage information"
        return 1
    fi
    
    if [[ -z "$end_time" && -z "$duration" ]]; then
        echo "Error: Either end time or duration is required"
        echo "Use -h or --help for usage information"
        return 1
    fi
    
    if [[ -n "$end_time" && -n "$duration" ]]; then
        echo "Error: Cannot specify both end time and duration"
        return 1
    fi
    
    if [[ -z "$output_file" ]]; then
        echo "Error: Output file is required"
        echo "Use -h or --help for usage information"
        return 1
    fi
    
    if [[ -n "$duration" ]]; then
        ffmpeg -i "$input_file" -ss "$start_time" -t "$duration" -threads $threads \
            -c:v $codec -c:a copy "$output_file"
    else
        ffmpeg -i "$input_file" -ss "$start_time" -to "$end_time" -threads $threads \
            -c:v $codec -c:a copy "$output_file"
    fi
}

kind編

kindとはローカルのdocker環境でKubernetesのクラスタを構築できるものになります。以下の記事でも紹介しているのでぜひ参照してください!

https://zenn.dev/akasan/articles/1633f745945c56

  • クラスタの作成
    • 使い方:kind_create クラスタ名
function kind_create(){
  if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ -z "$1" ]]; then
    echo "Usage: kind_create <cluster-name>"
    echo "Creates a new kind cluster with the specified name"
    return 0
  fi
  kind create cluster --name $1
}
  • クラスタの削除
    • 使い方:kind_delete クラスタ名
function kind_delete(){
  if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ -z "$1" ]]; then
    echo "Usage: kind_delete <cluster-name>"
    echo "Deletes a kind cluster with the specified name"
    return 0
  fi
  kind delete cluster --name $1
}

そのほか

  • ファイルやフォルダのバックアップの作成
    • 使い方:bk hogehoge -> hogehoge.bkという名前に変わります
function bk(){
  local backup_file_name=$1.bk
  cp -r $1 $backup_file_name
}
  • バックアップから復元
    • 使い方:bk hogehoge.bk -> hogehogeという名前に復元されます
function unbk(){
  local original_name=${1%.bk}
  if [[ $1 == *.bk ]]; then
    if [[ -e $1 ]]; then
      mv $1 $original_name
    else
      echo "Error: Backup file or folder '$1' does not exist"
      return 1
    fi
  else
    echo "Error: File or folder does not have .bk extension"
    return 1
  fi
}
  • .heicファイルをpngに変換する
    • 使い方:heic_png hogehoge.heicを実行するとhogehoge.pngが生成されます
function heic_png() {
  local original_name=${1%.heic}
  if [[ $1 == *.heic ]]; then
    convert $1 $original_name.png
  else
    echo "Error: Please specify .heic file"
  fi
}
  • n8nサーバを起動する(n8nサーバ起動を楽にしたかっただけです)
    • 使い方:n8nを実行
function n8n() {
  docker compose -f /<path-to-n8n>/docker-compose.yml up -d
}
  • フォルダを作成してそのフォルダに移動
    • 使い方:mkcd hogehogeとするとhogehogeフォルダが作成されそのフォルダに移動
function mkcd() {
  mkdir -p "$1" && cd "$1"
}
  • クリップボードにファイルの内容をコピー
    • 使い方:clip hogehogeでhogehogeの内容をコピー
function clip() {
  if [[ -f "$1" ]]; then
    cat "$1" | pbcopy
    echo "Copied contents of $1 to clipboard."
  else
    echo "File not found: $1"
  fi
}
  • 指定したフォルダをzipにする
    • 使い方:zipfolder hogehogeでhogehoge.zipが生成されます
zipfolder() {
    if [[ $# -eq 0 ]]; then
        echo "Usage: zipfolder <folder_path>"
        return 1
    fi
    
    local folder_path="$1"
    
    # Check if the folder exists
    if [[ ! -d "$folder_path" ]]; then
        echo "Error: '$folder_path' is not a valid directory"
        return 1
    fi
    
    # Get the folder name without path
    local folder_name="${folder_path:t}"
    
    # Remove trailing slash if present
    folder_path="${folder_path%/}"
    
    # Create zip file with the same name as the folder
    local zip_file="${folder_name}.zip"
    
    # Check if zip file already exists
    if [[ -e "$zip_file" ]]; then
        echo "Warning: '$zip_file' already exists. Overwrite? (y/N)"
        read -r response
        if [[ ! "$response" =~ ^[Yy]$ ]]; then
            echo "Cancelled"
            return 1
        fi
    fi
    
    # Create the zip file
    echo "Creating $zip_file..."
    zip -r "$zip_file" "$folder_path"
    
    if [[ $? -eq 0 ]]; then
        echo "Successfully created $zip_file"
    else
        echo "Failed to create $zip_file"
        return 1
    fi
}

まとめ

今回は自分が普段よく使っている自作コマンドを紹介しました。自分で定義するのも大変なのでclaude codeとかの助けを借りながら色々実装してます。お世辞にもシェルを使いこなせているとは言えないので、これから精進していこうと思います。ぜひみなさんもおすすめのコマンドがあれば共有してくれると嬉しいです!ちなみに、環境整えたらdotfiles公開する予定です。

Discussion

AkasanAkasan

こうしてみるとあんまり自作コマンド作ってなかったですw
今日は外出していて帰ってきたら時間なく、短編で行かせていただきました