Walker News

Linux File Archiving And Compression Commands By Examples

Here are some examples of file compression and archiving commands, which include the GNU tar, cpio, gzip, and zip commands.

cpio command examples:
To create a cpio archive for all files and directories, including the symbolic links and sub-directories, in the current working directory:
find . -depth -print | cpio -ocvB -O /tmp/myhome.cpio

To create a cpio archive and apply gzip compression on the fly:
find . -depth -print | cpio -ocvB | gzip -c >/tmp/myhome.cpio.gz

To archive only dir1 and dir2 directories, including all files and sub-directories in them:
find dir1 dir2 -depth -print | cpio -ocvB -O /tmp/mydir1dir2.cpio

To view or examine cpio archive file content:
cat /tmp/myhome.cpio | cpio -t

zcat /tmp/myhome.cpio.gz | cpio -t

To extract only particular file, let’s say dir1/file1, from cpio archive to current working directory:
cat /tmp/myhome.cpio | cpio -icvdBum dir1/file1

zcat /tmp/myhome.cpio.gz | cpio -icvdBum  dir1/file1

To extract all files to current working directory:
cat /tmp/myhome.cpio | cpio -icvdBum

zcat /tmp/myhome.cpio.gz | cpio -icvdBum

tar command examples:
To create a tar archive for all files and directories, including the symbolic links and sub-directories, in the current working directory:
tar -cvpf /tmp/myhome.tar *

To create compressed tar archive using gzip:
tar -zcvpf /tmp/myhome.tgz *
The convention of naming gzip-compressed tar archive is using either .tgz or .tar.gz suffix.

Besides gzip, the GNU tar command also supports bzip2 compression:
tar -jcvpf /tmp/myhome.tar.bz2 *

To archive only dir1 and dir2 directories:
tar -zcvpf /tmp/mydir1dir2.tgz dir1 dir2

To view the tar archive (compressed and uncompressed archive):
tar -tvf /tmp/myhome.tar

tar -ztvf /tmp/myhome.tgz

tar -jtvf /tmp/myhome.tar.bz2

To extract only particular file from tar archive:
tar -xvf /tmp/myhome.tar dir1/file1

tar -zxvf /tmp/myhome.tgz dir1/file1

tar -jxvf /tmp/myhome.tar.bz2 dir1/file1

If without specifying dir1/file1, the tar -x option extracts all files in the archive, e.g.:
tar -zxvf /tmp/myhome.tgz

zip command examples:
Using zip command to create a compressed archive for dir1 and dir2 directories:
zip /tmp/mydir1dir2.zip dir1/* dir2/*

To create password-protected zip file (i.e. using a password to secure the zip file):
zip -e /tmp/dir1.zip dir1/*

To view the content of zip file:
unzip -l /tmp/dir1.zip

To extract only particular file from a zip archive:
unzip /tmp/dir1.zip dir1/file1

Without specifying dir1/file1 in previous example, it simply means unzip the zip file (i.e. extracts all files).

gzip command examples
To compress a file (after gzip completes, file1 becomes file1.gz file):
gzip file1

To uncompress a gz file (once gunzip completes, file1.gz becomes file1 file):
gunzip file1.gz
A gzipped-compressed file must have a .gz file extension (suffix). Otherwise, gunzip refuses to uncompress the gzipped file.

To view content of a gzipped ASCII file without uncompress it:
zcat file1.gz

Custom Search

2017  •  Privacy Policy