Difference between revisions of "Linux archives"
Jump to navigation
Jump to search
(19 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= TAR, GZ, BZIP = | = TAR, GZ, BZIP = | ||
Working with archives in linux where | Working with archives in linux where </code>tar = tape archive</code> and the most common used options are: | ||
*<code>-c</code> - ''create'' a new tar file | |||
*<code>-x</code> - ''extract'' file | |||
*<code>-t</code> - ''test'' list the contents of an archive | |||
*<code>-z</code> - ''compress'' use gzip compress, extention </code>.tar.gz</code> | |||
*<code>-j</code> - ''compress'' use bzip2 compress, extention </code>.tar.bz2</code> | |||
*<code>-v</code> - ''verbose'' displays files to compress or uncompress | |||
*<code>-f</code> - ''file'' specify the new archive name or an archive to extract from | |||
*<code>-r</code> - flag to append the additional files to the archive | |||
*<code>-u</code> - ''updating'' a file | |||
===Preview files inside archive= | |||
;Compress | |||
Create a compressed .tar.gz archive | |||
<source lang="bash"> | |||
#archive a directory and everything under (recursively), the dir tree will be preserved eg. /dir/to/be/compressed/ when you extract | |||
tar -czvf archive.tar.gz /dir/to/be/compressed/ | |||
tar -czvf archive.tar.gz /path/to/*.conf #archive only files matching *.conf | |||
</source> | |||
Create a compressed archive controlling ''directory tree structure'' | |||
<source lang="bash"> | |||
tar -czvf archive.tar.gz -C directory/level1/ . | |||
# -C change current working directory | |||
# . archive all files from a current working directory, this way when you extract it will extract to to the current working dir | |||
</source> | |||
Compress each directory in current directory into separate zip | |||
<source lang="bash"> | |||
$ for i in */; do echo tar -czvf "${i%/}.tar.gz" "$i"; done #test | |||
$ for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done #run | |||
$ for i in */; do rm -rf "$i"; done | |||
</source> | |||
;Extract | |||
Extract '''whole''' archive, use <code>-t</code> to test | |||
<source lang="bash"> | |||
tar -xzvf archive.tar.gz | |||
</source> | |||
A '''single file''' from compressed .tar.gz, use <code>-t</code> to test | |||
<source lang="bash"> | |||
tar -xzvf archive.tar.gz file_to_extract.log | |||
tar xvf archive.tar --strip-components=1 -C anotherDirectory/ file_to_extract.log | |||
# --strip-components=NUMBER Strip NUMBER leading components/directories from file names on extraction | |||
</source> | |||
A '''single directory''' (here '''conf''' directory) to specific directory (here '''/tmp/logs''' from compressed .tar.gz, use <code>-t</code> to test | |||
<source lang="bash"> | |||
tar -xzvf archive.tar.gz conf -C /tmp/logs | |||
</source> | |||
;Preview files inside archive | |||
<source lang="bash"> | |||
tar -tzvf {.tar.gz} | |||
tar -tjvf {.tbz2} | |||
</source> | |||
;Extract multiple archives at once | |||
<source lang=bash> | |||
for file in *.tar.gz; do tar -xzvf "$file"; done | |||
cat *.tar.gz | tar -xzvf - -i # -i, --ignore-zeros - ignore blocks of zeros in archive (normally mean EOF) | |||
</source> | |||
= Zip = | = Zip = | ||
Install <tt>zip</tt> and <tt>unzip</tt> then use interactive method: | Install <tt>zip</tt> and <tt>unzip</tt> then use interactive method: | ||
;Extract using default options, retaining directory structure | ;Extract using default options, retaining directory structure, if password protected you will be prompted | ||
<source lang="bash"> | |||
unzip file.zip | |||
Enter password: *** # <- only when password protected | |||
</source> | |||
;Compress file or directory | ;Compress file or directory | ||
<source lang="bash"> | |||
zip name.zip directory/* | |||
zip name.zip file1 file2 file3 | |||
# Encrypted password protected zip | |||
zip --encrypt secure.zip file1 file2 | |||
Enter password: *** | |||
Verify password: *** | |||
adding: file (deflated 8%) | |||
# Create an encrypted ZIP archive from a folder /var/log/ | |||
zip --encrypt -r secure.zip /var/log/ | |||
</source> | |||
<blockquote>Warning! The standard ZIP encryption is very weak and could be cracked easily. </blockquote> | |||
Compress each directory in current directory into separate zip | |||
<source lang="bash"> | |||
$ for file in */; do echo zip -r "${file%/}.zip" "$file"; done # Test | |||
$ for file in */; do zip -r "${file%/}.zip" "$file"; done # Run | |||
</source> | |||
[[Category:linux]] | [[Category:linux]] |
Latest revision as of 12:22, 2 November 2021
TAR, GZ, BZIP
Working with archives in linux where tar = tape archive and the most common used options are:
-c
- create a new tar file-x
- extract file-t
- test list the contents of an archive-z
- compress use gzip compress, extention .tar.gz-j
- compress use bzip2 compress, extention .tar.bz2-v
- verbose displays files to compress or uncompress-f
- file specify the new archive name or an archive to extract from-r
- flag to append the additional files to the archive-u
- updating a file
- Compress
Create a compressed .tar.gz archive
#archive a directory and everything under (recursively), the dir tree will be preserved eg. /dir/to/be/compressed/ when you extract tar -czvf archive.tar.gz /dir/to/be/compressed/ tar -czvf archive.tar.gz /path/to/*.conf #archive only files matching *.conf
Create a compressed archive controlling directory tree structure
tar -czvf archive.tar.gz -C directory/level1/ . # -C change current working directory # . archive all files from a current working directory, this way when you extract it will extract to to the current working dir
Compress each directory in current directory into separate zip
$ for i in */; do echo tar -czvf "${i%/}.tar.gz" "$i"; done #test $ for i in */; do tar -czvf "${i%/}.tar.gz" "$i"; done #run $ for i in */; do rm -rf "$i"; done
- Extract
Extract whole archive, use -t
to test
tar -xzvf archive.tar.gz
A single file from compressed .tar.gz, use -t
to test
tar -xzvf archive.tar.gz file_to_extract.log tar xvf archive.tar --strip-components=1 -C anotherDirectory/ file_to_extract.log # --strip-components=NUMBER Strip NUMBER leading components/directories from file names on extraction
A single directory (here conf directory) to specific directory (here /tmp/logs from compressed .tar.gz, use -t
to test
tar -xzvf archive.tar.gz conf -C /tmp/logs
- Preview files inside archive
tar -tzvf {.tar.gz} tar -tjvf {.tbz2}
- Extract multiple archives at once
for file in *.tar.gz; do tar -xzvf "$file"; done cat *.tar.gz | tar -xzvf - -i # -i, --ignore-zeros - ignore blocks of zeros in archive (normally mean EOF)
Zip
Install zip and unzip then use interactive method:
- Extract using default options, retaining directory structure, if password protected you will be prompted
unzip file.zip Enter password: *** # <- only when password protected
- Compress file or directory
zip name.zip directory/* zip name.zip file1 file2 file3 # Encrypted password protected zip zip --encrypt secure.zip file1 file2 Enter password: *** Verify password: *** adding: file (deflated 8%) # Create an encrypted ZIP archive from a folder /var/log/ zip --encrypt -r secure.zip /var/log/
Warning! The standard ZIP encryption is very weak and could be cracked easily.
Compress each directory in current directory into separate zip
$ for file in */; do echo zip -r "${file%/}.zip" "$file"; done # Test $ for file in */; do zip -r "${file%/}.zip" "$file"; done # Run