Archive Command

Quick note for compress files

There are many types of compress file, with each type we should have different command to interact with it. To find which archive type we are working with, use file command.

$ file a.zip
a.zip: Zip archive data, at least v1.0 to extract

$ file b.tar
b.tar: POSIX tar archive (GNU)

$ file c.tar.gz
c.tar.gz: gzip compressed data, last modified: Tue Aug 20 04:33:47 2019, from Unix

Here are some most common types i’ve been working with recently.

Zip

# Compress
$ zip a.zip file1 file2 file3 # from individual files

$ zip -r a.zip dir_name       # from a folder, -r = recursive

# Decompress
$ unzip -d directory a.zip

-j | –junk-paths => Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

# Generate source.zip file which contains all hdl files in this folder and its sub-folder
$ find . -name "*.hdl" | zip -j source -@

Tar

# Compress
$ tar cvf b.tar file1 file2

# Decompress
$ tar xvf b.tar

Tar.gz

# Compress
$ tar czvf c.tar.gz file1 file2

# Decompress
$ tar xzvf c.tar.gz

Note

  • c compress | x extract

  • f file

  • z gz

  • v verbose (optional for debugging)

  • You can compress without declaring the extension .zip, .tar, or .tar.gz. The former will automatically put .zip into a new file, however it’s not true for the laters. When in doubt of archive file type, use command file.

Reference


See also