1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ディレクトリ内の tar ファイルを展開して整理する (Bash スクリプト)

Posted at

概要

このスクリプトは、指定ディレクトリにある .tar ファイルを順に展開 し、展開後に元の tar ファイルを old/ ディレクトリに移動して整理する簡単な Bash スクリプトです。

大量の tar アーカイブを自動で展開しつつ、整理する作業に便利です。

スクリプト内容

#!/bin/bash
DIR=/root/work/no_loop_1/tmp
OLDDIR="$DIR/old"

# old ディレクトリを作成
if [ ! -d $OLDDIR ]; then
	mkdir -p "$OLDDIR"
fi

for f in $DIR/*.tar; do
    [ -f "$f" ] || continue
    #echo "extracting $f ..."
    tar -C $DIR -xf "$DIR/$f"
    mv "$DIR/$f" old/
done

特徴・ポイント

自動展開と整理

  • 指定ディレクトリ内のすべての tar ファイルを自動で展開し、整理できます。

二重処理防止

  • 展開済みの tar ファイルは old/ ディレクトリに移動するため、次回実行時に再度展開されません。

安全なディレクトリ指定

  • tar -C "$DIR" により、展開先を明示的に指定して安全に展開できます。

存在チェック付き

  • [ -f "$f" ] || continue により、存在しないファイルをスキップしてエラーを防止しています。

注意点

$DIR にある tar ファイルすべてを展開するので、意図しないファイルがないか事前に確認してください

展開先は $DIR 直下です。必要に応じてサブディレクトリを指定してください

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?