Difference between revisions of "Linux archives"

From Ever changing code
Jump to navigation Jump to search
Line 1: Line 1:
= TAR, GZ, BZIP =
= TAR, GZ, BZIP =
Working with archives in linux where '''tar = tape archive''' and the most common used options are:
Working with archives in linux where </code>tar = tape archive</code> and the most common used options are:
:'''c''' - ''create'' a new tar file  
*<code>-c</code> - ''create'' a new tar file  
:'''x''' - ''extract'' file
*<code>-x</code> - ''extract'' file
:'''t''' - ''list'' the contents of an archive
*<code>-t</code> - ''test'' list the contents of an archive
:'''z''' - ''compress'' use gzip compress, extention '''.tar.gz'''
*<code>-z</code> - ''compress'' use gzip compress, extention </code>.tar.gz</code>
:'''j''' - ''compress'' use bzip2 compress, extention '''.tar.bz2'''
*<code>-j</code> - ''compress'' use bzip2 compress, extention </code>.tar.bz2</code>
:'''v''' - ''verbose'' displays files to compress or uncompress
*<code>-v</code> - ''verbose'' displays files to compress or uncompress
:'''f''' - ''file'' specify the new archive name or an archive to extract from
*<code>-f</code> - ''file'' specify the new archive name or an archive to extract from
*<code>-r</code> - flag to append the additional files to the archive
*<code>-u</code> - ''updating'' a file
===Compress===
===Compress===
Create a compressed .tar.gz archive
Create a compressed .tar.gz archive

Revision as of 13:52, 27 October 2018

TAR, GZ, BZIP

Working with archives in linux where tar = tape archive and the most common used options are:

  • -c - create a new tar file
  • -x - extract file
  • -t - test list the contents of an archive
  • -z - compress use gzip compress, extention .tar.gz
  • -j - compress use bzip2 compress, extention .tar.bz2
  • -v - verbose displays files to compress or uncompress
  • -f - file specify the new archive name or an archive to extract from
  • -r - flag to append the additional files to the archive
  • -u - updating a file

Compress

Create a compressed .tar.gz archive

#archive a directory and everything under (recursively), the dir tree will be preserved eg. /dir/to/be/compressed/ when you extract
tar -czvf archive.tar.gz /dir/to/be/compressed/
tar -czvf archive.tar.gz /path/to/*.conf #archive only files matching *.conf

Create a compressed archive controlling directory tree structure

tar -czvf archive.tar.gz -C directory/level1/ .
# -C change current working directory
# .  archive all files from current working directory, this way when you extract it will extract to to the current working dir

Compress each directory in current directory into separate zip

$ for i in */; do echo tar -czvf "${i%/}.tar.gz" "$i"; done #test
tar -czvf hosting.tar.gz hosting/
tar -czvf incubator.tar.gz incubator/
tar -czvf secrets.tar.gz secrets/
$ for i in */; do      tar -czvf "${i%/}.tar.gz" "$i"; done #run

Extract

tar -xzvf archive.tar.gz

A single file from compressed .tar.gz

tar -xzvf archive.tar.gz myscript.txt

A single directory (here conf directory) from compressed .tar.gz

tar -xzvf archive.tar.gz conf

Preview files inside archive

tar -tzvf {.tar.gz}
tar -tjvf {.tbz2}

Zip

Install zip and unzip then use interactive method:

Extract using default options, retaining directory structure
unzip file.zip
Compress file or directory
zip name.zip directory/*
zip name.zip file1 file2 file3

'Compress each directory in current directory into separate zip

$ for i in */; do echo zip -r "${i%/}.zip" "$i"; done # Test
$ for i in */; do      zip -r "${i%/}.zip" "$i"; done # Run