Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

こんな構成のディレクトリ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
naoyes
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away