Ubuntu / Linux

Decompression

.zip

# decompress   .zip  folder (as used in Windows)

unzip samples.zip

.gz

# decompress  .gz  file (compressed .gz file will be removed after decompression)

gzip -d sample.fastq.gz

  sample.fastq

# view the content of a .gz file

zcat sample.fastq.gz

# view top 20 line  - pass decompressed content via unix pipe to head command

zcat sample.fastq.gz | head -20 

.tar.gz

# tar   decompress .tar.gz files

tar -zxvf samples.tar.gz

# unix pipe  pass decompressed file into a pipe: extract all tar archive files to standard output (option -O)

tar -zxOf samples.tar.gz | head -20   # views the first 20 lines

Compression

Compress a single file

# compress single file as file.gz using gzip (original file will be removed after compression)

gzip sample.fastq

 sample.fastq.gz

Compress complete directory

# compress complete folder as tar.gz archive (recommended standard for working with Ubuntu/Linux)

tar -zcvf samples_compressed.tar.gz /path/to/sample/directory/

# compress a complete folder as single zip file for using in Windows (not gzip)

zip -r samples.zip sample_folder/

# to change gzip level used in tar archive (default compression level is 6, max level is 9)

# a) providing the compression command by option -I

tar -I 'gzip -9' -cvf samples_compressed.tar.gz sample_directory/

# b) combine tar and gzip using a unix pipe

tar cvf - sample_directory/  | gzip -9 > samples_compressed.tar.gz

Extract single file from tar archive

# list content (all files) of a .tar.gz archive

tar -tf samples.tar.gz

  sample_1.fastq

  sample_2.fastq

# extract selected file from .tar.gz archive

tar -zxvf samples.tar.gz sample_1.fastq

  sample_1.fastq

# extract selected folder from .tar.gz archive

tar -zxvf samples.tar.gz  data/projectA/fastq/

  data/projectA/fastq/sample_R1.fastq

  data/projectA/fastq/sample_R2.fastq


Install zip and unzip

sudo apt install zip unzip

see also

 → www.gzip.org

 → bzip2

How to combine multiple files into an tar archive file (Indiana University)

https://kb.iu.edu/d/acfi