LWM-Linux/06 - Linux File Operations/Compression and Archiving (tar and gzip).md

84 lines
2.8 KiB
Markdown
Raw Normal View History

# 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:
2024-09-07 09:29:54 -06:00
- -c: "C"reate a new archive
- -x: E"x"tract files from an archive
- -v: "V"erbose mode (list files processed)
- -f: Specify the archive "f"ile name
- -t: Lis"t" whats inside the archive
- -C: CAPITAL C will "C"hange the directory to output the extracted contents
Examples:
2024-09-07 09:29:54 -06:00
### - Create an archive:
- `tar -cvf archive.tar file1 file2 directory1` # "C"reate an archive called archive.tar using file1, file2, and directory1.
### - Extract an archive:
- `tar -xvf archive.tar -C ~/extracted/` # Will change to the home "~" directory and then into the extracted directory before extracting the archive.
### - List contents of an archive:
- `tar -tvf archive.tar`
## 3. Using gzip:
Basic syntax: gzip [options] [file_name]
Common options:
2024-09-07 09:29:54 -06:00
- -d: Decompress
- -r: Recursive (compress files in directories)
- -v: Verbose mode
- -[1-9]: Compression level (1-9, 9 being highest)
Examples:
2024-09-07 09:29:54 -06:00
### - Compress a file with a compression level:
- `gzip -9 file.txt` # gzip a file using the best compression "-9" available.
### - Decompress a file:
- `gzip -d file.txt.gz`
## 4. Combining tar and gzip:
tar can use gzip compression directly with the 'z' option.
2024-09-07 09:29:54 -06:00
### - Create a compressed archive:
- `tar -czvf archive.tar.gz directory1` # Will "c"reate a tar archive, then g"z"ip it.
### - Extract a compressed archive:
`tar -xzvf archive.tar.gz`
## 5. Advanced usage and options:
2024-09-07 09:29:54 -06:00
### - Exclude files or directories:
- `tar -czvf archive.tar.gz directory1 --exclude=*.log`
### - Update an existing archive:
- `tar -uvf archive.tar newfile` # This will add the newfile into the existing archive.tar.
### - 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