การจัดเก็บและบีบอัดไฟล์: `tar`, `gzip`, `bzip2`
ใช้คำสั่งสำหรับสร้างไฟล์จัดเก็บ บีบอัดไฟล์ และแตกเนื้อหาออกมา
การจัดเก็บและบีบอัดไฟล์: `tar`, `gzip`, `bzip2` เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจัดเก็บและบีบอัดไฟล์: `tar`, `gzip`, `bzip2`”
ใช้คำสั่งสำหรับสร้างไฟล์จัดเก็บ บีบอัดไฟล์ และแตกเนื้อหาออกมา คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 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