Use archives within Linux CLI: Difference between revisions

From WikiMLT
m (Стадий: 3 [Фаза:Разработване, Статус:Разутвърден]; Категория:Linux Server)
m (Стадий: 6 [Фаза:Утвърждаване, Статус:Утвърден]; Категория:Linux Server)
 
(9 intermediate revisions by the same user not shown)
Line 22: Line 22:
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
tar -xjf nextcloud-[version].tar.bz2
tar -xjf nextcloud-[version].tar.bz2
</syntaxhighlight>
=== Extract certain file from the archive directory tree ===
<syntaxhighlight lang="shell" line="1">
tar --strip-components=6 -xvf "data.tar.zst" \
./usr/lib/x86_64-linux-gnu/nautilus/extensions-3.0/libnautilus-share.so
</syntaxhighlight>
</syntaxhighlight>
== 7zip ==
== 7zip ==
Line 47: Line 53:
* <code class="noTypo">-o</code> - output directory.
* <code class="noTypo">-o</code> - output directory.


== Unzip ==
== Zip and Unzip ==
Create an recursive archive for a directory.<syntaxhighlight lang="shell" line="1">
zip -r "archive-name.zip" "./directory-to-be-archived/"
</syntaxhighlight>Extract archive.<syntaxhighlight lang="shell" line="1">
unzip "archive-name.zip"
</syntaxhighlight>
 
== Rar and Unrar ==
Some times the Linux archive managers can't handle properly RAR archives - for example look at this topic [https://askubuntu.com/q/912692/566421 Extract files within an .exe file]. Here is how to extract such files.<syntaxhighlight lang="shell" line="1">
sudo apt install unrar
</syntaxhighlight>
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
unzip nextcloud-[version].zip
unrar x "PracticalMalwareAnalysis-Labs.exe"
unrar x "archive-name.rar"
</syntaxhighlight>
</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/845601/6543935 '''How do I untar a subdirectory into the current directory?''']
*Unix and Linux: [https://unix.stackexchange.com/q/61461/201297 How to extract specific file(s) from tar.gz]
* 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]
Line 63: Line 82:
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Server
  | Стадий = 3
  | Стадий = 6
  | Фаза  = Разработване
  | Фаза  = Утвърждаване
  | Статус = Разутвърден
  | Статус = Утвърден
  | ИдтПт  = Spas
  | ИдтПт  = Spas
  | РзбПт  = {{REVISIONUSER}}
  | РзбПт  = Spas
  | АвтПт  = Spas
  | АвтПт  = Spas
  | УтвПт  = Spas
  | УтвПт  = {{REVISIONUSER}}
  | ИдтДт  = 7.07.2022
  | ИдтДт  = 7.07.2022
  | РзбДт  = {{Today}}
  | РзбДт  = 17.09.2022
  | АвтДт  = 15.08.2022
  | АвтДт  = 24.09.2022
  | УтвДт  = 15.08.2022
  | УтвДт  = {{Today}}
  | ИдтРв  = [[Special:Permalink/27794|27794]]
  | ИдтРв  = [[Special:Permalink/27794|27794]]
  | РзбРв  = {{REVISIONID}}
  | РзбРв  = [[Special:Permalink/31603|31603]]
  | АвтРв  =  
  | АвтРв  = [[Special:Permalink/31793|31793]]
  | РзАРв  = [[Special:Permalink/30177|30177]]
  | РзАРв  = [[Special:Permalink/31401|31401]]
  | УтвРв  =  
  | УтвРв  = {{REVISIONID}}
  | РзУРв  = [[Special:Permalink/30179|30179]]
  | РзУРв  = [[Special:Permalink/31403|31403]]
}}
}}
</div>
</div>
</noinclude>
</noinclude>

Latest revision as of 22:17, 24 September 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

Ex­tract cer­tain file from the archive di­rec­to­ry tree

tar --strip-components=6 -xvf "data.tar.zst" \
./usr/lib/x86_64-linux-gnu/nautilus/extensions-3.0/libnautilus-share.so

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.

Zip and Un­zip

Cre­ate an re­cur­sive archive for a di­rec­to­ry.

zip -r "archive-name.zip" "./directory-to-be-archived/"

Ex­tract archive.

unzip "archive-name.zip"

Rar and Un­rar

Some times the Lin­ux archive man­agers can't han­dle prop­er­ly RAR archives – for ex­am­ple look at this top­ic Ex­tract files with­in an .exe file. Here is how to ex­tract such files.

sudo apt install unrar
unrar x "PracticalMalwareAnalysis-Labs.exe"
unrar x "archive-name.rar"

Ref­er­ence