How To Create Gz File In Linux
How to create a .tar.gz file on Linux
A few examples to show the use of tar command to create a .tar.gz
file on Linux.
Table of contents
- 1. Create, list and extract a .tar.gz file
- 2. What is tarball or .tar.gz?
- 3. Create a .tar.gz file
- 3.1 Tar and compress a file
- 3.2 Tar and compress multiple files
- 3.3 Tar and compress multiple files and folders.
- 4. Extract the .tar.gz file
- 4.1 Extract the .tar.gz file to current directory
- 4.2 Extract the .tar.gz file to a specified directory
- 5. References
1. Create, list and extract a .tar.gz file
We use -c
to create a .tar.gz
file.
Terminal
$ tar -cvzf example.tar.gz {files or folders to tar and compress}
We use -tf
to list the content of a .tar.gz
file.
Terminal
$ tar -tf example.tar.gz
We use -x
to extract a .tar.gz
file.
Terminal
$ tar -xvzf example.tar.gz $ tar -xvzf example.tar.gz -C {path}
2. What is tarball or .tar.gz?
On Linux or macOS, there is a tar
command to group files and folders into one archive file, aka tarball or a .tar
file, and later, we can compress the tarball to reduce the file size, the standard compress algorithm is gzipping or .gz
file.
For example, below are two text files.
Terminal
a.txt b.txt
We can group the above files into one .tar
file or tarball.
Terminal
a.txt b.txt --> tar --> example.tar (100MB)
And we compress the above tar file to reduce the file size.
Terminal
example.tar --> gzip --> example.tar.gz (50MB)
3. Create a .tar.gz file
Below are some tar
options to create a .tar.gz
file.
-
-c
to tar a few files or folders -
-z
to gzip compress the tar file -
-f
to give a file name -
-v
to verbose output (optional)
The final command is tar -cvzf filename.tar.gz {files} {folders}
.
Below are two text files for testing.
Terminal
ls -lsa total 16 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 17:41 . 0 drwxr-xr-x 12 yongmookkim staff 384 May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12 May 29 17:36 b.txt
3.1 Tar and compress a file
Below example tar
a single file a.txt
and compress it using gzip and output it to example.tar.gz
.
Terminal
$ tar -czf example.tar.gz a.txt $ ls -lsah total 24 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 17:43 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 123B May 29 17:43 example.tar.gz # list the content of the tar.gz file $ tar -tf example.tar.gz a.txt
3.2 Tar and compress multiple files
Below tar
command tar multiple files a.txt
and b.txt
and compress it using gzip and output it to example2.tar.gz
.
Terminal
$ tar -czf example2.tar.gz a.txt b.txt $ ls -lsa total 32 0 drwxr-xr-x 6 yongmookkim staff 192 May 29 17:46 . 0 drwxr-xr-x 12 yongmookkim staff 384 May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12 May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 156 May 29 17:46 example2.tar.gz $ tar -tf example2.tar.gz a.txt b.txt
3.3 Tar and compress multiple files and folders.
Review the current directory structure.
Terminal
$ tree . ├── a.txt ├── b.txt └── folderA ├── c.txt └── folderB └── d.txt
We want to tar and compress a.txt
file and folderA
directory and its subdirectories and subfiles and output it to example3.tar.gz
.
Terminal
$ ls -lsah total 16 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 17:50 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA $ tar cvzf example3.tar.gz a.txt folderA a a.txt a folderA a folderA/folderB a folderA/c.txt a folderA/folderB/d.txt $ ls -lsah total 24 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 17:54 . 0 drwxr-xr-x 12 yongmookkim staff 384B May 29 17:35 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 12B May 29 17:36 b.txt 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA $ tar -tf example3.tar.gz a.txt folderA/ folderA/folderB/ folderA/c.txt folderA/folderB/d.txt
Below are some tar
options to uncompress, unzip or untar a .tar.gz
file.
-
-x
to extract the tar file -
-z
to gzip uncompress the tar file -
-f
to give a file name -
-v
for the detail (optional)
The final command to extract the .tar.gz
is tar -xvzf filename.tar.gz
.
By default, the -x
will extract the files or folders to the current directory running the command.
Terminal
$ ls -lsah total 8 0 drwxr-xr-x 3 yongmookkim staff 96B May 29 21:31 . 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 21:31 .. 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz $ tar -xvzf example3.tar.gz x a.txt x folderA/ x folderA/folderB/ x folderA/c.txt x folderA/folderB/d.txt $ ls -lsah total 16 0 drwxr-xr-x 5 yongmookkim staff 160B May 29 21:36 . 0 drwxr-xr-x 6 yongmookkim staff 192B May 29 21:31 .. 8 -rw-r--r-- 1 yongmookkim staff 6B May 29 17:35 a.txt 8 -rw-r--r-- 1 yongmookkim staff 240B May 29 17:54 example3.tar.gz 0 drwxr-xr-x 4 yongmookkim staff 128B May 29 17:50 folderA
We can add a -C DIR
to change to a specified directory and do the extract operation next, which means extract the content of .tar.gz
to a specified directory.
The below example extracts the content of example3.tar.gz
to this directory /home/mkyong/backup
.
Terminal
$ tar -xvzf example3.tar.gz -C /home/mkyong/backup x a.txt x folderA/ x folderA/folderB/ x folderA/c.txt x folderA/folderB/d.txt $ ls -lsa /home/mkyong/backup total 8 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 21:42 . 0 drwxr-xr-x+ 38 yongmookkim staff 1216 May 29 21:42 .. 8 -rw-r--r-- 1 yongmookkim staff 6 May 29 17:35 a.txt 0 drwxr-xr-x 4 yongmookkim staff 128 May 29 17:50 folderA
5. References
- Wikipedia – tar
- How to password protect gzip files on the command line?
- How to backup a website to Amazon S3 using shell script
- How to create tar.gz in Java
Comments
How To Create Gz File In Linux
Source: https://mkyong.com/linux/how-to-zip-unzip-tar-in-unix-linux/
Posted by: eastmansainest1939.blogspot.com
0 Response to "How To Create Gz File In Linux"
Post a Comment