Use archives within Linux CLI: Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разутвърден]; Категория:Linux Server)
Line 1: Line 1:
<noinclude><!--[[Category:Linux_Server|?]]-->{{ContentArticleHeader/Linux_Server}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Server}}</noinclude>


== Tar ==
== Tar ==
* Note <code class="noTypo">.tgz === .tar.gz</code>
* Note <code class="noTypo">.tgz === .tar.gz</code>
=== Create a gzip-compressed archive ===
=== Create a gzip-compressed archive ===
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
tar czvf archived-file.tar.gz /my/directory/or/file_1 /my/directory/or/file_2
tar czvf archived-file.tar.gz /my/directory/or/file_1 /my/directory/or/file_2
</syntaxhighlight>
</syntaxhighlight>
* <code class="noTypo">-c</code>, <code class="noTypo">--create</code> - create a new archive,
* <code class="noTypo">-c</code>, <code class="noTypo">--create</code> - create a new archive,
* <code class="noTypo">-z</code>, <code class="noTypo">--gzip</code> - filter the archive through gzip,
* <code class="noTypo">-z</code>, <code class="noTypo">--gzip</code> - filter the archive through gzip,
* <code class="noTypo">-v</code>, <code class="noTypo">--verbose</code> - verbosely list files processed,
* <code class="noTypo">-v</code>, <code class="noTypo">--verbose</code> - verbosely list files processed,
* <code class="noTypo">-f</code> - use archive file or device ARCHIVE - it must be the last option, otherwise you can use <code class="noTypo">--file=ARCHIVE</code>.
* <code class="noTypo">-f</code> - use archive file or device ARCHIVE - it must be the last option, otherwise you can use <code class="noTypo">--file=ARCHIVE</code>.
=== Extract a gzip-compressed archive ===
=== Extract a gzip-compressed archive ===
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
tar xzvf my-file.tar.gz
tar xzvf my-file.tar.gz
</syntaxhighlight>
</syntaxhighlight>
* <code class="noTypo">-x</code>, <code class="noTypo">--extract</code>, <code class="noTypo">--get</code> - extract files from an archive,
* <code class="noTypo">-x</code>, <code class="noTypo">--extract</code>, <code class="noTypo">--get</code> - extract files from an archive,
* The rest options are the same as the above,
* The rest options are the same as the above,
* the compression type (<code class="noTypo">-z</code> in this case) could be omitted, because tar will detect it automatically.
* the compression type (<code class="noTypo">-z</code> in this case) could be omitted, because tar will detect it automatically.


=== Extract bzip2-compressed archives ===
<syntaxhighlight lang="shell" line="1">
tar -xjf nextcloud-[version].tar.bz2
</syntaxhighlight>
== 7zip ==
== 7zip ==
7zip is not installed by default with most Linux distributions, so on Debian based distribution you can install it by the following command.
7zip is not installed by default with most Linux distributions, so on Debian based distribution you can install it by the following command.
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
sudo apt update && sudo apt install p7zip-full
sudo apt update && sudo apt install p7zip-full
</syntaxhighlight>
</syntaxhighlight>
IMO, 7zip is the best or at least the most easiest way to create password protected compressed archive within the command line.
IMO, 7zip is the best or at least the most easiest way to create password protected compressed archive within the command line.
=== Create a 7z-compressed archive ===
=== Create a 7z-compressed archive ===
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
Line 39: Line 35:
7z a archive.7z * -p'SECRET @#!'  # create password protected archive, append the password to the command
7z a archive.7z * -p'SECRET @#!'  # create password protected archive, append the password to the command
</syntaxhighlight>
</syntaxhighlight>
* <code class="noTypo">a</code> - create archive,
* <code class="noTypo">a</code> - create archive,
* <code class="noTypo">-p</code> - password protected [followed by the pass phrase].
* <code class="noTypo">-p</code> - password protected [followed by the pass phrase].
=== Extract a 7z-compressed archive ===
=== Extract a 7z-compressed archive ===
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
Line 50: Line 44:
7z x archive.7z -p'SECRET' -o'/otput/dir/'  # set an output directory
7z x archive.7z -p'SECRET' -o'/otput/dir/'  # set an output directory
</syntaxhighlight>
</syntaxhighlight>
* <code class="noTypo">x</code> - extract,
* <code class="noTypo">x</code> - extract,
* <code class="noTypo">-o</code> - output directory.
* <code class="noTypo">-o</code> - output directory.


== Unzip ==
<syntaxhighlight lang="shell" line="1">
unzip nextcloud-[version].zip
</syntaxhighlight>
== Reference ==
== Reference ==
* Ubuntu man pages: <code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/man1/tar.1.html tar]</code>
* Ubuntu man pages: <code class="noTypo">[https://manpages.ubuntu.com/manpages/jammy/man1/tar.1.html tar]</code>
* Stack Overflow: [https://stackoverflow.com/a/28160425/6543935 7-Zip command to create and extract a password-protected ZIP file on Windows?]
* Stack Overflow: [https://stackoverflow.com/a/28160425/6543935 7-Zip command to create and extract a password-protected ZIP file on Windows?]
*[http://www.putorius.net/2015/04/how-to-create-enrcypted-password.html How to Create an Encrypted (Password Protected) Tar or Zip Archive in Linux]
*[http://www.putorius.net/2015/04/how-to-create-enrcypted-password.html How to Create an Encrypted (Password Protected) Tar or Zip Archive in Linux]
*[http://superuser.com/a/162628/664884 How to password protect gzip files on the command line?]
*[https://superuser.com/a/162628/664884 How to password protect gzip files on the command line?]
*[http://superuser.com/questions/370389/how-do-i-password-protect-a-tgz-file-with-tar-in-unix How do I password protect a .tgz file with tar in Unix?]
*[https://superuser.com/questions/370389/how-do-i-password-protect-a-tgz-file-with-tar-in-unix How do I password protect a .tgz file with tar in Unix?]
* [https://askubuntu.com/q/615874/566421 <code class="noTypo">7zip</code> destination folder <code class="noTypo">-o</code>]
* [https://askubuntu.com/q/615874/566421 <code class="noTypo">7zip</code> destination folder <code class="noTypo">-o</code>]
* [https://sevenzip.osdn.jp/chm/cmdline/switches/output_dir.htm <code class="noTypo">7z -o</code> (set Output directory) switch]
* [https://sevenzip.osdn.jp/chm/cmdline/switches/output_dir.htm <code class="noTypo">7z -o</code> (set Output directory) switch]
<noinclude>
<noinclude>
<div id='devStage'>
<div id='devStage'>

Revision as of 05:52, 15 August 2022

Tar

  • Note .tgz === .tar.gz

Cre­ate a gzip-com­pressed archive

tar czvf archived-file.tar.gz /my/directory/or/file_1 /my/directory/or/file_2
  • -c, --create – cre­ate a new archive,
  • -z, --gzip – fil­ter the archive through gzip,
  • -v, --verbose – ver­bose­ly list files processed,
  • -f – use archive file or de­vice ARCHIVE – it must be the last op­tion, oth­er­wise you can use --file=ARCHIVE.

Ex­tract a gzip-com­pressed archive

tar xzvf my-file.tar.gz
  • -x, --extract, --get – ex­tract files from an archive,
  • The rest op­tions are the same as the above,
  • the com­pres­sion type (-z in this case) could be omit­ted, be­cause tar will de­tect it au­to­mat­i­cal­ly.

Ex­tract bzip2-com­pressed archives

tar -xjf nextcloud-[version].tar.bz2

7zip

7zip is not in­stalled by de­fault with most Lin­ux dis­tri­b­u­tions, so on De­bian based dis­tri­b­u­tion you can in­stall it by the fol­low­ing com­mand.

sudo apt update && sudo apt install p7zip-full

IMO, 7zip is the best or at least the most eas­i­est way to cre­ate pass­word pro­tect­ed com­pressed archive with­in the com­mand line.

Cre­ate a 7z-com­pressed archive

7z a archive.7z /directory/or/file # create 7z compressed archive
7z a archive.7z * -p               # create password protected archive, enter the password in prompt
7z a archive.7z * -p'SECRET @#!'   # create password protected archive, append the password to the command
  • a – cre­ate archive,
  • -p – pass­word pro­tect­ed [fol­lowed by the pass phrase].

Ex­tract a 7z-com­pressed archive

7z x archive.7z     # extract an archive in the current directory
7z x archive.7z -p  # extract a password protected archive in the current directory
7z x archive.7z -p'SECRET @#!'              # append the password to the command
7z x archive.7z -p'SECRET' -o'/otput/dir/'  # set an output directory
  • x – ex­tract,
  • -o – out­put di­rec­to­ry.

Un­zip

unzip nextcloud-[version].zip

Ref­er­ence