DEV Community

Peter Lau
Peter Lau

Posted on • Originally published at quickanswer.awesomefe.dev

CLI |> Compress and Decompress via CLI

IF YOU JUST SOLVED SOME PROBLEM, WELCOME TO DM qaRabbit ON TWITTER, OR CREATE A PULL REQUEST IN GITHUB REPO.

macOS

  • file.zip

    extract to the current directory

    unzip file.zip

    extract to the target directory

    unzip file.zip -d target_dir_path

    compress to file.zip

    zip file path1 path2 path3

  • file.tar (archive, no compress)

    extract to the current directory

    tar -xvf file.tar

    extract to target_dir_path (must be created first)

    mkdir -p target_dir_path && tar -C target_dir_path -xvf file.tar

    compress path

    tar -cvf file.tar path1 path2

  • file.tar.gz file.tgz

    tar.gz file is a Tar archive compressed with Gzip

    extract to the current directory

    tar -zxvf file.tar.gz

    extract to target_dir_path (must be created first)

    mkdir -p target_dir_path && tar -C target_dir_path -zxvf file.tar.gz

    compress path

    tar -czvf file.tar.gz path1 path2 ...

    compress with exclude

    tar -czvf file.tar.gz path --exclude=sub_path1 --exclude=sub_path2

    tar -czvf file.tar.gz path --exclude={sub_path1, sub_path2}

    tar -czvf file.tar.gz path --exclude=*.ext

  • file.tar.xz

    tar.gz file is a Tar archive compressed with Xz a LZMA based compress algorithm

    tar auto-detects compression type and extracts the archive

    tar -xf file.tar.xz

  • file.tar.bz2 file.tbz

    tar.bz2 file is a Tar archive compressed with Bzip2

    extract to the current directory

    tar -xvf file.tar.bz2

    extract to target_dir_path (--directory, -C; must be created first)

    mkdir -p target_dir_path && tar -xvf file.tar.bz2 -C target_dir_path

    extract specific files

    tar -xf archive.tar.bz2 file1 file2 dir1 dir2

    tar -xf archive.tar.bz2 --wildcards '*.md'

    extract from stdin

    wget -c ftp://ftp.vim.org/pub/vim/unix/vim-8.1.tar.bz2 -O - | sudo tar -xj

    list archive content (--list, -t)

    tar -tvf file.tar.bz2

    compress path

    tar -cjvf file.tar.bz2 path1 path2

GNU/Linux


References:

Top comments (0)