{"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>\u00a0<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dbzip2%20file.txt%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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[pastacode lang=\u201dbash\u201d manual=\u201dbzip2%20file1.txt%20file2.txt%20file3.txt%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dbzip2%20-c%20file.txt%20%3E%20file.txt.bz%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20bzip2%20-k%20file.txt%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201d%20%20bzip2%20-d%20file.txt.bz2%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>OR<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201d%20bunzip2%20file.txt.bz2%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dbunzip2%20-c%20file.txt.bz2%20%3E%20file.txt%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>OR<\/p>\n[pastacode lang=\u201dbash\u201d manual=\u201dbunzip2%20-k%20file.txt.bz2\u2033 message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u2013verbose 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[pastacode lang=\u201dbash\u201d manual=\u201d%5Broot%40centos%20~%5D%23%20bzip2%20-v%20linux-3.18.19.tar%0A%20%20linux-3.18.19.tar%3A%20%206.015%3A1%2C%20%201.330%20bits%2Fbyte%2C%2083.37%25%20saved%2C%20580761600%20in%2C%2096552670%20out.%0A%0A%5Broot%40centos%20~%5D%23%20ls%20-lah%0A-rw-r\u2013r\u2013.%201%20root%20root%20554M%20Jul%2022%2010%3A38%20linux-3.18.19.tar%0A-rw-r\u2013r\u2013.%201%20root%20root%20%2093M%20Jul%2022%2010%3A38%20linux-3.18.19.tar.bz2%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dtar%20cjvf%20etc.tar.bz2%20%2Fetc%2F%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dbash\u201d manual=\u201dbzip2%20-c%20file1.txt%20%3E%20files.bz2%0Abzip2%20-c%20file2.txt%20%3E%3E%20files.bz2%0A\u201d message=\u201dbash code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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>\u00a0<\/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}]}}