LoginSignup
0
0

ディレクトリの圧縮方法

Last updated at Posted at 2024-01-11

1. 初めに

今回の創成課題でディレクトリを圧縮しました。研究を経てgzipでディレクトリ圧縮する方法を学んだので本記事で紹介します。

2. 圧縮方法について

 データ圧縮方式の1つであるgzipは, ディレクトリを直接圧縮することができません。ディレクトリを事前にアーカイブディレクトリに変換する必要があります。アーカイブディレクトリに変換する際、Linux環境では一般にtarコマンドが使われています。
 第1に、tarコマンドでディレクトリをアーカイブします。tarコマンドは以下のように使用します。

tar -cvf second.tar /path/to/first

"-"でオプションを設定することができます。

  • "-c":新しいアーカイブを作成
  • "-v":圧縮/解凍の進捗を表示
  • "-f": アーカイブファイルの指定
    /path/to/firstにはアーカイブ前のディレクトリパス、second.tarにアーカイブ後のディレクトリ名(名前は自由)を指定します。

実際にコマンドを実行してみると、この様になります。

ken@MyComputer:~/directory_press$ tar -cvf second.tar /home/ken/directory_press/first
tar: Removing leading `/' from member names
/home/ken/directory_press/first/
/home/ken/directory_press/first/大学フォルダ.PNG
/home/ken/directory_press/first/rebarth.PNG
/home/ken/directory_press/first/kadai.PNG
/home/ken/directory_press/first/2021後期.PNG
/home/ken/directory_press/first/kankyo.PNG
/home/ken/directory_press/first/実験環境.PNG
/home/ken/directory_press/first/2022後期.PNG
/home/ken/directory_press/first/y.PNG
/home/ken/directory_press/first/hindo.PNG
/home/ken/directory_press/first/size.PNG
/home/ken/directory_press/first/2022前期.PNG
/home/ken/directory_press/first/gazo.PNG
/home/ken/directory_press/first/2021前期.PNG
/home/ken/directory_press/first/2023前期.PNG
/home/ken/directory_press/first/2023後期.PNG
/home/ken/directory_press/first/ra-.PNG
/home/ken/directory_press/first/実装.PNG
ken@MyComputer:~/directory_press$
ken@MyComputer:~/directory_press$ ls
first  second.tar
ken@MyComputer:~/directory_press$

 第2にtarアーカイブをgzipで圧縮します。gzipコマンドは以下のように使用します。

gzip second.tar

second.tarには第1で作成したアーカイブ名を指定します。
これにより、圧縮されたディレクトリ:second.tar.gzが生成されます。

実際にコマンドを実行してみると、この様になります。

ken@MyComputer:~/directory_press$ ls
first  second.tar
ken@MyComputer:~/directory_press$ gzip second.tar
ken@MyComputer:~/directory_press$ ls
first  second.tar.gz
ken@MyComputer:~/directory_press$

確認として、ディレクトリが正しく圧縮できているか確認します。

ken@MyComputer:~/directory_press$ ls
first  second.tar.gz
ken@MyComputer:~/directory_press$ du -k first
600     first
ken@MyComputer:~/directory_press$ du -k second.tar.gz
480     second.tar.gz
ken@MyComputer:~/directory_press$

圧縮前のfirstディレクトリのサイズが600KBなのに対し、圧縮後のsecond.tar.gzディレクトリは480KBにまで圧縮されています。
これにてディレクトリの圧縮が正しくできていることが確認できました。

また、tarコマンドとgzipコマンドを組み合わせることができます。この場合、前述の第1と第2を一纏めにして実行することができます。コマンドは以下のようになります。

tar -czvf second.tar.gz /path/to/first

"-"でオプションを設定することができます。

  • "-c": 新しいアーカイブを作成する
  • "-z": gzip 圧縮を行う
  • "-v": 圧縮/解凍の進捗を表示する
  • "-f": アーカイブファイルを指定する
    /path/to/firstにはアーカイブ前のディレクトリパス、second.tar.gzにアーカイブ後のディレクトリ名(名前は自由)を指定します。

実際にコマンドを実行してみると、この様になります。

ken@MyComputer:~/directory_press$ ls
first
ken@MyComputer:~/directory_press$ tar -czvf second.tar.gz /home/ken/directory_press/first
tar: Removing leading `/' from member names
/home/ken/directory_press/first/
/home/ken/directory_press/first/大学フォルダ.PNG
/home/ken/directory_press/first/rebarth.PNG
/home/ken/directory_press/first/kadai.PNG
/home/ken/directory_press/first/2021後期.PNG
/home/ken/directory_press/first/kankyo.PNG
/home/ken/directory_press/first/実験環境.PNG
/home/ken/directory_press/first/2022後期.PNG
/home/ken/directory_press/first/y.PNG
/home/ken/directory_press/first/hindo.PNG
/home/ken/directory_press/first/size.PNG
/home/ken/directory_press/first/2022前期.PNG
/home/ken/directory_press/first/gazo.PNG
/home/ken/directory_press/first/2021前期.PNG
/home/ken/directory_press/first/2023前期.PNG
/home/ken/directory_press/first/2023後期.PNG
/home/ken/directory_press/first/ra-.PNG
/home/ken/directory_press/first/実装.PNG
ken@MyComputer:~/directory_press$ ls
first  second.tar.gz

ここでも、正しく圧縮できているか確認します。

ken@MyComputer:~/directory_press$ ls
first  second.tar.gz
ken@MyComputer:~/directory_press$ du -k first
600     first/
ken@MyComputer:~/directory_press$ du -k second.tar.gz
480     second.tar.gz
ken@MyComputer:~/directory_press$

こちらでも、圧縮前のfirstディレクトリのサイズが600KBなのに対し、圧縮後のsecond.tar.gzディレクトリは480KBにまで圧縮されています。
コマンドを一纏めにしてもディレクトリの圧縮が正しくできていることが確認できましたゞ

3. 終わりに

本記事ではgzipでディレクトリ圧縮する方法を紹介しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0