How to Use 7zip Archiver on Linux Command Line

Channel: Linux
Abstract: $ 7z a output.7z output1 output2 output3$ 7z a output.zip output1 output2 output3

If you are a windows user, you'd have definitely used the 7zip file archiver. It not only has the highest compression ratio but it also supports a host of archive formats like 7z, LZMA2, XZ, ZIP, Zip64, CAB, RAR, and more. In this article, we will discuss how to use the 7zip archiver on Linux command line.

If 7zip is not currently installed on your system, just install it through your favourite command line download manager. For example, I used the following command to install it :

$ sudo apt-get install p7zip-full

Once the package is installed, the '7z' command should do the archiving and extraction work. In this article, we will discuss how to use the 7zip archiver (the one that has the highest compression ratio) on the Linux command line.

How To Unrar Rar File On Mac

To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video

How To Unrar Rar File On Mac Testing Environment

Here are the details of the testing environment used for this article :

  • OS – Ubuntu 13.04
  • Shell – Bash 4.2.45
  • Application – p7zip-full 9.20.1~dfsg.1-4
Some Examples

Before we begin with the examples, a point worth noting is that the 7z command provides function letters that work like command line options but are used without hash (-). These function letters tell the command about the function it is required to do.

Here are some examples of using 7zip on Linux command line :

1) Create a simple .7z archive using the function letter 'a'

To create a .7z archive, use a command like this :

$ 7z a output.7z output1 output2 output3

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)
Scanning

Creating archive output.7z

Compressing output1
Compressing output2
Compressing output3

Everything is Ok

So you can see that the 7z command is passed the function letter 'a' followed by the name of the archive to be created, followed by the name of the file(s) to be included in the archive. Once this command is executed, an archive with the same name is produced.

Here is how you can verify the same:

$ ls output.7z
output.7z

Not only 7z archives, you can also create other archives. For example, lets create a .zip archive :

$ 7z a output.zip output1 output2 output3

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)
Scanning

Creating archive output.zip

Compressing output1
Compressing output2
Compressing output3

Everything is Ok

$ ls output.zip
output.zip
2) Extract an archive using the function letter 'e'

Just like 'a' is used to add files to an archive, the function letter 'e' can be used to extract an archive.

For example, lets extract the output.7z archive we created in the last example :

$ 7z e output.7z

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Processing archive: output.7z

Extracting output1
Extracting output2
Extracting output3

Everything is Ok

Files: 3
Size: 101
Compressed: 248

$ ls output*
output1 output2 output3 output.7z

So you can see that the files were successfully extracted from the archive.

3) List the details of an archive using function letter 'l'

If you ever want to list the details of an archive, you can use pass its name as an argument to the 7z command along with the function letter 'l'.

Here is an example :

$ 7z l output.7z

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Listing archive: output.7z

--
Path = output.7z
Type = 7z
Method = LZMA
Solid = +
Blocks = 1
Physical Size = 248
Headers Size = 176

Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2013-08-19 11:25:32 ....A 18 72 output1
2013-08-15 21:49:55 ....A 13 output2
2013-08-15 21:51:00 ....A 70 output3
------------------- ----- ------------ ------------ ------------------------
101 72 3 files, 0 folders

So you can see that details of the archive contents were listed in the output.

4) Update an archive using function letter 'u'

Suppose you create an archive of a folder (that contains some files).

$ ls
new_dir

$ 7z a new.7z new_dir/

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)
Scanning

Creating archive new.7z

Compressing new_dir/output1
Compressing new_dir/output2
Compressing new_dir/output3

Everything is Ok

$ ls
new.7z new_dir

Now, after some time you add some new files to the folder and want to add them to the archive too. So, instead of creating a new archive, you can update the existing the archive in the following way :

$ 7z u new.7z new_dir/

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Scanning

Updating archive new.7z

Everything is Ok

So this way you can update an archive.

5) Test the integrity of an archive using the function letter 't'

Here is an example of how you can test the integrity of an archive :

$ 7z t new.7z

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Processing archive: new.7z

Testing new_dir/output1
Testing new_dir/output2
Testing new_dir/output3
Testing new_dir/output4
Testing new_dir

Everything is Ok

Folders: 1
Files: 4
Size: 101
Compressed: 276

But if there is some problem, the integrity check is really helpful. Here is an example :

$ 7z t new.7z

7-Zip [64] 9.20 Copyright (c) 1999-2010 Igor Pavlov 2010-11-18
p7zip Version 9.20 (locale=en_IN,Utf16=on,HugeFiles=on,2 CPUs)

Processing archive: new.7z

Error: Can not open file as archive

For all the other details, please read the man page of this command.

Read Also:
  • How to Use Linux gunzip Command with Examples
  • 20 Examples for Linux Zip and UnZip Command

Ref From: linoxide
Channels:

Related articles