Create a
This will compress the contents of source-folder-name to a tar.gz archive named tar-archive-name.tar.gz
To extract a tar.gz compressed archive you can use the following command:
This will extract the archive to the folder tar-archive-name.
#linux #tar #targz #zip #compress
tar.gz
file using tar
command:tar -zcvf tar-archive-name.tar.gz source-folder-name
-z
: The z option is very important and tells the tar command to uncompress the file (gzip).-c
: I don't know! If you know what it does please tell me. Thank you.-v
: The “v” stands for “verbose.” This option will list all of the files one by one in the archive.-f
: This options tells tar that you are going to give it a file name to work with.This will compress the contents of source-folder-name to a tar.gz archive named tar-archive-name.tar.gz
To extract a tar.gz compressed archive you can use the following command:
tar -zxvf tar-archive-name.tar.gz
-x
: This option tells tar
to extract the files.This will extract the archive to the folder tar-archive-name.
#linux #tar #targz #zip #compress
In order to compress a file with level of 9 (maximum level of compression), you need to
set an ENV variable. In order to not clobber the file system environments you can use pipe in your command:
#tar #gzip #compression_level
set an ENV variable. In order to not clobber the file system environments you can use pipe in your command:
tar cvf - /path/to/directory | gzip -9 - > file.tar.gz
#tar #gzip #compression_level
tarfile
is a python library to read and write gzip`/`bz2
compressed files.How to read a
gzip
compressed tar archive and display some member information:import tarfile
tar = tarfile.open("sample.tar.gz", "r:gz")
for tarinfo in tar:
print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
if tarinfo.isreg():
print "a regular file."
elif tarinfo.isdir():
print "a directory."
else:
print "something else."
tar.close()
Create a compressed file:
with tarfile.open(dst, "w:gz") as tar:
print("Archiving " + src + " into " + dst)
tar.add(src, arcname = os.path.basename(src))
NOTE:
the flag of w:gz
opens the destination in write mode. Used to create a new tar file.#python #tarfile #tar #bz2 #gzip