Work with Archive commands in Pisi Linux

tar

The command tar (tape archive) creates and extracts archives of file and directories. The archive .tar is uncompressed, a compressed archive has the extension .tgz or.tar.gz (zip) or.tbz (bzip2).

Do not use absolute path when creating an archive, you probably want to unpack it somewhere

else. Some typical commands are:

Create

# cd /
# tar -cf home.tar home/                                                                     # archive the whole /home directory (c for create)
# tar -czf home.tgz home/                                                                   # same with zip compression
# tar -cjf home.tbz home/                                                                    # same with bzip2 compression

Only include one (or two) directories from a tree, but keep the relative structure. For example archive /usr/local/etc and /usr/local/www and the first directory in the archive should be local/.

# tar -C /usr -czf local.tgz local/etc local/www
# tar -C /usr -xzf local.tgz                                                                         # To untar the local dir into /usr
# cd /usr; tar -xzf local.tgz                                                                      # Is the same as above

Extract

To extract a tar.gz compressed archive in Pisi Linux you can use the following command

tar -zxvf  tar-archive-name.tar.gz

# tar -tzf home.tgz                                                                                   # look inside the archive without extracting (list)
# tar -xf home.tar                                                                                   # extract the archive here (x for extract)
# tar -xzf home.tgz                                                                                 # same with zip compression (-xjf for bzip2 compression)
# remove leading path gallery2 and extract into gallery
# tar –strip-components 1 -zxvf gallery2.tgz -C gallery/
# tar -xjf home.tbz home/colin/file.txt                                                       # Restore a single file
# tar -xOf home.tbz home/colin/file.txt                                                      # Print file to stdout (no extraction)

More advanced

# tar c dir/ | gzip | ssh user@remote ‘dd of=dir.tgz’                                            # arch dir/ and store remotely.
# tar cvf – `find . -print` > backup.tar                                                                     # arch the current directory.
# tar -cf – -C /etc . | tar xpf – -C /backup/etc                                                          # Copy directories
# tar -cf – -C /etc . | ssh user@remote tar xpf – -C /backup/etc                         # Remote copy.
# tar -czf home.tgz –exclude ‘*.o’ –exclude ‘tmp/’ home/

zip/unzip

Zip files can be easier to share with Windows.

# zip -r fileName.zip /path/to/dir                                 # zip dir into file fileName.zip
# unzip fileName.zip                                                       # uncompress zip file
# unzip -l fileName.zip                                                   # list files inside archive
# unzip -c fileName.zip fileinside.txt                          # print one file to stdout (no extraction)
# unzip fileName.zip fileinside.txt                              # extract one file only

Gzip

gzip is standard and widely used file compression and decompression utility. Gzip allows file concatenation. Compressing the file with gzip, outputs the tarball which is in the format of ‘*.tar.gz‘ or ‘*.tgz‘.

gzip options

  1. –stdout                                                 Produce output on standard output.
  1. –to-stdout                                            Produce output on standard output.
  1. –decompress                                       Decompress File.
  1. –uncompress                                       Decompress File.
  1. -d                                                         Decompress File.
  1. -f                                                          Force Compression/Decompression.

gzip Examples

Create an ‘gzip’ archive file.

# tar -cvzf name_of_archive.tar.gz /path/to/folder

To extract a ‘gzip’ archive file.

# gunzip file_name.tar.gz

The above command must be passed followed with below command.

# tar -xvf file_name.tar

cpio

cpio stands for Copy in and out. Cpio is a general purpose file archiver for Linux. It is actively used by RedHat Package Manager (RPM) and in the initramfs of Linux Kernel as well as an important archiving tool in Apple Computer’s Installer (pax).

Options

     -0                                      Read a list of filenames terminated by a null character instead of a newline.
  1. -a                                        Reset Access time.
  1. -A                                        Append.
  1. -b                                         swap.
  1. -d                                        Make Directories.

cpio Examples

Create an ‘cpio’ archive file.

# cd groni

# ls file1.o file2.o file3.o

# ls | cpio -ov > /path/to/output_folder/obj.cpio

To extract a cpio archive file.

# cpio -idv < /path/to folder/obj.cpio