See this page as a slide show
Archival & Compression Commands
CS155 Archives
Archival and Compression: gzip, bzip2
- To save space we can compress files using two common compression tools.
- Gnu zip – a compression tool from GNU
gzip
: compress file
gunzip
: uncompress file
- Usually better compression, but slower:
bzip2
: compress file
bunzip2
: uncompress file
Compression Examples
% cp ~/pub/domains.txt .
% ls -l
total 8
-r-------- 1 cs155 class 4520 Dec 22 02:18 domains.txt
% gzip domains.txt
% ls -l
total 4
-r-------- 1 cs155 class 2530 Dec 22 02:18 domains.txt.gz
% gunzip domains.txt.gz
% bzip2 domains.txt
% ls -l
total 4
-r-------- 1 cs155 class 2481 Dec 22 02:18 domains.txt.bz2
bzip2
usually produces smaller files, but it also takes longer to run.
Archival and Compression: tar
- Store many files together as one file
(packages files together as a single archival file)
- Compresses files
- Can maintain directory structure or not
- Methods
tar
– tape archive
tar
allows us to put many files into a single file
while preserving the directory structure
zip
- works on Unix and Windows
- Archiving files using
tar
or zip
is a convenient way of
backing up files or sending a group of files to someone.
Archival and Compression: tar
tar
– tape archive
tar
allows us to put many files into a single file
while preserving the directory structure
- Archiving files using
tar
is a convenient way of backing up files
or sending a group of files to someone.
tar: Usage
- Usage:
tar
operation [options] files
- Operations:
- c – create an archive
- x – extract files from archive
- t – list files in archive
- Options:
- v – verbose output
- f – file name follows immediately as the next item
- j – filter through bzip2
- z – filter through gzip
tar: Examples
- Create a tar file from my_dir and my_file
tar -cvf my_tar.tar my_dir/ my_file
- List the contents of the created tar file
tar -tvf my_tar.tar
- Extract everything in the archive into the current directory
tar -xvf my_tar.tar
- Extract just
my_file2
into the current directory
tar -xvf my_tar.tar my_file2
Archival with Compression
Using tar
, we can filter archives through a compression program.
- Create a gzipped tar file of the directory my_files:
tar -zcvf my_tar.tgz my_files/
- Extract from a gzipped tar file into the current directory:
tar -zxvf my_tar.tgz
- Create a bzip2 tar file of the current directory:
tar -cjvf /tmp/my_tar.tbz .
zip: Usage
- Usage:
zip
operation [options] files
- Options:
- f - freshen: only changed files
- r - recurse into directories
- x - exclude the following names
- u - update: only changed or new files
- v - verbose: print info
- D - do not add directory entries
zip: Examples
- Create a zip file from files in my_dir and my_file
zip myArchive.zip my_dir/* my_file
- List the contents of the created zip file
unzip -l myArchive.zip
- Extract the contents into the directory temp_dir
unzip -d temp_dir myArchive.zip
- Append the file my_file2 to the archive
zip -u myArchive.zip my_file2
- Extract my_file2 into temp_dir
unzip -d temp_dir myArchive.zip my_file2