このページで解説している内容は、以下の YouTube 動画の解説で見ることができます。

Linuxコマンド演習07

以下のコマンドの演習を行います。

実際にコマンド操作を行いながら、Linuxの操作に慣れていきましょう。

演習01:gzip コマンド

・’/etc/services’ ファイルをカレントディレクトリにコピーします。

$ cp /etc/services .
$ ls -l
合計 656
drwxr-xr-x. 2 user01 user01     40  1月  3 12:57 Desktop
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Documents
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Downloads
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Music
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Pictures
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Public
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Templates
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Videos
-rw-r--r--. 1 user01 user01 670293  1月  8 12:10 services

‘services’ のファイルサイズは、670,293バイトです。

・’services’ ファイルを圧縮します。

$ gzip services
$ ls -l
合計 136
drwxr-xr-x. 2 user01 user01     40  1月  3 12:57 Desktop
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Documents
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Downloads
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Music
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Pictures
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Public
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Templates
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Videos
-rw-r--r--. 1 user01 user01 136088  1月  8 12:10 services.gz

 ファイルサイズが、約1/5の136,088バイトに圧縮されています。元の ‘services’ ファイルが削除され、拡張子「.gz」が付いた ‘services.gz’ が作られています。gzipコマンドは、ファイルの圧縮を行うコマンドで、ディレクトリの圧縮には対応していません。

演習02:gunzip コマンド

・’services.gz’ ファイルを解凍します。

$ gunzip services.gz
$ ls -l
合計 656
drwxr-xr-x. 2 user01 user01     40  1月  3 12:57 Desktop
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Documents
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Downloads
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Music
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Pictures
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Public
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Templates
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Videos
-rw-r--r--. 1 user01 user01 670293  1月  8 12:10 services

 ’services.gz’ ファイルが解凍され、’services’ ファイルが作成されます。元の圧縮ファイルは削除され、解凍されたファイルだけが残ります。

演習03:gzip コマンド

・標準出力に圧縮データを出力し、それをリダイレクトして保存します。

圧縮前のデータを残して、圧縮ファイルを作成します。

$ gzip -c services > services.gz
$ ls -l
合計 792
drwxr-xr-x. 2 user01 user01     40  1月  3 12:57 Desktop
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Documents
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Downloads
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Music
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Pictures
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Public
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Templates
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Videos
-rw-r--r--. 1 user01 user01 670293  1月  8 12:10 services
-rw-rw-r--. 1 user01 user01 136088  1月  8 13:57 services.gz

圧縮前のファイル’services’ が残っていることが確認できます。

演習04:gunzip コマンド

・元の圧縮ファイルを保持するように解凍します。

まず、’service’ ファイルを削除しておきます。

$ rm services
$ ls
Desktop    Downloads  Pictures  Templates  services.gz
Documents  Music      Public    Videos

・圧縮ファイル’services.gz’ を保持するように解凍します。

$ gunzip -c services.gz > services
$ ls -l
合計 792
drwxr-xr-x. 2 user01 user01     40  1月  3 12:57 Desktop
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Documents
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Downloads
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Music
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Pictures
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Public
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Templates
drwxr-xr-x. 2 user01 user01      6  1月  3 12:48 Videos
-rw-rw-r--. 1 user01 user01 670293  1月  8 14:40 services
-rw-rw-r--. 1 user01 user01 136088  1月  8 13:57 services.gz

 ’-c’ オプションを指定して標準出力への内容をリダイレクトして’services’ ファイルに保存することで、元の圧縮ファイル ‘services.gz’ は保持されます。

演習終了時の作業:ファイルの削除

・この演習で作成したファイルを削除しておきます。

$ rm services
$ rm services.gz