Mastering Core Skills in Linux File Compression and Packaging

Linux Cloud Computing | Daily Practice | Practical Tools | Career Development

In Linux systems, file compression and packaging are essential skills. Whether for saving disk space or facilitating file transfer, mastering these techniques can significantly enhance your efficiency. This article will take you through common compression and packaging formats such as tar, gzip, and zip, each with its advantages suitable for different scenarios. For instance, tar is a powerful packaging tool, gzip is suitable for single file compression, while zip combines compression and encryption features, widely used across various operating systems.

Additionally, we will detail the common commands for these tools, from simple packaging and compression to complex parameter settings, enabling you to get started quickly. Whether you are a beginner or have some experience with Linux, you will benefit from this knowledge. Mastering these skills will not only make your file management more efficient but also allow you to navigate your work with ease. Join us to learn and enhance your Linux file management capabilities, making complex operations simple!

🎁 Fan Benefits

Follow our public account and reply with 【Linux Toolkit】 to get:βœ… 150 basic Linux commands (bookmark version)βœ… Linux installation tutorial

Mastering Core Skills in Linux File Compression and Packaging

01. Linux File Compression and Decompression

In Linux systems, compressing and decompressing files is a common operation that helps save disk space and facilitates file transfer.

πŸ“‚ Common Compression Formats

  • .gz: Files compressed using the <span>gzip</span> tool.

  • .bz2: Files compressed using the <span>bzip2</span> tool.

  • .xz: Files compressed using the <span>xz</span> tool.

  • .tar: Files packaged using the <span>tar</span> tool, which does not compress by itself.

πŸ”§ Compression and Decompression Commands

πŸ”‘ Compressing Files

  • Use the <span>gzip</span> command to compress files:

gzip filename
For example, to compress the file `2.txt`:
gzip 2.txt

πŸ”‘ Decompressing Files

  • Use the <span>gzip -d</span> command to decompress files:

gzip -d filename.gz

For example, to decompress the file `2.txt.gz`:
gzip -d 2.txt.gz

πŸ“‚ <span>.bz2</span> File Format

<span>.bz2</span> is an efficient compression format compressed by the <span>bzip2</span> tool. It is typically used for compressing single files or directories, offering high compression rates, suitable for large files.

πŸ”§ Compressing Files

πŸ”‘ Using the <span>bzip2</span> Command to Compress Files

  • Command Format:<span>bzip2 -z [filename]</span>

  • Example: Compress the <span>2.txt</span> file.

bzip2 -z 2.txt
Output: Creates the file `2.txt.bz2`.

πŸ”‘ Using the <span>xz</span> Command to Compress Files

  • Command Format:<span>xz [filename]</span>

  • Example: Compress the <span>2.txt</span> file.

xz 2.txt
Output: Creates the file `2.txt.xz`

πŸ”‘ Using the <span>xz</span> Command to Decompress Files

  • Command Format:<span>xz -d [filename.xz]</span>

  • Example: Decompress the <span>2.txt.xz</span> file.

xz -d 2.txt.xz
Output: Restores the `2.txt` file.

πŸ“‚ <span>tar</span> Command Basics

<span>tar</span> command is used to create archive files, i.e., to package files and directories. It does not provide compression by itself but can be combined with other compression tools.

πŸ”‘ Basic Command Options

-z: Indicates compression with gzip.

-j: Indicates compression with bzip2.

-J: Indicates compression with xz.

-x: Indicates unpacking or decompressing.

-t: Indicates viewing the files in the tar package.

-c: Indicates creating a tar package or compressed file package.

-v: Indicates visualization.

-f: Followed by the filename (-filename, indicates the compressed file name as filename, or the file to decompress as filename, note that if multiple parameters are combined, generally the -f parameter is written last.

–exclude filename: Indicates that during packaging or compression, do not include the filename file.

.tar.gz: Can be understood as first packaging with tar, then compressing with gzip, using the -tf option, you can view the file list in the package or compressed package.

Mastering Core Skills in Linux File Compression and Packaging

File Packaging and Compression: Practical Cases of the <span><span>tar</span></span> Command

πŸ“‚ <span>tar</span> Command Basic Usage

πŸ”‘ Create Files

[root@localhost test]# mkdir test111[root@localhost test]# touch test111/1.txt test111/2.txt[root@localhost test]# echo 'hello' &gt; test111/1.txt[root@localhost test]# echo 'hello' &gt; test111/2.txt[root@localhost test]# cp 2.txt test111/2.txt[root@localhost test]# ls test1112.txt   test111.tar  test111[root@localhost test]# tar -cvf test111.tar test111

πŸ”‘ View Packaging Results

[root@localhost test]# ls2.txt  test111  test111.tar

πŸ”‘ Exclude Specific Files from Packaging

[root@localhost test]# tar -cvf test.tar test111 2.txt[root@localhost test]# ls2.txt  test111  test.tar

πŸ”‘ Compress Using <span>gzip</span>

[root@localhost test]# tar -czvf test111.tar

πŸ”‘ Compress Using <span>zip</span>

[root@localhost test]# zip -r test111.zip test111.tar.gz test111.tar test111.zip test.tar[root@localhost test]# ls2.txt  test111.tar.bz2  test111.tar.bz2  test111.tar.gz  test111.zip  test.tar

πŸ”‘ Decompressing a Secondary Directory

[root@localhost test]# zip -r test111.zip test111/*[root@localhost test]# unzip test111.zip

Through the practice of these basic commands, you can manage and transfer your Linux files more effectively.

πŸ“‚ Extended Practice Operations: File Permission Management

Set Default Permissions: umask 002, create files with default permissions 664, directories 775.

touch testfile.txtmkdir testdirls -ld testfile.txt testdir

Modify File Permissions: chmod 755 filename, set file permissions.

chmod 755 testfile.txtls -l testfile.txt

Set Special Attributes and View: chattr +i filename, lsattr filename.

chmod 755 testfile.txtls -l testfile.txt

Mastering Core Skills in Linux File Compression and PackagingMastering Core Skills in Linux File Compression and Packaging

02. grep Tool and Regular Expressions

In Linux, <span><span>grep</span></span> is a powerful text search tool that can use regular expressions to find matching text.

πŸ“‚ <span>grep</span> Tool Usage

<span>grep</span> command is used to search for lines containing a specific string.

πŸ”‘ 1.1 <span>grep</span> Command Format

  • <span>grep [options] regex filename</span>

πŸ”‘ 1.2 Command Examples

  1. Search for lines containing <span>halt</span>:

grep -A2 'halt' /etc/passwd
  • Output:

halt:x:7:0:halt:/sbin//sbin/haltmail:x:8:12:mail:/var/spool/mail/sbin/nologinoperator:x:11:0:operator:/root/sbin/nologin

2. Lines containing <span>halt</span> and the two lines above:

grep -B2 'halt' /etc/passwd
  • Output:

sync:x:5:0:sync:/sbin/synchalt:x:7:0:halt:/sbin/sbin/halt

3. Lines containing <span>halt</span> and the two lines around it:

grep -C2 'halt' /etc/passwd
  • Output:

shutdown:x:0:0:shutdown:/sbin/shutdownhalt:x:7:0:halt:/sbin/sbin/haltmail:x:8:12:mail:/var/spool/mail/sbin/nologin

4. Filter lines containing the <span>passwd</span> keyword and output line numbers:

grep -n 'passwd' /etc/passwd
  • Output:

root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/sbin/nologin

5. Filter all lines containing numbers:

grep '[0-9]' /etc/inittab
  • Output:

# multi-user.target: analogous to runlevel 3# graphical.target: analogous to runlevel 5

6. Filter all lines not containing numbers:

grep -v '[0-9]' /etc/inittab
  • Output:

# inittab is no longer used when using systemd.# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.

Through the practice of these basic commands, you can manage and transfer your Linux files more effectively.

Mastering Core Skills in Linux File Compression and Packaging

Mastering Core Skills in Linux File Compression and Packaging

03. Extended Practice

As a Linux operations engineer, filtering and processing files is a fundamental skill.

πŸ“‚ Online Practice:<span>/etc/os.conf</span><span> File Operations</span>

πŸ”‘ 1. Filter out all lines starting with <span>#</span>:

grep -v '^#' /etc/os.conf

This command will display all lines except those starting with <span>#</span>.

πŸ”‘ 2. Filter out all empty lines and lines starting with <span>#</span>:

grep -E '^[^#]' /etc/os.conf

This command will exclude empty lines and lines starting with <span>#</span>.

πŸ”‘ 3. Filter out any character followed by a repeated character:

grep -o '.\1' /etc/os.conf

This command will match any character followed by a repeated character.

πŸ”‘ 4. Specify the number of times a character should appear:

grep -o 'o.\{2}' /etc/os.conf

This command will match content where the letter <span>o</span> appears twice.

πŸ”‘ 5. Filter out one or more specified characters:

grep -E '[abc]' /etc/os.conf

This command will match lines containing any of the characters <span>a</span>, <span>b</span>, or <span>c</span>.

πŸ”‘ 6. Filter out 0 or 1 specified character:

grep -E '^[0-9]$' /etc/os.conf

This command will match lines containing numbers.

πŸ”‘ 7. If the above command is executed using <span>egrep</span>:

Using <span>egrep</span> command (<span>grep -E</span>) will enable extended regular expressions to match multiple character classes.

πŸ”‘ 8. List the contents of <span>backup.tar.gz</span> compressed file:

tar -tzvf backup.tar.gz

This command will list the contents of the <span>backup.tar.gz</span> compressed package.

πŸ”‘ 9. Extract the <span>src/main.c</span> file from <span>sources.tar.gz</span>:

tar -xzvf sources.tar.gz src/main.c -C .

This command will extract the <span>src/main.c</span> file from the <span>sources.tar.gz</span> compressed package to the current directory.

Through the practice of these basic commands, you can manage and operate Linux files more effectively. If you have any questions about Linux file handling or want to learn more related knowledge, feel free to leave a comment, and let’s discuss and learn together!🌟

Mastering Core Skills in Linux File Compression and Packaging

Linux File Compression and Packaging: Comprehensive Practical Guide

Mastering Core Skills in Linux File Compression and Packaging

Previous Recommendations

Linux Command Quick Reference: Even Beginners Can Master It Easily

Mastering Basic Linux Commands: From Novice to Expert

Linux File Permission Management: A Must for Becoming a Pro

🌟 The next article will introduce:

πŸš€ Linux Command Crash Course: Secrets to Doubling Your Efficiency!

In Linux systems, the command line is a powerful tool for enhancing work efficiency. Here are some commonly used Linux commands and their usage tips to help you quickly master and improve your work efficiency!

πŸ” grep Command: The Text Search Wizard

  • Basic Usage:<span>grep 'pattern' file</span>, quickly search for specific patterns in files.

  • Filter Empty Lines:<span>grep -v '^$' file</span>, exclude empty lines and only show lines with content.

  • Extended Regular Expressions:<span>egrep 'pattern' file</span> or <span>grep -E 'pattern' file</span>, use extended regular expressions for more complex searches.

πŸ” Filtering and Matching: Precise Positioning

  • Filter Specific Characters:<span>grep 'o.*' file</span>, match lines starting with β€œo”.

  • Multi-character Matching:<span>grep 'a.*z' file</span>, match lines containing β€œa” to β€œz”.

  • Count Matching Lines:<span>grep -c 'pattern' file</span>, count matching lines, a great tool for data analysis.

πŸ”„ sed Command: The Text Editing Tool

  • Replace Text:<span>sed 's/old/new/g' file</span>, globally replace text.

  • Delete Lines:<span>sed -i '/pattern/d' file</span>, delete matching lines.

  • Insert Text:<span>sed -i '1i New line' file</span>, insert a new line at the beginning of the file.

πŸ“„ Text Processing: Advanced Usage of sed Command

  • Print Specific Lines:<span>sed -n '1,3p' file</span>, print content from line 1 to line 3.

  • Print Lines Containing Specific Strings:<span>sed -n '/pattern/p' file</span>, print lines containing specific strings.

πŸ’‘ Reflection and Practice

These commands are not only very useful in daily work but also help you better understand how the Linux system works. Try applying these techniques in your actual work, and you will find significant improvements in your work efficiency and data processing capabilities!

πŸ”— Follow us for more Linux tips and practical tutorials!

Article Statement This article is exclusively published by the Linux Engineer Club. Please obtain authorization from the account administrator before reprinting. Planning and Production

Planning: Qingfeng | Supervisor: Mingyue

Editor: Xiaoxiao | Image Source: Gaoding Design and the Internet, infringement will be deleted

πŸš€ Welcome to the Treasure World of Linux Operations!

This is a paradise for technology enthusiasts, gathering cutting-edge Linux operation essentials. Whether you are a beginner or a seasoned engineer, you can find your treasure here.

πŸ” System Optimization: Master Linux system optimization techniques to boost server performance and easily handle high concurrency challenges.

πŸ” Troubleshooting: Quickly locate issues, efficiently resolve faults, and ensure stable system operation.

✏️ Script Writing: Learn to write efficient scripts to automate daily tasks and improve work efficiency.

πŸ€– Automated Operations: Explore automated operation tools for one-click deployment, monitoring, and management, making operations easier.

🌟 Interactive Topics: What is the biggest challenge you have encountered in Linux operations? Feel free to share your story in the comments, and let’s grow together!

🌟 Path of Growth: Let’s work together on the road of operations, exploring the mysteries of Linux and achieving a stronger self!

🌟 Join Us: QQ Group Number 628669460, follow me for more practical content, and let’s sail through the sea of technology together!

Mastering Core Skills in Linux File Compression and Packaging

Leave a Comment