51
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

tar(1) でアーカイブ前の名前ではなく新たな名前のディレクトリで展開したいときのメモ

Posted at

こんな構成のディレクトリbeforeをアーカイブするとします。

$ tree before/
before/
├── one
│   ├── two
│   │   └── three
│   └── two1
└── one1

3 directories, 2 files

$ tar czvf dummy.tgz before
before/
before/one/
before/one/two1
before/one/two/
before/one/two/three/
before/one1

これをafterという名前で展開します。

$ mkdir after && tar xzvf dummy.tgz -C after --strip-components 1
before/one/
before/one/two1
before/one/two/
before/one/two/three/
before/one1

$ tree after/
after/
├── one
│   ├── two
│   │   └── three
│   └── two1
└── one1

3 directories, 2 files

実現させてくれるオプションのmanualは以下のとおり。

-C, --directory=DIR
       change to directory DIR

--strip-components=NUMBER
       strip NUMBER leading components from file names on extraction

ちなみに--strip-componentsの引数の値を変えてみるとこんな感じです。上位のN階層を「剥ぎとって」くれるようですね。

$ mkdir after2 && tar xzvf dummy.tgz -C after2 --strip-components 2
before/one/two1
before/one/two/
before/one/two/three/

$ tree after2
after2
├── two
│   └── three
└── two1

2 directories, 1 file
51
37
1

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
51
37

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?