Archiving is the process of combining multiple files and directories (same or different sizes) into one file. Compression is the process of reducing the size of a file or directory.
Common type of compression:
-
Gzip: with extension.tar.gz. It has fastest compression time. -
Bzip: with extension.tar.bz2 -
Xz: with extension.tar.xz. It has smallest size compression.
Common programs for archiving files:
tarzipgzipbzip2xz
We will talk about how we use tar command. It has a lot of option that we can use:
-cor--create: create a new file archive-
-for--file: filename
# create an archive from multiple files
tar -cf ARCHIVE.tar FILE1.txt FILE2.txt FILE3.txt
# create an archive from a directory
tar -cf ARCHIVE.tar DIRECTORY
# using long command
tar --create --file ARCHIVE.tar DIRECTORY
-
-Xor--exclude: exclude a file
# exclude a file when creating an archive
tar -cf ARCHIVE.tar DIRECTORY --exclude=FILE.txt
-
-xor--extract: extract an archive
# extract an archive file
tar -xf ARCHIVE.tar
# using long command
tar --extract --file ARCHIVE.tar
-z: zip or unzip usinggzip
create a compressed archive
# create an archive with gzip compression
tar -czf ARCHIVE.tar.gz DIRECTORY
# using gzip
gzip ARCHIVE.tar.gz DIRECTORY
extract a compressed archive
# extract an archive with gzip compression
tar -xzf ARCHIVE.tar.gz
# using gunzip --decompress
gunzip -d ARCHIVE.tar.gz
-j: zip or unzip usingbzip2
create a compressed archive
# create an archive with bzip compression
tar -cjf ARCHIVE.tar.bz2 DIRECTORY
# using bzip2
bzip2 ARCHIVE.tar.bz2 DIRECTORY
extract a compressed archive
# extract an archive with bzip compression
tar -xjf ARCHIVE.tar.bz2
# using bunzip2 --decompress
bunzip2 -d ARCHIVE.tar.bz2
-J: zip or unzip usingxz
create a compressed archive
# create an archive with xz compression
tar -cJf ARCHIVE.tar.xz DIRECTORY
# using xz
xz ARCHIVE.tar.xz DIRECTORY
extract a compressed archive
# extract an archive with xz compression
tar -xJf ARCHIVE.tar.xz
# using unxz --decompress
unxz -d ARCHIVE.tar.xz
zipandunzip
create a compressed archive
# create using zip
zip ARCHIVE.zip DIRECTORY
extract a compressed archive
# extract using unzip
unzip ARCHIVE.zip
-
-tor--list: list the contents of an archive without extracting it
# display contents of an archive
tar -tf ARCHIVE.tar
# display contents using gzip
gzip -l ARCHIVE.tar.gz
# display contents using unzip
unzip -l ARCHIVE.zip
-
-ror--append: append files to the end of an archive
# append files to an archive
tar -rf ARCHIVE.tar FILE.txt
# using --append
tar --append -file ARCHIVE.tar FILE.txt
-
-uor--update: Append files which are newer than the corresponding copy in the archive
# append files to an archive
tar -uf ARCHIVE.tar FILE.txt
# using --append
tar --update -file ARCHIVE.tar FILE.txt
-
delete: delete files inside an archive
# delete from the archive
tar --delete --file ARCHIVE.tar FILE.txt
-
-Cor--directory: change the directory
# change location of unarchived files
tar -xf ARCHIVE.tar -C DIRECTORY
-vor--verbose: see the detailed output
We can combine this options with all others--wildcards: matching on the filenames within an archive
# extract a particular type of file
tar -xf ARCHIVE.tar --wildcards "*.txt"
Top comments (0)