アーカイブファイルと圧縮
#2020.03 #新しいLinuxの教科書
tarコマンド ファイルをアーカイブする
tar cf アーカイブファイル名 アーカイブするファイルのパス ファイルをアーカイブする(cはcreate、fはfileのf)
tarコマンドのオプションはハイフンなしでもOK
code:tarコマンド
vagrant 13:21:25 ~/work $ ls
2015.txt 2021.csv cal-p.txt dir1 hello world.txt newfile1.txt sample.dat testtxt.txt
vagrant 13:21:29 ~/work $ tar cf dir1.tar dir1
vagrant 13:22:17 ~/work $ ls
2015.txt 2021.csv cal-p.txt dir1 file-1.txt newMail.txt sample.csv testdir
dir1.tar
vagrant 13:22:19 ~/work $
tar tf アーカイブファイル アーカイブファイルの内容を表示する(tはlistのt)
code:tar tf
vagrant 13:25:15 ~/work $ tar tf dir1.tar
dir1/
dir1/doc/
dir1/doc/file-1.txt
dir1/doc2/
dir1/doc2/file-1.txt
dir1/file-1.txt
dir1/file-2.txt
dir1/file-3.txt
dir1/file-4.txt
dir1/file-5.txt
vagrant 13:25:32 ~/work $
tar xf アーカイブファイル アーカイブファイルを展開する(xはextractのx)
code:tar xf
vagrant 13:27:37 ~/work $ ls
2014.txt 2020.csv cal-copy.txt crontab file-1.txt newMail.txt sample.csv testdir
2015.txt 2021.csv cal-p.txt dir1.tar hello world.txt newfile1.txt sample.dat testtxt.txt
2016.txt 2022.csv cal.txt example mail.txt root.txt sample.txt
vagrant 13:27:37 ~/work $
vagrant 13:27:39 ~/work $
vagrant 13:27:40 ~/work $ tar xf dir1.tar
vagrant 13:28:43 ~/work $ ls
2014.txt 2020.csv cal-copy.txt crontab example mail.txt root.txt sample.txt
2015.txt 2021.csv cal-p.txt dir1 file-1.txt newMail.txt sample.csv testdir
2016.txt 2022.csv cal.txt dir1.tar hello world.txt newfile1.txt sample.dat testtxt.txt
vagrant 13:28:44 ~/work $
vオプション 実行と同時にファイルリストを表示する
code:tar cvf
vagrant 13:30:54 ~/work $ tar cvf testdir.tar testdir
testdir/ # 実行と同時にリスト表示
testdir/tmpfile
testdir/test/
testdir/touchedfile
vagrant 13:31:19 ~/work $ ls
2014.txt 2020.csv cal-copy.txt crontab example mail.txt root.txt sample.txt testtxt.txt
2015.txt 2021.csv cal-p.txt dir1 file-1.txt newMail.txt sample.csv testdir
2016.txt 2022.csv cal.txt dir1.tar hello world.txt newfile1.txt sample.dat testdir.tar
vagrant 13:31:21 ~/work $ tar tf testdir.tar
testdir/  # 先ほどと同じリストが表示される
testdir/tmpfile
testdir/test/
testdir/touchedfile
vagrant 13:31:44 ~/work $
vagrant 13:31:44 ~/work $ tar tvf testdir.tar  # tvを一緒に使うと詳細が表示される
drwxrwxr-x vagrant/vagrant 0 2020-03-01 05:40 testdir/
-rw-rw-r-- vagrant/vagrant 0 2020-03-01 05:33 testdir/tmpfile
drwxrwxr-x vagrant/vagrant 0 2020-03-01 05:39 testdir/test/
-rw-rw-r-- vagrant/vagrant 0 2020-03-01 05:40 testdir/touchedfile
vagrant 13:32:39 ~/work $
tarはファイル属性を保持する
ファイル属性とはパーミッションやオーナーなどの情報
一般ユーザーでtarコマンドを実行する場合は注意
rootオーナーのファイルなど一部のアーカイブが不完全になる可能性があるので、root権限で行うのが良い
gzipコマンド gzip 圧縮ファイル
code:gzip
vagrant 13:37:36 ~/work $ ps aux > ps.txt
vagrant 13:38:00 ~/work $ ls -lh ps.txt
-rw-rw-r--. 1 vagrant vagrant 6.6K Mar 16 13:37 ps.txt
vagrant 13:38:15 ~/work $ gzip ps.txt
vagrant 13:38:31 ~/work $ ls -lh ps.txt.gz
-rw-rw-r--. 1 vagrant vagrant 1.5K Mar 16 13:37 ps.txt.gz # 6.6k->1.5kに圧縮された
vagrant 13:38:36 ~/work $
# 圧縮ファイルを展開する
vagrant 13:39:49 ~/work $ gzip -d ps.txt.gz (gunzip ps.txt.gz でも一緒)
vagrant 13:40:02 ~/work $ ls -lh ps.txt
-rw-rw-r--. 1 vagrant vagrant 6.6K Mar 16 13:37 ps.txt
vagrant 13:40:09 ~/work $
code:-cオプション
# 標準出力にgzipファイルを出力
vagrant 13:43:42 ~/work $ gzip -c ps.txt > ps_test.txt.gz
vagrant 13:44:05 ~/work $ ls
2014.txt 2020.csv cal-copy.txt crontab example mail.txt ps.txt sample.csv testdir
2015.txt 2021.csv cal-p.txt dir1 file-1.txt newMail.txt ps_test.txt.gz sample.dat testdir.tar
2016.txt 2022.csv cal.txt dir1.tar hello world.txt newfile1.txt root.txt sample.txt testtxt.txt
vagrant 13:44:06 ~/work $
アーカイブ作成と同時にzipファイルを作る
tar czf dir1.tar.gz dir1 zオプションを使う、.tar.gzという形式を使う
tar xzf dir1.tar.gz アーカイブとzipを一気に展開
パイプラインを使ったアーカイブと展開
code:\
$ tar cf - dir1 | gzip -c > dir1.tar.gz
# tarコマンドの第一引数をハイフンにすると結果を標準出力に出力できる
# ->それをgzipに渡して-cで標準出力して出力先にファイルを指定する
$ gzip -d -c dir1.tar.gz | tar xf -
# -dでzipを展開して、-cコマンドとパイプラインでtarに渡す
# ->tarのxオプションでファイル名にハイフンを指定すると標準入力から読み込むことができる
$ ssh mochi@serverB 'tar czf - dir1' | tar xzf -
リモートホストserverBのdir1ディレクトリをtar+gzファイルとして転送
bzip2コマンド
zipコマンド
標準でインストールされていないのでインストールする
code:install zip
vagrant 14:02:55 ~ $ sudo yum install zip unzip
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
・
・
・
Installed:
unzip.x86_64 0:6.0-20.el7 zip.x86_64 0:3.0-11.el7
Complete!
vagrant 14:03:42 ~ $ which zip
/usr/bin/zip
vagrant 14:03:47 ~ $ which unzip
/usr/bin/unzip