アーカイブファイルと圧縮
tarコマンド ファイルをアーカイブする
tar cf アーカイブファイル名 アーカイブするファイルのパス ファイルをアーカイブする(cはcreate、fはfileのf)
tarコマンドのオプションはハイフンなしでもOK
code:tarコマンド
2015.txt 2021.csv cal-p.txt dir1 hello world.txt newfile1.txt sample.dat testtxt.txt
2015.txt 2021.csv cal-p.txt dir1 file-1.txt newMail.txt sample.csv testdir
dir1.tar
tar tf アーカイブファイル アーカイブファイルの内容を表示する(tはlistのt)
code:tar tf
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
tar xf アーカイブファイル アーカイブファイルを展開する(xはextractのx)
code:tar xf
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
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
vオプション 実行と同時にファイルリストを表示する
code:tar cvf
testdir/ # 実行と同時にリスト表示
testdir/tmpfile
testdir/test/
testdir/touchedfile
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
testdir/ # 先ほどと同じリストが表示される
testdir/tmpfile
testdir/test/
testdir/touchedfile
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
tarはファイル属性を保持する
ファイル属性とはパーミッションやオーナーなどの情報
一般ユーザーでtarコマンドを実行する場合は注意
rootオーナーのファイルなど一部のアーカイブが不完全になる可能性があるので、root権限で行うのが良い
gzipコマンド gzip 圧縮ファイル
code:gzip
-rw-rw-r--. 1 vagrant vagrant 6.6K Mar 16 13:37 ps.txt
-rw-rw-r--. 1 vagrant vagrant 1.5K Mar 16 13:37 ps.txt.gz # 6.6k->1.5kに圧縮された
# 圧縮ファイルを展開する
-rw-rw-r--. 1 vagrant vagrant 6.6K Mar 16 13:37 ps.txt
code:-cオプション
# 標準出力にgzipファイルを出力
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
アーカイブ作成と同時に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
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!
/usr/bin/zip
/usr/bin/unzip