Compress | Decompress | Linux

Linux Apr 5, 2022

Zip

The -r option is used to recursively zip files. This option will compress all the files present within a folder. An example of such command is as shown below:

zip –r sampleZipFile.zip MyDirectory

Exclude Files in a Zip (Here ExampleFile.txt will not be added to the sampleZipFile.zip.)

zip -x sampleZipFile.zip ExampleFile.txt

Zip with Password

zip -re OUTPUT_FILE.zip FOLDER

Unzip to a Different Directory

unzip sampleZipFile.zip -d /usr/sampleZip/ExampleDir

Use Linux Unzip with Multiple Zip Files

unzip ‘*.zip’

Suppress Output When Using Unzip in Linux (In case you want to suppress these messages, then you can use the -q option. The command would be as shown below:)

unzip -q sampleZipFile.zip

Exclude Files Using Unzip in Linux

unzip sampleZipFile.zip -x excludedFile.txt
unzip sampleZipFile.zip -x "*.png/*"

Using Unzip in Linux with Password Protected Files

unzip -P Password sampleZipFile.zip

Overriding Zip Files

[y]es, [n]o, [A]ll, [N]one, [r]ename

You can override these files by using the -o options

unzip -o sampleZipFile.zip

Using Linux Unzip Without Overwriting Files

unzip -n sampleZipFile.zip

How to List the Content of a Zip in Linux

unzip -l sampleZipFile.zip

Tar

Options

Options:
-c : Creates Archive
-x : Extract the archive
-f : creates archive with given filename
-t : displays or lists files in archived file
-u : archives and adds to an existing archive file
-v : Displays Verbose Information
-A : Concatenates the archive files
-z : zip, tells tar command that creates tar file using gzip
-j : filter archive tar file using tbzip
-W : Verify a archive file
-r : update or add file or directory in already existed .tar file

This command creates a tar file called file.tar which is the Archive of all .c files in current directory.

tar cvf file.tar *.c

gzip compression on the tar Archive, using option -z

tar cvzf file.tar.gz *.c

Extracting files from Archive using option -xvf : This command extracts files from Archives.

tar xvf file.tar

Extracting a gzip tar Archive *.tar.gz using option -xvzf

tar xvzf file.tar.gz

Creating compressed tar archive file in Linux using option -j : This command compresses and creates archive file less than the size of the gzip. Both compress and decompress takes more time then gzip.

tar cvfj file.tar.tbz example.cpp

Untar single tar file or specified directory in Linux

tar xvfj file.tar -C path of file in directory 

Untar multiple .tar, .tar.gz, .tar.tbz file in Linux : This command will extract or untar multiple files from the tar, tar.gz and tar.bz2 archive file. For example the above command will extract “fileA” “fileB” from the archive files.

tar xvf file.tar "fileA" "fileB" 

or

tar zxvf file1.tar.gz "fileA" "fileB"

or

tar jxvf file2.tar.tbz "fileA" "fileB"

Check size of existing tar, tar.gz, tar.tbz file in Linux : The above command will display the size of archive file in Kilobytes(KB).

tar czf file.tar | wc -c

Update existing tar file in Linux

tar rvf file.tar *.c

list the contents and specify the tarfile using option -tf

tar tf file.tar

Applying pipe to through ‘grep command’ to find what we are looking for

tar tvf file.tar | grep "text to find" 

Viewing the Archive using option -tvf

tar tvf file.tar

To search for an image in .png format

tar tvf file.tar --wildcards '*.png' 

Zst - sudo apt install zstd

The extention .zst means that the archive is compressed by zstd.

tar --use-compress-program=unzstd -xvf archive.tar.zst
tar -I zstd -xvf archive.tar.zst
unzstd yourfilename.zst

Simple

zstd file.txt 

Decompress

zstd -d file.txt.zst
unzstd file.txt.zst

If you want to compress a directory, or combine multiple files into a single archive, you’ll need to use tar to create an archive and then compress it with zstd. You’ll need to add the --zstd option, along with any other flags you choose.

tar --zstd -cf archive.tar.zst /home/linuxnightly

Alternatively, we could use the a option with the tar command, which will choose the correct compression method based on the file extension (zst in this case) specified.

tar acf archive.tar.zst /home/linuxnightly

Use the following tar command to open a Zstandard tarball.

tar --zstd -xf archive.tar.zst

Alternatively, the a option can save us a few keystrokes again.

tar axf archive.tar.zst

More here

https://linuxnightly.com/how-to-use-zstandard-compression-on-linux-with-commands/

Gunzip Gzip

To Decompress A File Using The "gunzip" Command:

gunzip myfilename.gz

Force A File To Decompress:

gunzip -f myfilename.gz

To keep both the compressed and decompressed file:

gunzip -k myfile.gz

To display compressed output:

nzip -l myfile.gz

Decompressing Lots Of Files Recursively:

gunzip -r /tmp/

Tags

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.