温馨提示×

centos extract官方文档

小樊
56
2025-10-24 01:28:52
栏目: 智能运维

Note: CentOS does not include an official extract command by default. The term “extract” typically refers to using standard command-line tools like tar, unzip, or 7z for decompressing files. Below is a consolidated guide to common extraction tasks in CentOS, based on official documentation and community best practices.

Common Extraction Tools in CentOS

CentOS relies on the following tools for handling compressed files:

  • tar: For .tar, .tar.gz, .tar.bz2, .tar.xz (and other tar-based formats).
  • unzip: For .zip files.
  • 7z: For .7z files (requires installation via yum or dnf).

1. Using tar for Tar-Based Archives

tar is the most versatile tool for extracting tar-based archives (common in CentOS repositories and software bundles).

Basic Syntax

tar -xvf <archive_name>.tar[.gz|.bz2|.xz] [-C <target_directory>]
  • -x: Extract files.
  • -v: Verbose mode (shows extracted files).
  • -f: Specifies the archive file name.
  • -C: Optional; sets the target directory for extraction.

Examples

  • Extract a .tar file (no compression):
    tar -xvf archive.tar
    
  • Extract a .tar.gz file (gzip-compressed):
    tar -xzvf archive.tar.gz
    
  • Extract a .tar.bz2 file (bzip2-compressed):
    tar -xjvf archive.tar.bz2
    
  • Extract to a specific directory (e.g., /opt/myapp):
    tar -xzvf archive.tar.gz -C /opt/myapp
    

View Archive Contents Without Extracting

tar -tzvf archive.tar.gz  # For .tar.gz files
tar -tjf archive.tar.bz2  # For .tar.bz2 files

2. Using unzip for ZIP Files

For .zip archives (common in cross-platform scenarios), install unzip first if not available:

sudo yum install unzip

Basic Syntax

unzip <archive_name>.zip [-d <target_directory>]
  • -d: Optional; sets the target directory.

Examples

  • Extract a .zip file to the current directory:
    unzip archive.zip
    
  • Extract to a specific directory (e.g., ~/downloads):
    unzip archive.zip -d ~/downloads
    

3. Using 7z for 7z Files

For .7z archives (high-compression format), install the p7zip package:

sudo yum install p7zip p7zip-plugins

Basic Syntax

7z x <archive_name>.7z [-o<target_directory>]
  • x: Extract with full paths.
  • -o: Optional; sets the target directory (no space after -o).

Example

  • Extract a .7z file to the current directory:
    7z x archive.7z
    
  • Extract to a specific directory (e.g., /tmp):
    7z x archive.7z -o/tmp
    

Key Notes

  • Permissions: Use sudo for extraction to system directories (e.g., /usr/local) if you encounter permission errors.
  • Overwrite Protection: By default, tools like tar and unzip will prompt before overwriting existing files. Use -f (force) with tar to overwrite silently (e.g., tar -xzf archive.tar.gz -C /opt/myapp --overwrite).
  • Excluding Files: With tar, use --exclude to skip specific files/directories (e.g., tar -xzvf archive.tar.gz --exclude='*.log').

For advanced use cases (e.g., extracting specific files from an archive), refer to the tool’s man pages:

man tar
man unzip
man 7z

0