Skip to content

Instantly share code, notes, and snippets.

@TomyJaya
Last active February 11, 2016 15:55
Show Gist options
  • Save TomyJaya/7eb8da26c7eef2da2082 to your computer and use it in GitHub Desktop.
Save TomyJaya/7eb8da26c7eef2da2082 to your computer and use it in GitHub Desktop.
Cheatsheet to working with unix archival and compression

.tar vs .gz vs .zip

  • .tar: archiving (putting files together into 1 folder) in Unix
  • .gz: compression in Unix
  • .tar.gz: archiving + compression in Unix
  • .zip: archiving + compression in Windows

Archiving as .tar

To compress a directory:

tar -cvf <archive_name>.tar <directory_to_compress>

e.g.

tar -cvf test.tar tar-experiment

To combine multiple files and/or directories into a single file, use the following command:

tar -cvf <archive_name>.tar <file_1> <file_2> <to> <file_n>

e.g.

tar -cvf test.tar test1.txt test2.txt test3.txt

To extract the file:

tar -xvf archive_name.tar

e.g.

tar -xvf test.tar 

Compressing as .gz

Compress a file:

gzip -c <file_name>.txt > <file_name>.txt.gz

e.g.

gzip -c test.txt > test.txt.gz

Deflate/ Extract a file:

gzip -d <file_name>.gz>

e.g.

gzip -d file.gz

Directly working with .tar.gz

To archive and compress a directory:

tar -zcvf <archive_name>.tar.gz <directory_to_compress>

e.g.

tar -zcvf test.tar.gz test.txt

To decompress and extract the file:

tar -zxvf <archive_name>.tar.gz

e.g.

tar -zcvf test.tar.gz test.txt

zgrep & zcat

zgrep: grep gzipped files zcat: cat gzipped files

zgrep

zgrep <pattern> <file_name>.gz 

e.g.

zgrep Tomy test.txt.gz

zcat

zcat <file_name>.gz | more

Regardless of zipped or not

zcat -f <file_name> | more 

Using log4j RollingAppender to roll as .gz files

  1. Add the below Maven Dependency
<dependency>
	<groupId>log4j</groupId>
	<artifactId>apache-log4j-extras</artifactId>
	<version>1.2.17</version>
</dependency>
  1. Use the sample log4j.xml below (Feel free to copy paste):
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">
	<appender name="default.file" class="org.apache.log4j.rolling.RollingFileAppender">
		<rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy">
			<param name="ActiveFileName" value="./logs/HomeyFood.log" />
			<param name="FileNamePattern" value="./logs/HomeyFood.log.%d{yyyyMMdd}.gz" />
		</rollingPolicy>
		<param name="append" value="true" />
		<layout class="org.apache.log4j.PatternLayout">
			<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n" />
		</layout>
	</appender>
	<appender name="async" class="org.apache.log4j.AsyncAppender">
		<param name="BufferSize" value="5000" />
		<appender-ref ref="default.file" />
	</appender>
	<root>
		<level value="info" />
		<appender-ref ref="async" />
	</root>
</log4j:configuration>

Source:

  1. http://unix.stackexchange.com/questions/46786/how-to-tell-gzip-to-keep-original-file
  2. http://www.cyberciti.biz/faq/linux-unix-bsd-extract-targz-file/
  3. http://www.simplehelp.net/2008/12/15/how-to-create-and-extract-zip-tar-targz-and-tarbz2-files-in-linux/
  4. http://askubuntu.com/questions/122141/whats-the-difference-between-tar-gz-and-gz-or-tar-7z-and-7z
  5. http://stackoverflow.com/questions/11534918/are-tar-gz-and-tgz-the-same-thing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment