LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compressing directory with bzip2 (https://www.linuxquestions.org/questions/linux-newbie-8/compressing-directory-with-bzip2-336204/)

Haiyadragon 06-22-2005 05:02 PM

Compressing directory with bzip2
 
I'm trying to compress a directory with bzip2. According to documentation I have to 'bzip2 whatever.bz2 dirtopack'. But it gives me a big:

Code:

bzip2: Can't open input file whatever.bz2: No such file or directory.
bzip2: Input file dirtopack is a directory.

What the hell am I doing wrong? I've tried some switches but nothing works. Tar works fine but bzip2 is more powerful.

rjlee 06-22-2005 05:13 PM

You seem to misunderstand the difference between tar and bzip2.

tar is a file archive utility that takes a number of files, with pathnames, and puts them into a single binary file. It doesn't do anything to compress the data.

bzip2 (like gzip and compress) is a file (or stream) compression utility, that takes a single file and makes it smaller.

If you want to create an archive of a directory, you first need to create a tar archive, then bzip2 compress the archive; this is why “tarball”s often have the extension .tar.bz2
Code:

tar -cf file.tar dir && bzip2 file.tar
You can also do this in one step:
Code:

tar -cjf file.tar.bz2 dir
This compresses better than all-in-one formats like ZIP, which compress each file seperately then append then put the compressed files into an archive.

If you want to compress each file within a directory, you can use
Code:

bzip2 dir/*
To do this recursively you'll need to use find:
Code:

find dir -exec bzip2 '{}' ';'

sluphiphif 12-18-2016 04:44 PM

Best way to compress as 2016
 
I was googling how to do the best compression of a directory on linux and I found that if you have memory XZ is the best as they explain it here how to compress a directory in linux

AwesomeMachine 12-19-2016 12:23 AM

I always just use the 'j' switch with 'tar'.

rjlee 12-19-2016 03:51 PM

It all depends on what you're trying to do, really.

gzip (-z with tar) is generally fastest to compress and uncompress, but only provides limited compression.
bzip (-j with tar) provides slightly better compression than gzip (except for very small files - unlikely with tar) and takes a little more cpu and memory, but is generally a good alternative.
xz (-J with tar) provides the best compression ratio, but is expensive in terms of time and memory used, so generally best for archiving or creating big distributions over slower networks.


All times are GMT -5. The time now is 06:34 PM.