0
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でスペース入りのファイル名をクォートしようとしたら不要だった

0
Posted at

sed -e 's/^/"/' -e 's/$/"/'とかいらなかった

背景

.gitignoreで追跡されてないファイルをtarで固めるスクリプトを書いていた
ファイル名にスペース入のファイルもちゃんと固められるようにしたかった。
そんなものを作るなといったらそれまでだが。

試したこと

ansibleのplaybookやrolesがあるディレクトリでhost_varsやインベントリをtarに固める。

git ls-files --other --ignored --exclude-standard
# host_vars/a b    <- こういうファイルをちゃんとtarで固めたい
# host_vars/rocky01.yaml
# host_vars/ubuntu01.yaml
# inventory.ini


# こうする必要があると思った
git ls-files --other --ignored --exclude-standard | sed -e 's/^/"/' -e 's/$/"/'
# "host_vars/a b"
# "host_vars/rocky01.yaml"
# "host_vars/ubuntu01.yaml"
# "inventory.ini"

# tarしてみる
#   ※ tarの-Tオプション: 通常はアーカイブ対象を引数で指定するが、
#      -Tでファイル名を渡すとそのファイルに記載されたものがアーカイブされる。
#      ファイル名ではなく'-'を渡すと標準入力からアーカイブ対象のファイル名を読み取る
ARCHIVE_NAME="ansible_20251130.tar.gz"
git ls-files --other --ignored --exclude-standard | sed -e 's/^/"/' -e 's/$/"/' | tar czvf "$ARCHIVE_NAME" --quote-chars=\" -T -
# tar: "host_vars/a b": stat 不能: そのようなファイルやディレクトリはありません
# tar: "host_vars/rocky01.yaml": stat 不能: そのようなファイルやディレクトリはありません
# tar: "host_vars/ubuntu01.yaml": stat 不能: そのようなファイルやディレクトリはありません
# tar: "inventory.ini": stat 不能: そのようなファイルやディレクトリはありません
# tar: 前のエラーにより失敗ステータスで終了します

man tarquoteという文字列で検索をかけたところ--quote-chars--quoting-styleという
オプションを発見。

       --quote-chars=STRING
              Additionally quote characters from STRING.

       --quoting-style=STYLE
              Set quoting style for file and member names.  Valid values for STYLE are literal, shell,
              shell-always, c, c-maybe, escape, locale, clocale.

※ quoting-styleで指定可能な値については別途書く。

試してみる。

git ls-files --other --ignored --exclude-standard | sed -e 's/^/"/' -e 's/$/"/' | tar czvf "$ARCHIVE_NAME" --quote-chars=\" -T -
# tar: \"host_vars/a b\": stat 不能: そのようなファイルやディレクトリはありません
# tar: \"host_vars/rocky01.yaml\": stat 不能: そのようなファイルやディレクトリはありません
# tar: \"host_vars/ubuntu01.yaml\": stat 不能: そのようなファイルやディレクトリはありません
# tar: \"inventory.ini\": stat 不能: そのようなファイルやディレクトリはありません
# tar: 前のエラーにより失敗ステータスで終了します

git ls-files --other --ignored --exclude-standard | sed -e 's/^/"/' -e 's/$/"/' |  tar czvf "$ARCHIVE_NAME" --quoting-style=shell -T -
tar: '"host_vars/a b"': stat 不能: そのようなファイルやディレクトリはありません
tar: '"host_vars/rocky01.yaml"': stat 不能: そのようなファイルやディレクトリはありません
tar: '"host_vars/ubuntu01.yaml"': stat 不能: そのようなファイルやディレクトリはありません
tar: '"inventory.ini"': stat 不能: そのようなファイルやディレクトリはありません
tar: 前のエラーにより失敗ステータスで終了します

なんだか様子がおかしい。
試しにsedとquote-charsなどのオプションなしでやってみる。

git ls-files --other --ignored --exclude-standard |  tar czvf "$ARCHIVE_NAME"  -T -
# host_vars/a b
# host_vars/rocky01.yaml
# host_vars/ubuntu01.yaml
# inventory.ini

成功した。

tar -tf "$ARCHIVE_NAME" 
# host_vars/a b
# host_vars/rocky01.yaml
# host_vars/ubuntu01.yaml
# inventory.ini

# 適当な場所にtar.gzを移して試しに展開
TEMP_DIR="tmp"
mkdir "$TEMP"
mv "$ARCHIVE_NAME" "$TMP"
cd "$TMP"
tar -xzvf "$ARCHIVE_NAME"
# host_vars/a b
# host_vars/rocky01.yaml
# host_vars/ubuntu01.yaml
# inventory.ini

展開も無事できた…

チャンチャン

0
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
0
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?