112 lines
2.5 KiB
Markdown
112 lines
2.5 KiB
Markdown
|
|
# Compression In Linux (tar and gzip)
|
||
|
|
|
||
|
|
1. Introduction to tar and gzip
|
||
|
|
2. Using tar
|
||
|
|
3. Using gzip
|
||
|
|
4. Combining tar and gzip
|
||
|
|
5. Advanced usage and options
|
||
|
|
6. Best practices and tips
|
||
|
|
|
||
|
|
## 1. Introduction to tar and gzip:
|
||
|
|
|
||
|
|
tar (Tape Archive) and gzip (GNU Zip) are two essential utilities in Linux for archiving and compressing files.
|
||
|
|
|
||
|
|
- tar: Creates archives by combining multiple files and directories into a single file.
|
||
|
|
- gzip: Compresses single files to reduce their size.
|
||
|
|
|
||
|
|
These tools are often used together to create compressed archives.
|
||
|
|
|
||
|
|
## 2. Using tar:
|
||
|
|
|
||
|
|
Basic syntax: tar [options] [archive_name] [files_to_archive]
|
||
|
|
|
||
|
|
Common options:
|
||
|
|
- c: Create a new archive
|
||
|
|
- x: Extract files from an archive
|
||
|
|
- v: Verbose mode (list files processed)
|
||
|
|
- f: Specify the archive file name
|
||
|
|
|
||
|
|
Examples:
|
||
|
|
- Create an archive:
|
||
|
|
```
|
||
|
|
tar -cvf archive.tar file1 file2 directory1
|
||
|
|
```
|
||
|
|
- Extract an archive:
|
||
|
|
```
|
||
|
|
tar -xvf archive.tar
|
||
|
|
```
|
||
|
|
- List contents of an archive:
|
||
|
|
```
|
||
|
|
tar -tvf archive.tar
|
||
|
|
```
|
||
|
|
|
||
|
|
## 3. Using gzip:
|
||
|
|
|
||
|
|
Basic syntax: gzip [options] [file_name]
|
||
|
|
|
||
|
|
Common options:
|
||
|
|
- d: Decompress
|
||
|
|
- r: Recursive (compress files in directories)
|
||
|
|
- v: Verbose mode
|
||
|
|
- [number]: Compression level (1-9, 9 being highest)
|
||
|
|
|
||
|
|
Examples:
|
||
|
|
- Compress a file:
|
||
|
|
```
|
||
|
|
gzip file.txt
|
||
|
|
```
|
||
|
|
- Decompress a file:
|
||
|
|
```
|
||
|
|
gzip -d file.txt.gz
|
||
|
|
```
|
||
|
|
- Compress with highest level:
|
||
|
|
```
|
||
|
|
gzip -9 file.txt
|
||
|
|
```
|
||
|
|
|
||
|
|
## 4. Combining tar and gzip:
|
||
|
|
|
||
|
|
tar can use gzip compression directly with the 'z' option.
|
||
|
|
|
||
|
|
- Create a compressed archive:
|
||
|
|
```
|
||
|
|
tar -czvf archive.tar.gz directory1
|
||
|
|
```
|
||
|
|
- Extract a compressed archive:
|
||
|
|
```
|
||
|
|
tar -xzvf archive.tar.gz
|
||
|
|
```
|
||
|
|
|
||
|
|
## 5. Advanced usage and options:
|
||
|
|
|
||
|
|
- Exclude files or directories:
|
||
|
|
```
|
||
|
|
tar -czvf archive.tar.gz directory1 --exclude=*.log
|
||
|
|
```
|
||
|
|
- Update an existing archive:
|
||
|
|
```
|
||
|
|
tar -uvf archive.tar newfile
|
||
|
|
```
|
||
|
|
- Use bzip2 compression (often better for text):
|
||
|
|
```
|
||
|
|
tar -cjvf archive.tar.bz2 directory1
|
||
|
|
```
|
||
|
|
- Preserve file permissions:
|
||
|
|
```
|
||
|
|
tar -cpvf archive.tar directory1
|
||
|
|
```
|
||
|
|
- Split large archives:
|
||
|
|
```
|
||
|
|
tar -cvf - directory1 | split -b 1G - archive.tar.part
|
||
|
|
```
|
||
|
|
|
||
|
|
## 6. Best practices and tips:
|
||
|
|
|
||
|
|
- Use .tar.gz or .tgz extension for gzip-compressed tar archives
|
||
|
|
- Test archives after creation by listing or extracting
|
||
|
|
- Use compression levels wisely (higher levels are slower)
|
||
|
|
- Consider using xz for better compression (use -J option with tar)
|
||
|
|
- For large backups, consider using incremental backups with tar's --listed-incremental option
|
||
|
|
|
||
|
|
This guide covers the basics and some advanced usage of tar and gzip in Linux.
|