Notes on archiving and compression in Linux. Writing things down helps me to remember!

bunzip2

decompression tool to complement bzip2. By default replaces the compressed file with the decompressed version

bunzip2 mrnoobot.txt.bz2
#decompresses file into mrnoobot.txt

bunzip2 -v Documents/*
#decompresses all files in the Documents directory

bzcat

utility to examine the contents of a file compressed with bzip2, as cat would with a normal file

bzcat noobfile.txt.gbz2
#displays contents of noobfile.txt.bz2

bzip2

compression tool that works in almost identical fashion to gzip but uses different algorithm. REPLACES the source file with compressed file

bzip2 mrnoobot.txt
#produces compressed file mrnoobot.txt.bz2

bzip2 -v mrnoobot.txt
#compresses file into mrnoobot.txt.bz2 and displays compression information VERBOSE

bzip2 -v Documents/*
#compresses all files in Documents

bzip2 -cv mrnoobot.txt > mrnoobot.txt.bz2
#compresses mrnoobot.txt WITHOUT REPLACING it

cpio – copy in/out archiver

cpio is considered a legacy archiving tool. It must be fed an input and stdout must be sent to a file

ls | cpio -ov > noobarchive.cpio
#feeds all files in current directory into cpio which archives into noobarchive.cpio

the find command can be used to add recursion
find . -depth -print | cpio -ov > noobarchive.cpio
#archives all files/folders in current directory to noobarchive.cpio

cpio -iv /tmp/noob < noobarchive.cpio
#take archive file as an input and extract to /tmp/noob
-d #create directories as necessary on extraction
-u #overwrite existing files

find . -print0 | cpio --null -pvd /tmp/noob
#PASSTHROUGH mode - this syntax will take all files in current directory (inlcuding those with space seperated names) and identitically copy them including paths to /tmp/noob

dd – bit by bit copy/delete

dd takes an input file and then produces a specified output file

dd if=INPUT of=OUTPUT bs=BLOCKSIZE count=COUNT

dd if=/dev/zero of/tmp/swapex bs=1M count=500
#creates a file with 500 x 1MByte blocks of zeros

dd if=/dev/sda of=/dev/sdb
#makes a bit by bit copy of device sda onto device sdb

dd if=/dev/sda of=device.iso
#makes an iso copy of device sda

gunzip

gunzip is a decompression tool to complement gzip. By default it will REPLACE the compressed file with the uncompressed version.

gunzip mrnoobot.txt.gz
#produces an uncompressed file mrnoobot.txt

gunzip -r /noobfiles
#decompresses zipped files in the noobfiles directory

gzip

gzip is a compression tool. By default it will REPLACE the target file with a compressed file

gzip mrnoobot.txt
#produces a compressed file mrnoobot.txt.gz

gzip -c mrnoobot.txt > mrnoobot.txt.gz
#compresses file to stdout and feeds into a separate file, leaving mrnoobot.txt in place

gzip -r /noobfiles
#compresses contents of noobfiles recursively if permissions exist

tar – tarball, ‘Tape Archive’

tar is used to create an archive file from multiple files

tar -cf noobarchive.tar ~/noobdocs
#archives all files from noobdocs to noobarchive.tar

tar -cfvz noobarchive.tar.gz ~/noobdocs
#as above but VERBOSE and compresses archive using GZIP

-j #compress with bzip2
-J #compress with xz

tar -tf noobarchive.tar.gz
#list contents of noobarchive.tar.gz

tar -xf noobarchive.tar.gz
#extract contents of noobarchive.tar.gz

tar -xf noobarchive.tar.gz -C noobdocs
#extract archive to directory noobdocs

unxz

decompression tool to complement xz.

unxz noobfile.txt.xz
#decompresses file to noobfile.txt

unzip

decompression tool to complement zip.

unzip noobarchive.zip
#decompresses zip archive, creating any necessary directories

unzip -l noobarchive.zip
#list contents of noobarchive.zip

xz

compression tool similar to gzip and bzip2. Compression is default mode
and will run with just the command typed.

xz -z *
#COMPRESSES all files in current directory

xz -d *
#DECOMPRESSES all files in current directory (see also unxz)

xzcat

utility to examine the contents of a file compressed with xz, as cat would with a normal file

xzcat noobfile.txt.xz
#displays contents of noobfile.txt.xz

zcat

utility to examine the contents of a file compressed with gzip, as cat would with a normal file

zcat noobfile.txt.gz
#displays contents of noobfile.txt.gz

zcat -l noobfile.txt.gz
#displays compression information of noobfile.txt.gz

zcat -f noobfile.txt
#displays contents of normal file noobfile.txt just like cat

zip

compression tool capable of handling multiple files and storing them in a single file. 

zip mrnoobot ~/noobdocs/*
#compresses all files from noobdocs into zip archive mrnoobot.zip

zip -r mrnoobot ~/noobdocs/
#recursively compresses all files and directories from noobdocs into zip archive mrnoobot.zip