MTI TEK
  • Home
  • About
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • All Resources
Linux-Ubuntu | gzip/gunzip -- compression/decompression
  1. Examples

  1. Examples
    • Compressing a file:

      This will compress the file "file1" and rename it to "file1.gz":
      $ touch file1
      
      $ ls -al file1
      -rw-r--r-- 1 mtitek mtitek 5632 Feb  3 21:01 file1
      
      $ gzip file1
      
      $ ls -al file1.gz
      -rw-r--r-- 1 mtitek mtitek 242 Feb  3 21:01 file1.gz
      
    • Decompressing a file:

      • This will decompress the file "file1.gz" and rename it to "file1":
        $ gzip -d file1.gz
        
        $ ls -al file1
        -rw-r--r-- 1 mtitek mtitek 5632 Feb  3 21:01 file1
        
    • Decompressing a file (gunzip):

      • You can use gunzip to decompress the file "file1.gz" and rename it to "file1":
        $ gunzip file1.gz
        
        $ ls -al file1
        -rw-r--r-- 1 mtitek mtitek 5632 Feb  3 21:01 file1
        
    • Keeping the original file during compression:

      • Use the -k option to keep the original file:
        $ gzip -k file1
        
        $ ls -al file1*
        -rw-r--r-- 1 mtitek mtitek 5632 Feb  3 21:01 file1
        -rw-r--r-- 1 mtitek mtitek  242 Feb  3 21:01 file1.gz
        
    • Viewing compression information:

      • Use the -l option to view compression statistics:
        $ gzip -l file1.gz
                 compressed        uncompressed  ratio uncompressed_name
                        242                5632  96.8% file1
        
    • Compressing multiple files:

      • You can compress multiple files at once:
        $ gzip file1 file2 file3
        
        $ ls -al file*.gz
        -rw-r--r-- 1 mtitek mtitek 242 Feb  3 21:01 file1.gz
        -rw-r--r-- 1 mtitek mtitek 156 Feb  3 21:01 file2.gz
        -rw-r--r-- 1 mtitek mtitek 089 Feb  3 21:01 file3.gz
        
    • Compressing with different compression levels:

      • Use -1 for fastest compression or -9 for best compression:
        $ gzip -1 file1    # fastest compression
        $ gzip -9 file1    # best compression
        
    • Testing compressed files:

      • Use the -t option to test file integrity:
        $ gzip -t file1.gz
        
        If the file is corrupted, you'll see an error message. No output means the file is intact.
    • Compressing to stdout:

      • Use -c to write to stdout and keep the original file:
        $ gzip -c file1 > file1.gz
        
        $ ls -al file1*
        -rw-r--r-- 1 mtitek mtitek 5632 Feb  3 21:01 file1
        -rw-r--r-- 1 mtitek mtitek  242 Feb  3 21:01 file1.gz
        
    • Viewing compressed files without decompressing:

      • Use zcat to view the contents of compressed files:
        $ zcat file1.gz
        
        You can also use zless or zmore for paging through large files.
© 2025 mtitek