Unixコマンド/tar の変更点


*&SIZE(15){''tar''}; [#uc7f5106]

[[dokuwiki.fl8.jp転載済み>http://dokuwiki.fl8.jp/doku.php/02_freebsd/80_unix%E3%82%B3%E3%83%9E%E3%83%B3%E3%83%89/tar]]
#contents

**''機能'' [#l6da1309]
複数のファイルをひとつのファイルにまとめることをアーカイブといいます。~
tarコマンドは、指定したファイルや指定したディレクトリ以下のツリーを、テープなどのメディアに~
アーカイブしたり、アーカイブデータを展開するコマンドです。~
~
▼dumpコマンドとの使い分け~
dumpコマンドがファイルシステム単位でバックアップをとるのに対し、tarコマンドは~
任意のディレクトリ以下をアーカイブできるので、システム全体のバックアップは~
dumpコマンドで、それ以外のバックアップはtarコマンドで、といった使い分けができます。

**''構文'' [#t4bea5fc]
tar [オプション] [ファイル名]

**''オプション'' [#j228cbb4]
>
 c          アーカイブファイルを新規に作成する
 f          指定されたファイルにアーカイブデータを出力する
 t          アーカイブファイルの内容を表示する
 v          ファイル名のほかにタイムスタンプやアクセス権などの詳細な情報を表示する
 x          アーカイブファイルからファイルを取り出す
 z          アーカイブファイルをgzipで圧縮する(FreeBSD・Linux)
 j          bzip2を通して処理を行う 
 X FILE     FILEに記述されたファイル(フォルダ)を除外する。

**''コマンド例'' [#k4de7a24]
***/etcディレクトリ以下のファイルをフロッピーディスク(/dev/fd0)にアーカイブする [#ee4f5dcc]
>
 # cd /etc
 # tar cf /dev/fd0 ./*
***アーカイブ内のファイル名を表示する [#xc0ae567]
>
 # tar tf /dev/fd0 |head
 etc/X11/
 etc/aliases
 etc/amd.map
 etc/antivir.conf
 etc/apmd.conf
 etc/auth.conf
 etc/avguard.conf
 etc/crontab
 etc/csh.cshrc
 etc/csh.login
***アーカイブ内のファイル名とファイル属性を表示する [#u517a48c]
>
 root@vaio# tar tfv /dev/fd0 |head
 drwxr-xr-x root/wheel        0  5 26 06:27 2004 etc/X11/
 lrwxrwxrwx root/wheel        0  9 21 23:27 2004 etc/aliases -> mail/aliases
 -rw-r--r-- root/wheel      208  5 26 06:28 2004 etc/amd.map
 -rw-rw-r-- root/antivir   3623 11 10 07:38 2004 etc/antivir.conf
 -rw-r--r-- root/wheel     1167  5 26 06:28 2004 etc/apmd.conf
 -rw-r--r-- root/wheel      272  5 26 06:28 2004 etc/auth.conf
 -rw-rw-r-- root/antivir   3730 11 10 07:31 2004 etc/avguard.conf
 -rw-r--r-- root/wheel      637  5 26 06:28 2004 etc/crontab
 -rw-r--r-- root/wheel      108  5 26 06:28 2004 etc/csh.cshrc
 -rw-r--r-- root/wheel      486  5 26 06:28 2004 etc/csh.login
***アーカイブデータからファイルを取り出す [#qdc1c34d]
>
 # tar xf /dev/fd0
***特定フォルダを除外する[#fb51420c]
>workフォルダにa,b,cというフォルダがあるとする
 $ find work
 work
 work/a
 work/a/a.html
 work/b
 work/b/b.html
 work/c
 work/c/c.html
>jogaiとうファイルをに除外したいフォルダ名を記述しておく。
 $ echo b > jogai
 $ echo c >> jogai
 $ cat jogai
 b
 c
>jogaiファイルで指定したフォルダを除いて圧縮できる。
 $ tar zcvXf jogai backup.tar.gz x
 work/
 work/a/
 work/a/a.html

***bzip2を通して、tar,gz2で圧縮・解凍 [#ga858385]
>
-圧縮
 $ tar jcvf work.tar.gz2 work
 a work
 a work/sabun.sh
 a work/test.rb
 a work/ch_20060317.txt
 a work/kr_20060317.txt
 a work/test.rb.bak
 a work/seek.sh
 a work/A
 a work/serverlist.txt
 a work/B
 a work/test.txt
 a work/a.sh
 a work/sample01.sh
 a work/test.php
-解凍
 $ tar jxvf work.tar.gz2
 x work
 x work/sabun.sh
 x work/test.rb
 x work/ch_20060317.txt
 x work/kr_20060317.txt
 x work/test.rb.bak
 x work/seek.sh
 x work/A
 x work/serverlist.txt
 x work/B
 x work/test.txt
 x work/a.sh
 x work/sample01.sh
 x work/test.php
-tar.gzより、tar.gz2の方が圧縮率が高い
 $ ls -al
 drwxr-xr-x   2 matsui  Admin      512  8 13 03:36 work/
 -rw-r--r--   1 matsui  users     4900  8 16 11:41 work.tar.gz
 -rw-r--r--   1 matsui  users     4348  8 16 11:40 work.tar.gz2

***特定ファイルだけを取り出す [#df65cb9a]
 $ tar zxvf foo.tgz filename