サーバーのよく使うコマンドメモ|tar

2015年2月7日

コマンド サーバー テクノロジー

Summery

ファイルやフォルダを圧縮したり解凍したりする。 ファイル整理やバックアップバッチで活用しよう!

Construction

$ tar [option] [file]

Option

-z : データが圧縮(gunzip) -c : 複数のファイルをまとめる(tar) -v : 作業中にターミナルに表示を行う -f : ファイル指定 -x : 解凍 -C : 解凍先の指定 -t : 内容リストの取得

Sample

# 圧縮(archive)※圧縮後のファイル名、対象のファイルの順番に注意 $ tar zcvf hoge.tgz huga.txt # 解凍(unarchive) $ tar zxvf hoge.tgz # 指定フォルダに解凍 $ tar zxvfC hoge.tgz sore/ # 圧縮ファイルの中身のリストを取得 *実際には解凍されないので、確認の時に使おう $ tar tf hoge.tgz # 圧縮ファイルの中身のリストを取得(容量も表示) $ tar tfv hoge.tgz # 特定のファイルを対象外にする $ tar zcvf hoge,tgz hoge/ --exclude uga.dat # 解凍する時に困るので、対象ディレクトリの相対パスに移動してからアーカイブする $ tar zcvf hoge.tgz -C /etc/test aho/

Caution!

第2引数の対象ファイル(ディレクトリ)をrootから記述すると、エラーになる。

※圧縮は行われるがエラーメッセージが出る。 ※おそらく別システムに持っていった時に、絶対パスで書くとNGになる可能性がある為と思われる。

Discription

tar creates and manipulates streaming archive files. This implementation can extract from tar, pax, cpio, zip, jar, ar, and ISO 9660 cdrom images and can create tar, pax, cpio, ar, and shar archives. The first synopsis form shows a ``bundled'' option word. This usage is provided for compatibility with historical implementations. See COMPATIBILITY below for details. The other synopsis forms show the preferred usage. The first option to tar is a mode indicator from the following list: -c Create a new archive containing the specified items. -r Like -c, but new entries are appended to the archive. Note that this only works on uncompressed archives stored in regular files. The -f option is required. -t List archive contents to stdout. -u Like -r, but new entries are added only if they have a modification date newer than the corresponding entry in the archive. Note that this only works on uncompressed archives stored in regular files. The -f option is required. -x Extract to disk from the archive. If a file with the same name appears more than once in the archive, each copy will be extracted, with later copies over- writing (replacing) earlier copies. In -c, -r, or -u mode, each specified file or directory is added to the archive in the order specified on the command line. By default, the contents of each directory are also archived. In extract or list mode, the entire command line is read and parsed before the archive is opened. The pathnames or patterns on the command line indicate which items in the archive should be processed. Patterns are shell-style globbing patterns as documented in tcsh(1).

このブログを検索

ごあいさつ

このWebサイトは、独自思考で我が道を行くユゲタの少し尖った思考のTechブログです。 毎日興味がどんどん切り替わるので、テーマはマルチになっています。 もしかしたらアイデアに困っている人の助けになるかもしれません。