2.11.16. Create, view, test and extract archive
Archive
zip
Adding files and directories to the archive:
zip -r archive.zip file1.txt file2.txt directory1 directory2
In command:
-r— archive the contents of the specified directories.archive.zip— the name of the target archive. If the archive does not exist, it will be created.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
7z
Adding files and directories to the archive:
7za a archive.7z file1.txt file2.txt directory1 directory2
In command:
a— add files and directories to the archive.archive.7z— the name of the target archive. If the archive does not exist, it will be created.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
tar
Attention!
tar is an uncompressed format. Archiving does not reduce the file size; it simply combines them into one.Archiving files and directories:
tar -cvf archive.tar file1.txt file2.txt directory1 directory2
In command:
-c— create a new archive.-v— display information during the archiving process.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the target archive. If the archive already exists, it will be overwritten if the-coption is present.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
Adding files and directories to the archive:
tar -rvf archive.tar file1.txt file2.txt directory1 directory2
In command:
-r— add files and directories to the archive.-v— display information during the archiving process.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the target archive. If the archive does not exist, it will be created.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
tar.gz
Notes:
- gzip compresses less effectively than bzip2, but operates faster.
- The archive file may have the extensions
tar.gz,tar.gzip, ortgz.
Archiving files and directories:
tar -czvf archive.tar.gz file1.txt file2.txt directory1 directory2
In command:
-c— create a new archive.-z— compress data using gzip.-v— display information during the archiving process.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the target archive. If the archive already exists, it will be overwritten if the-coption is present.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
tar.bz2
Notes:
- bzip2 compresses better than gzip, but works more slowly.
- The archive file may have the extensions
tar.bz2,tar.bzip2,tbz2,tb2, ortbz.
Archiving files and directories:
tar -cjvf archive.tar.bz2 file1.txt file2.txt directory1 directory2
In command:
-c— create a new archive.-j— compress data using bzip2.-v— display information during the archiving process.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the target archive. If the archive already exists, it will be overwritten if the-coption is present.file1.txtand so on — files that need to be archived.directory1, etc. — directories that need to be archived.
View content
Universal method
Viewing archive contents:
lsar archive.7z
In command:
archive.7z— the name of the archive.
zip
Viewing archive contents:
unzip -l archive.zip
In command:
-l— display the list of files in the archive.archive.zip— the name of the archive.
7z
Viewing archive contents:
7za l archive.7z
In command:
l— display the list of files in the archive.archive.7z— the name of the archive.
rar
Viewing archive contents:
unrar l archive.rar
In command:
l— display the list of files in the archive.archive.rar— the name of the archive.
tar
Viewing the archive summary:
tar -tf archive.tar
In command:
-l— display the list of files in the archive.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the archive.
Viewing the full contents of the archive:
tar -tvf archive.tar
In command:
-l— display the list of files in the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the archive.
tar.gz
tar.gz, tar.gzip, or tgz.
Viewing the archive summary:
tar -tf archive.tar.gz
In command:
-l— display the list of files in the archive.-f— indicates that the next argument in the command is the archive name.archive.tar.gz— the name of the archive.
Viewing the full contents of the archive:
tar -tvf archive.tar.gz
In command:
-l— display the list of files in the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.gz— the name of the archive.
tar.bz2
tar.bz2, tar.bzip2, tbz2, tb2, or tbz.
Viewing the archive summary:
tar -tf archive.tar.bz2
In command:
-l— display the list of files in the archive.-f— indicates that the next argument in the command is the archive name.archive.tar.bz2— the name of the archive.
Viewing the full contents of the archive:
tar -tvf archive.tar.bz2
In command:
-l— display the list of files in the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.bz2— the name of the archive.
Test
zip
Testing the archive:
unzip -tq archive.zip
In command:
-t— test the archive for errors.-q— output minimal information.archive.zip— the name of the archive.
7z
Testing the archive:
7za t archive.7z
In command:
t— test the archive for errors.archive.7z— the name of the archive.
rar
Testing the archive:
unrar t archive.rar
In command:
t— test the archive for errors.archive.rar— name of the archive.
tar.gz
tar.gz, tar.gzip, or tgz.
Testing the archive:
gzip -t archive.tar.gz
In command:
-t— test the archive for errors.archive.tar.gz— the name of the archive.
tar.bz2
tar.bz2, tar.bzip2, tbz2, tb2, or tbz.
Testing the archive:
bzip2 -t archive.tar.bz2
In command:
-t— test the archive for errors.archive.tar.bz2— the name of the archive.
gz
Testing the archive:
gzip -tv archive.gz
In command:
-t— test the archive for errors.-v— display the test results.archive.gz— the name of the archive.
Extract
Universal method
Unpacking the contents of the archive into the current directory:
unar archive.7z
In command:
archive.7z— name of the archive.
Unpacking the contents of the archive into the specified directory:
unar archive.7z -o /path/to/dir
In command:
archive.7z— name of the archive.-o— unpack to the specified directory./path/to/dir— the path to the specified directory.
zip
Attention!
To avoid potential encoding issues, extract archives containing files with Cyrillic names using unar.Unpacking the contents of the archive into the current directory:
unzip archive.zip
In command:
archive.zip— the name of the archive file.
Unpacking the contents of the archive into the specified directory:
unzip archive.zip -d /path/to/dir
In command:
archive.zip— name of the archive.-d— unpack to the specified directory./path/to/dir— the path to the specified directory.
7z
Attention!
To avoid potential encoding issues, extract archives containing files with Cyrillic names using unar.Unpacking the contents of the archive into the current directory:
7za x archive.7z
In command:
x— extract the contents of the archive.archive.7z— the name of the archive.
Unpacking the contents of the archive into the specified directory:
7za x archive.7z -o/path/to/dir
In command:
x— extract the contents of the archive.archive.7z— name of the archive.-o— unpack to the specified directory. ⚠️ There should be no space after the key./path/to/dir— the path to the specified directory. ⚠️ The path must not start with~.
rar
Attention!
To avoid potential encoding issues, extract archives containing files with Cyrillic names using unar.Unpacking the contents of the archive into the current directory:
unrar e archive.rar
In command:
e— extract the contents of the archive.archive.rar— name of the archive.
Unpacking the contents of the archive into the specified directory:
unrar e archive.rar /path/to/dir
In command:
e— extract the contents of the archive.archive.rar— name of the archive./path/to/dir— the path to the specified directory.
tar
Unpacking the contents of the archive into the current directory:
tar -xvf archive.tar
In command:
-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the archive.
Unpacking the contents of the archive into the specified directory:
mkdir /path/to/dir
tar -xvf archive.tar -C /path/to/dir
In command:
-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar— the name of the archive.-C— unpack to the specified directory./path/to/dir— the path to the specified directory.
tar.gz
tar.gz, tar.gzip, or tgz.
Unpacking the contents of the archive into the current directory:
tar -zxvf archive.tar.gz
In command:
-z— unpack the contents of the archive.-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.gz— the name of the archive.
Unpacking the contents of the archive into the specified directory:
mkdir /path/to/dir
tar -zxvf archive.tar.gz -C /path/to/dir
In command:
-z— unpack the contents of the archive.-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.gz— the name of the archive.-C— unpack to the specified directory./path/to/dir— the path to the specified directory.
tar.bz2
tar.bz2, tar.bzip2, tbz2, tb2, or tbz.
Unpacking the contents of the archive into the current directory:
tar -jxvf archive.tar.bz2
In command:
-j— unpack the contents of the archive.-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.bz2— name of the archive.
Unpacking the contents of the archive into the specified directory:
mkdir /path/to/dir
tar -jxvf archive.tar.bz2 -C /path/to/dir
In command:
-j— unpack the contents of the archive.-x— extract the contents of the archive.-v— display detailed information.-f— indicates that the next argument in the command is the archive name.archive.tar.bz2— name of the archive.-C— unpack to the specified directory./path/to/dir— the path to the specified directory.