Preserve files permissions and ownership.
The Basics
1# Archive
2tar cvf my_archive.tar <file1> <file2> </dir/folder/>
3
4## Archive and compress with zstd everything in the current dir and push to /target/dir
5tar -I zstd -vcf archive.tar.zstd -C /target/dir .
6
7# Extract
8tar xvf my_archive.tar
9
10# Extract and push to target dir
11tar -zxvf new.tar.gz -C /target/dir
Other useful options • t : list archive’s content. • T : archive list given by a file. • P : preserve absolute path (useful for backing up /etc). • X : exclude. • z : gunzip compression. • j : bzip2 compression. • J : lzma compression.
Other tricks
1# move the whole directory tree to /opt_bis
2tar cf - . | (cd /opt_bis ; tar xf - )
3
4# Corrupt package - Unexpected EOF in archive
5gunzip -c jakarta-tomcat-5.0.30.tar.gz | tar -xvf -
1# Archive with cpio
2ls | cpio -ov > /tmp/object.cpio
3
4# Extract
5cpio -idv < /tmp/object.cpio
6
7# Extract and create the needed directories
8cpio -i -make-directories
9
10# With format
11ls | cpio -ov -H tar -F sample.tar
12cpio -idv -F sample.tar
13
14# Check the content of a tar archive
15cpio -it -F sample.tar
16
17find /my/dir/to/move/ -depth | cpio -pmdv /mnt/out : move a whole tree to another directory (without creating an archive)
18 • -p makes cpio use pass-through mode. It's like piping cpio -o into cpio -i.
19 • -d creates leading directories as needed in the target directory.
20find . -print | cpio -pdmvu /opt_bis
Created by POSIX, less popular than tar.
1pax -wf my_archive.pax -x pax file1 file2 : Archive
2pax -wf my_archive.pax -x pax dir1/ : Archive
3pax -rf my_archive.pax : Extract
4pax -rzf my_archive.pax : Extract a gzip archive
The main pax options are the following, and can be freely combined: • w / r : build / extract the archive; • f : use the file given as argument; • x <my_format> : archive format, default “ustar”. • z : gunzip compression • j : bzip2 compression