archive

All posts tagged archive by Linux Bash
  • Posted on

    The tar command in Bash is commonly used to create archives of files and directories. It can compress or just archive the data, and it supports several formats such as .tar, .tar.gz, .tar.bz2, .tar.xz, etc.

    Here's a breakdown of how you can use tar for various purposes:

    1. Creating an Archive (without compression)

    To create a .tar archive from files or directories:

    tar -cvf archive_name.tar /path/to/directory_or_file
    
    • -c: Create a new archive
    • -v: Verbose mode (optional, shows the progress)
    • -f: Specify the name of the archive

    Example:

    tar -cvf backup.tar /home/user/documents
    

    This will create an archive backup.tar containing the contents of the /home/user/documents directory.

    2. Creating a Compressed Archive

    You can compress the archive using different compression algorithms:

    a. With gzip (creates a .tar.gz or .tgz file):

    tar -czvf archive_name.tar.gz /path/to/directory_or_file
    
    • -z: Compress with gzip

    Example:

    tar -czvf backup.tar.gz /home/user/documents
    

    b. With bzip2 (creates a .tar.bz2 file):

    tar -cjvf archive_name.tar.bz2 /path/to/directory_or_file
    
    • -j: Compress with bzip2

    Example:

    tar -cjvf backup.tar.bz2 /home/user/documents
    

    c. With xz (creates a .tar.xz file):

    tar -cJvf archive_name.tar.xz /path/to/directory_or_file
    
    • -J: Compress with xz

    Example:

    tar -cJvf backup.tar.xz /home/user/documents
    

    3. Extracting an Archive

    To extract files from a .tar archive:

    tar -xvf archive_name.tar
    

    For compressed archives, replace .tar with the appropriate extension (e.g., .tar.gz, .tar.bz2).

    Extracting .tar.gz:

    tar -xzvf archive_name.tar.gz
    

    Extracting .tar.bz2:

    tar -xjvf archive_name.tar.bz2
    

    Extracting .tar.xz:

    tar -xJvf archive_name.tar.xz
    

    4. Listing the Contents of an Archive

    To see the contents of a .tar file without extracting it:

    tar -tvf archive_name.tar
    

    For compressed files, you can use the same command but replace the extension appropriately.

    5. Extracting to a Specific Directory

    If you want to extract files to a specific directory, use the -C option:

    tar -xvf archive_name.tar -C /path/to/extract/directory
    

    6. Adding Files to an Existing Archive

    To add files or directories to an existing archive:

    tar -rvf archive_name.tar /path/to/new_file_or_directory
    
    • -r: Append files to an archive

    7. Excluding Files from an Archive

    To exclude specific files or directories while archiving:

    tar -cvf archive_name.tar --exclude='*.log' /path/to/directory
    

    This command excludes all .log files from the archive.

    8. Extracting Specific Files from an Archive

    To extract a specific file from an archive:

    tar -xvf archive_name.tar path/to/file_within_archive
    

    This will extract only the specified file from the archive.

    Summary of Useful tar Options:

    • -c: Create an archive
    • -x: Extract an archive
    • -v: Verbose output
    • -f: Specify the archive file name
    • -z: Compress using gzip
    • -j: Compress using bzip2
    • -J: Compress using xz
    • -C: Extract to a specific directory
    • --exclude: Exclude specific files or directories
    • -r: Append files to an existing archive
    • -t: List contents of an archive

    These are some of the common usages of tar to archive and compress files in Bash.