归档与压缩:`tar`、`gzip`、`bzip2`
使用相关命令创建归档、压缩文件并提取其中的内容。
归档与压缩:`tar`、`gzip`、`bzip2` 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Archive & Compress?
Ever needed to bundle many files into one, or reduce a file's size? That's where archiving and compression come in!
Archiving combines multiple files or directories into a single file, often called a "tarball." This makes them easier to manage and transfer.
Compression reduces the size of a file, saving disk space and speeding up transfers.
`tar`: The Archiver
The tar command (Tape ARchiver) is the standard Linux utility for creating and extracting archive files. It doesn't compress by default, but it's often used with compression tools.
Key options for tar include:
c: Create a new archive.x: Extract files from an archive.v: Verbose output (show files being processed).f: Specify the archive filename.
Creating a `.tar` File
To create an archive, you typically use the -c (create), -v (verbose), and -f (filename) options. Let's make a simple archive of a directory.
mkdir my_project
echo "line 1" > my_project/file1.txt
echo "line 2" > my_project/file2.txt
tar -cvf project.tar my_project
ls -l project.tarUnpacking a `.tar` File
To extract files from a .tar archive, use the -x (extract), -v (verbose), and -f (filename) options. We'll extract the archive we just created.
Notice how the original directory structure is restored after extraction.
rm -rf my_project # Clean up existing directory
mkdir my_project # Create it again
echo "line 1" > my_project/file1.txt
echo "line 2" > my_project/file2.txt
tar -cvf project.tar my_project # Create the archive
rm -rf my_project # Delete original directory
tar -xvf project.tar # Extract it
ls -l my_project`gzip` for Quick Compression
gzip (GNU zip) is a popular tool for compressing single files. It's fast and effective, often reducing file sizes significantly. Files compressed with gzip typically have a .gz extension.
gzip replaces the original file with its compressed version. To decompress, use gunzip or gzip -d.
`gzip` in Action
Let's see gzip in action. We'll create a text file, compress it, and then decompress it. Observe the file sizes and extensions.
echo "This is a test file for gzip compression." > testfile.txt
ls -l testfile.txt
gzip testfile.txt
ls -l testfile.txt.gz
gunzip testfile.txt.gz
ls -l testfile.txt`.tar.gz`: Archiving & Gzipping
The most common way to archive and compress files in Linux is to combine tar with gzip. This creates a "tarball" that is also compressed, usually with the .tar.gz or .tgz extension.
tar has built-in support for compression. You just add the -z option for gzip to your tar command.
Working with `.tar.gz` Files
To create a .tar.gz file, use tar -czvf. The z option tells tar to use gzip compression. To extract, use tar -xzvf.
mkdir web_assets
echo "index.html content" > web_assets/index.html
echo "style.css content" > web_assets/style.css
tar -czvf website.tar.gz web_assets
ls -l website.tar.gz
rm -rf web_assets # Clean up
tar -xzvf website.tar.gz
ls -l web_assets`bzip2` for Max Compression
bzip2 is another compression utility, similar to gzip, but it typically achieves a higher compression ratio. This means smaller file sizes, but it's often slower during compression and decompression.
Files compressed with bzip2 usually have a .bz2 extension. To decompress, use bunzip2 or bzip2 -d.
`.tar.bz2`: Archiving & Bzipping
When maximum compression is desired, you can combine tar with bzip2. This creates a .tar.bz2 or .tbz2 file. The tar option for bzip2 is -j.
To create, use tar -cjvf. To extract, use tar -xjvf.
mkdir large_logs
for i in $(seq 1 5); do echo "Log entry $i: Something important happened." >> large_logs/app.log; done
tar -cjvf logs.tar.bz2 large_logs
ls -l logs.tar.bz2
rm -rf large_logs # Clean up
tar -xjvf logs.tar.bz2
ls -l large_logsArchiving & Compression Quiz
Which command would you use to create an archive named backup.tar.gz containing the directory mydata, showing verbose output during the process?
Archiving & Compression Recap
Great job! You've learned the essentials of archiving and compression in Linux:
tar: For bundling files into a single archive (.tar).gzip: For fast, efficient compression (.gz).bzip2: For stronger compression, though slower (.bz2).- Combining
tarwith-zforgzip(.tar.gz) or-jforbzip2(.tar.bz2) for common compressed archives.
These tools are fundamental for managing files efficiently and securely!
常见问题解答
「归档与压缩:`tar`、`gzip`、`bzip2`」课时是免费的吗?
是的 — 「归档与压缩:`tar`、`gzip`、`bzip2`」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「归档与压缩:`tar`、`gzip`、`bzip2`」这节课中我会学到什么?
使用相关命令创建归档、压缩文件并提取其中的内容。 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「归档与压缩:`tar`、`gzip`、`bzip2`」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 归档与压缩:`tar`、`gzip`、`bzip2`
- 软件包管理(Debian/Ubuntu):`apt`、`dpkg`
- 软件包管理(CentOS/Fedora):`yum`、`dnf`、`rpm`
- 从源代码构建:configure、make 与 make install