Advertisement
v1ral_ITS

7zip Full Command Line Tutorial

May 11th, 2018
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.79 KB | None | 0 0
  1.  7-Zip (http://www.7-zip.org/) is a file archiver with highest compression ratio.The tool supports various archive formats such as LZMA2, XZ, ZIP, Zip64, CAB, RAR, ARJ, GZIP, BZIP2, TAR, CPIO, RPM, ISO, most file system images and DEB formats. Developer claims that the compression ratio in the new 7z format is 30-50% better than the ratio in Zip formats.
  2.  
  3. In this article, we will walk through the practical examples of 7zip utility and their usage.All the below examples have been tested on Ubuntu 16.04 LTS/16.10 and Linux Mint 18 , and the 7zip version we have used is 9.20
  4. Installation
  5.  
  6. The 7zip utility is not pre-installed on your Ubuntu systems. You have to install it in your system using the following commands.
  7.  
  8. $ sudo apt-get install p7zip-full
  9.  
  10. NOTE:In case, you want to use 7zip with rar files, then you’ll have to install the following package as well.
  11.  
  12. $ sudo apt-get install p7zip-rar
  13.  
  14. Usage/Functions
  15.  
  16. In this section, we will discuss the usage of the 7zip tool and the basic features it provides.
  17.  
  18. Basic syntax of 7zip:
  19.  
  20. $ 7z [adeltux] [-] [SWITCH] <ARCHIVE_NAME> <ARGUMENTS>..
  21.  
  22. We will be using the files displayed in the following screenshot for performing various operations using 7zip.
  23.  
  24. ls-command-output
  25. Create new archive/ add files to existing archive
  26.  
  27. Using the tool, you can package the files in a .7z file. This feature can be accessed using the ‘a’ function letter.
  28.  
  29. Here’s how you can use this feature:
  30.  
  31. $ 7z a [archived-filename] [names-of-files-to-be-archived]
  32.  
  33. For example:
  34.  
  35. $ 7z a testfiles.7z *
  36.  
  37. add-files-in7zip-compressed-file
  38.  
  39. Now to check, whether the archive file is created or not, you can use the ‘ls‘ command.
  40.  
  41. add-ls-7zip-command
  42.  
  43. As you can see in the screenshot above that a .7z archive file (testfiles.7z) has been created. Of course, you can also create/update, say, a .zip file this way.
  44. List contents of archive
  45.  
  46. You can use the ‘l’ function letter for listing the contents of an archived file.
  47.  
  48. $ 7z l [archived-filename]
  49.  
  50. For example:
  51.  
  52. $ 7z l testfiles.7z
  53.  
  54. Here is the list of files in testfiles.7z archived file.
  55.  
  56. listdelete-7zip-command
  57. Delete files from an archive
  58.  
  59. Using the tool, you can also delete a file from an archive file, something which you can do using the ‘d’ function letter.
  60.  
  61. $ 7z d [archived file] [name-of-file-to-be-deleted]
  62.  
  63. For example, to delete testfile1, we can use the following command:
  64.  
  65. $ 7z d testfiles.7z testfile1
  66.  
  67. delete-file-from-7zip-compressed-file
  68. Extracting files from archive file
  69.  
  70. With the help of 7zip, you can extract files from an already existing archive. This feature can be accessed using the ‘e’ function letter.
  71.  
  72. $ 7z e [archived-file]
  73.  
  74. For example, suppose we want to extract the ‘testfiles.7z’ archive.
  75.  
  76. ls-extract-7zip
  77.  
  78. Here’s how that can be done:
  79.  
  80. $ 7z e testfiles.7z
  81.  
  82. extract-7zip-file
  83. Updating existing archived files
  84.  
  85. If you want, you can add new files to a previously created archived file, or you can even update the contents of existing files in the archive. This is made possible using the ‘u’ function letter.
  86.  
  87. $ 7z u [archived-file] [name-of-new-or-updated-file]
  88.  
  89. For example, here is the directory structure in which we will perform some update operations.
  90.  
  91. directorystructure-linux
  92.  
  93. Let’s say we have already created an archive of the ‘dir1’ directory. And now, due to some modifications in, let’s say, file1, file2 and file3, we want to update the archive.
  94.  
  95. So, instead of individually adding the modified files again (using the ‘a’ function letter) to the archive, we can perform the operation in a single run by using the following command.
  96.  
  97. $ 7z u dir1.7z
  98.  
  99. update-7zipfile-linux-command-line
  100. Testing integrity of archived files
  101.  
  102. Using the tool, you can also check the integrity of an already existing archive file. Testing integrity is very important, as it tells, whether the contents are properly archived or not.
  103.  
  104. Sometimes, due to a hardware problem, RAM problem, or heating problem, your files may get affected and the archive isn’t created properly. To check whether the archive is corrupted or not, you can use the ‘t’ function letter.
  105.  
  106. $ 7z t [archived-file]
  107.  
  108. For example:
  109.  
  110. $ 7z t testfiles.7z
  111.  
  112. Here is the output
  113.  
  114. integrity-check-7zip-linux
  115.  
  116. Above output shows that the contents of the archive are not corrupted.
  117. Conclusion
  118.  
  119. 7 zip tool happens to be one of the best tools for archiving files. It’s available for both Windows and Linux, although only a command line port is available for the latter platform, so a good option for those Linux users whose work involves playing with files on the command line.
  120.  
  121. What we’ve discussed in this tutorial are only basic features. There are also several advanced options available. Head to the tool’s man page (man 7z) for more information and details.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement