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?

【Linux】tarコマンド周りを整理する

Last updated at Posted at 2024-12-29

■目的

アーカイブ、展開、圧縮、解凍のコマンド(gzip,tar,gunzip)が頭の中でごちゃごちゃになっているので、この機会に整理しようと思う。ググった情報を自分の言葉でまとめ直すと理解度もアップするはず。

■gzip,tar,gunzipの整理

◆アーカイブと圧縮の言葉の意味の整理

   ・アーカイブ(書籍化)⇒まとめる
   ・展開⇒アーカイブされたファイルをばらす
   ・圧縮⇒小さく

◆gzip,tar,gunzipの違い

   ・tar⇒ アーカイブ〇 圧縮✕("ター"って読むから"アー"カイブ)
   ・gzip⇒アーカイブ✕ 圧縮〇
   ・gunzip⇒解凍(実際は、gzip -dで解凍する。gunzipは使わないことが多い。)

◆拡張子.tar.gz

  tarでアーカイブして(まとめて)からgzipで圧縮した(小さくした)ときに作られるファイルに付く。

■使い方

◆tar

   ・アーカイブを作成したい
    ⇒tar cvf ≪任意のアーカイブ名≫.tar 【ファイル1】 【ファイル2】・・・

[root@www test]# ll
合計 0
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileC
[root@www test]# tar cvf fileabc fileA fileB fileC
fileA
fileB
fileC
[root@www test]# ll
合計 12
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileC
12 -rw-r--r-- 1 root root 10240 2024/12/28 18:15:34 fileabc

⇒fileabcが作成されたこと

   ・アーカイブを展開したい
    ⇒tar xvf ≪展開したいアーカイブのファイル名≫
※展開できたことの確認のために、fileA、fileB、fileCを事前に削除しておく。

[root@www test]# rm file{A..C}
rm: 通常の空ファイル 'fileA' を削除しますか? y
rm: 通常の空ファイル 'fileB' を削除しますか? y
rm: 通常の空ファイル 'fileC' を削除しますか? y
[root@www test]# ll
合計 12
12 -rw-r--r-- 1 root root 10240 2024/12/28 18:15:34 fileabc

⇒fileA、fileB、fileCをすべて削除できたこと

[root@www test]# tar xvf fileabc
fileA
fileB
fileC
[root@www test]# ll
合計 12
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root     0 2024/12/28 18:13:19 fileC
12 -rw-r--r-- 1 root root 10240 2024/12/28 18:15:34 fileabc

⇒fileA、fileB、fileCが作られたこと

   ・gzipで圧縮したアーカイブを作成したい
    ⇒tar cvzf ≪任意のアーカイブ名≫.tar.gz 【ファイル1】 【ファイル2】・・・

[root@www test]# tar cvzf fileabc.tar.gz fileA fileB fileC
fileA
fileB
fileC
[root@www test]# ll
合計 4
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileC
4 -rw-r--r-- 1 root root 134 2024/12/29 09:16:45 fileabc.tar.gz

fileabc.tar.gzが作成されたこと

   ・gzipで圧縮したアーカイブを解凍し、展開したい
    ⇒tar xvzf ≪解凍し、展開したいアーカイブのファイル名≫
※展開できたことの確認のために、fileA、fileB、fileCを事前に削除しておく。

[root@www test]# rm fileA fileB fileC
rm: 通常の空ファイル 'fileA' を削除しますか? y
rm: 通常の空ファイル 'fileB' を削除しますか? y
rm: 通常の空ファイル 'fileC' を削除しますか? y
[root@www test]# ll
合計 4
4 -rw-r--r-- 1 root root 134 2024/12/29 09:16:45 fileabc.tar.gz

⇒fileA、fileB、fileCをすべて削除できたこと

[root@www test]# tar xvzf fileabc.tar.gz
fileA
fileB
fileC
[root@www test]# ll
合計 4
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root   0 2024/12/28 18:13:19 fileC
4 -rw-r--r-- 1 root root 134 2024/12/29 09:16:45 fileabc.tar.gz

⇒fileA、fileB、fileCが作られたこと

◆gzip

   ・ファイルを圧縮したい
    ⇒gzip ≪ファイル名≫

[root@www test]# ll
合計 0
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root 0 2024/12/28 18:13:19 fileC
[root@www test]# gzip fileA
[root@www test]# ll
合計 4
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileA.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileB
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileC

⇒fileA.gzが作られたこと

   ・ファイルを残して圧縮したい
    ⇒gzip -k ≪ファイル名≫

[root@www test]# gzip -k fileB
[root@www test]# ll
合計 8
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileA.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileB
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileB.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileC

⇒fileB.gzが作成されたこと、fileBが残っていること

   ・.gzファイルを解凍したい
    ⇒gzip -d ≪ファイル名≫.gz

[root@www test]# ll
合計 8
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileA.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileB
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileB.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileC
[root@www test]# gzip -d fileA.gz
[root@www test]# ll
合計 4
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileA
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileB
4 -rw-r--r-- 1 root root 26 2024/12/28 18:13:19 fileB.gz
0 -rw-r--r-- 1 root root  0 2024/12/28 18:13:19 fileC

⇒fileAが作られたこと

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?