{"id":3572,"date":"2017-04-03T11:10:45","date_gmt":"2017-04-03T05:40:45","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=3572"},"modified":"2017-04-03T11:10:45","modified_gmt":"2017-04-03T05:40:45","slug":"compressing-directory-with-bzip2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/compressing-directory-with-bzip2\/","title":{"rendered":"LINUX &#8211; Compressing directory with bzip2"},"content":{"rendered":"<h4 id=\"bzip2\"><span style=\"color: #993300;\"><b>Bzip2:<\/b><\/span><\/h4>\n<ul>\n<li>bzip2 is a free and open-source file compression program that uses the Burrows\u2013Wheeler algorithm.<\/li>\n<li>It only compresses single files and is not a file archiver. It is developed and maintained by Julian Seward.<\/li>\n<li>The bzip2 command is used for compressing and decompressing files.<\/li>\n<li>Data compression, also referred to as just compression, is the process of encoding data using fewer bits.<\/li>\n<li>Data decompression, or just decompression, is the process of restoring compressed data back into a form in which it is again useful.<\/li>\n<li>bzip2 features a high rate of compression together with reasonably fast speed.<\/li>\n<li>Most files can be compressed to a smaller file size than is possible with the more traditional gzip and zip programs.<\/li>\n<li>Compression is performed even if the compressed file is larger than the original.<\/li>\n<li>This situation can occur in the case of very small files, because the compression mechanism has a fixed overhead of about 50 bytes.<\/li>\n<\/ul>\n<h4 id=\"compress-a-single-file\"><span style=\"color: #ff6600;\"><b>Compress a single file<\/b><\/span><\/h4>\n<ul>\n<li>This will compress file.txt and create file.txt.bz2, note that this will remove the original file.txt file.<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bzip2 file.txt<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"compress-multiple-files-at-once\"><span style=\"color: #99cc00;\"><b>Compress multiple files at once<\/b><\/span><\/h4>\n<ul>\n<li>This will compress all files specified in the command, note again that this will remove the original files specified by turning file1.txt, file2.txt and file3.txt into file1.txt.bz2, file2.txt.bz2 and file3.txt.bz2<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bzip2 file1.txt file2.txt file3.txt<\/code><\/pre> <\/div>\n<h4 id=\"compress-a-single-file-and-keep-the-original\"><span style=\"color: #993300;\">\u00a0<b>Compress a single file and keep the original<\/b><\/span><\/h4>\n<ul>\n<li>You can instead keep the original file and create a compressed copy.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bzip2 -c file.txt &gt; file.txt.bz<\/code><\/pre> <\/div>\n<ul>\n<li>The -c flag outputs the compressed copy of file.txt to stdout, this is then sent to file.txt.bz2, keeping the original file.txt file in place.<\/li>\n<li>The version of bzip2 that I am testing with, 1.0.6, which is currently the latest available as of this writing also has the -k option which keeps the original file, so alternatively you could also run the below command to get the same result.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">                                                                            bzip2 -k file.txt<\/code><\/pre> <\/div>\n<h4 id=\"decompress-a-bzip2-compressed-file\"><span style=\"color: #993300;\">\u00a0<span style=\"color: #ff6600;\"><b>Decompress a bzip2 compressed file<\/b><\/span><\/span><\/h4>\n<ul>\n<li>To reverse the compression process and get the original file back that you have compressed, you can use the bzip2 command itself or bunzip2 which is also part of the bzip2 package.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">  bzip2 -d file.txt.bz2<\/code><\/pre> <\/div>\n<p>OR<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\"> bunzip2 file.txt.bz2<\/code><\/pre> <\/div>\n<ul>\n<li>Both of these commands will produce the same result, decompressing file.txt.bz2 to file.txt, removing the compressed file.txt.bz2 file.<\/li>\n<li>Similar to example 3, it is possible to decompress a file and keep the original .bz2 file as below.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bunzip2 -c file.txt.bz2 &gt; file.txt<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>OR<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bunzip2 -k file.txt.bz2<\/code><\/pre> <\/div>\n<h4 id=\"list-compression-information\"><span style=\"color: #99cc00;\"><b>List compression information<\/b><\/span><\/h4>\n<ul>\n<li>With the -v or &#8211;verbose flag we can see useful information regarding the compression ratio of a file, which shows us how much disk space our compression is saving. Additional \u2018v\u2019 flags can be added for more in depth information.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">[root@centos ~]# bzip2 -v linux-3.18.19.tar<br\/>  linux-3.18.19.tar:  6.015:1,  1.330 bits\/byte, 83.37% saved, 580761600 in, 96552670 out.<br\/><br\/>[root@centos ~]# ls -lah<br\/>-rw-r--r--. 1 root root 554M Jul 22 10:38 linux-3.18.19.tar<br\/>-rw-r--r--. 1 root root  93M Jul 22 10:38 linux-3.18.19.tar.bz2<\/code><\/pre> <\/div>\n<ul>\n<li>In this example, a bzipped copy of the Linux kernel has compressed to 83.87% of its original size, taking up 93MB of space rather than 554MB.<\/li>\n<\/ul>\n<h4 id=\"compress-a-directory\"><span style=\"color: #993300;\"><b>Compress a directory<\/b>\u00a0 \u00a0<\/span><\/h4>\n<ul>\n<li>With the help of the tar command, we can create a tar file containing a whole directory and compress the result with bzip2.<\/li>\n<li>We can perform the whole lot in one step, as the tar command allows us to specify a compression method to use.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">tar cjvf etc.tar.bz2 \/etc\/<\/code><\/pre> <\/div>\n<ul>\n<li>\u00a0This example creates a compressed etc.tar.bz2 file of the entire \/etc\/ directory.<\/li>\n<li>The tar flags are as follows, \u2018c\u2019 creates a new tar archive, \u2018j\u2019 specifies that we want to compress with bzip2, \u2018v\u2019 provides verbose information, and \u2018f\u2019 specifies the file to create.<\/li>\n<li>The resulting etc.tar.bz2 file contains all files within \/etc\/ compressed using bzip2.<\/li>\n<\/ul>\n<h4 id=\"concatenate-multiple-files\"><span style=\"color: #ff6600;\"><b>Concatenate multiple files<\/b><\/span><\/h4>\n<ul>\n<li>Multiple files can be concatenated into a single .bz2 file.<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">bash code<\/span> <\/div> <pre class=\"language-bash code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-bash code-embed-code\">bzip2 -c file1.txt &gt; files.bz2<br\/>bzip2 -c file2.txt &gt;&gt; files.bz2<\/code><\/pre> <\/div>\n<ul>\n<li>The files.bz2 now contains the contents of both file1.txt and file2.txt, if you decompress files.bz2 you will get a file named \u2018files\u2019 which contains the content of both .txt files.<\/li>\n<li>The output is similar to running \u2018cat file1.txt file2.txt\u2019.<\/li>\n<li>If instead you want to create a single file that contains multiple files you can use the tar command which supports bzip2 compression,<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Compressing directory with bzip2 &#8211; bzip2 is a free and open-source file compression program that uses the Burrows\u2013Wheeler algorithm<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1699],"tags":[6492,6491,6495,6496,6500,6499,6497,6498,6502,6518,6515,6514,6493,6512,4689,6501,6516,1380,1446,6494,6519,6509,1456,1453,6508,1444,1447,1445,6520,6521,6506,6507,6503,6510,6504,6513,6511,6505,1448,6517],"class_list":["post-3572","post","type-post","status-publish","format-standard","hentry","category-linux","tag-ubuntu-how-to-use-bzip2-command-to-compress-directory","tag-10-simple-bzip2-examples","tag-bzip2-all-files-in-a-directory","tag-bzip2-command-in-linux-with-examples","tag-bzip2-compress-file","tag-bzip2-compress-to-different-directory","tag-bzip2-decompress-to-another-directory","tag-bzip2-multiple-files-into-one","tag-compress-files","tag-compress-folder","tag-compress-zip-file-gz-file-extract","tag-compressed-zip-folder","tag-compression-create-a-tar-bz2-file-linux","tag-extract-tar-file","tag-file-compression-and-archiving","tag-file-compressor","tag-gzip-command","tag-gzip-command-in-linux","tag-gzip-compression","tag-how-do-i-compress-a-whole-linux-or-unix-directory","tag-how-to-compress-a-file","tag-how-to-compress-files","tag-how-to-compress-zip-files","tag-how-to-extract-tar-file","tag-how-to-unzip-tar-gz","tag-tar-command","tag-tar-command-in-linux","tag-tar-file","tag-tar-file-extractor","tag-tar-linux","tag-tar-xvf","tag-tgz-extractor","tag-tgz-file","tag-tgz-file-extractor-tar-extract","tag-untar-command","tag-unzip-gz","tag-unzip-tar-file","tag-zip-command","tag-zip-command-in-linux","tag-zip-file-compressor"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=3572"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/3572\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=3572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=3572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=3572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}