Archive and Backup
Archiving and backing up files in Linux is essential for data protection and management. Various tools and commands can help you achieve this, including tar, gzip, bzip2, and xz.
tar - Tape Archiver
The tar command is used to create archive files and extract them. It can also compress the archives using gzip.
Commands:
-
To "compress" the files and print out a verbose output:
tar -cvf <file.tar> <file-or-dir>where:
c: Create a new archive.v: Verbose, list files processed.f: File name of the archive.
-
To compress and zip the files/directories in one command:
tar czvf <file.tar.gz> <files-or-dir>z: Compress the archive usinggzip.
-
To compress and zip the files/directories, excluding specific files:
tar cvzf bkup.tar.gz --exclude=file1 <files-or-dir> -
To compress the
tararchive usinggzip:gzip <file.tar> -
To extract the contents of a compressed
tarfile:tar xzvf <file.tar.gz> -
To extract the contents into a specific directory:
tar xvf <file.tar.gz> -C /tmp/dir2 -
To list the contents of a
tarfile without extracting:tar --list -f bkup.tar -
For a detailed list including permissions and owners:
tar tvf bkup.tar
gzip, bzip2, xz
These commands are used to compress and decompress files. gzip is the most common compression utility.
gzip
-
To zip a file, note that it replaces the original file with the zipped version:
gzip <file> -
To keep the original file, add the
-kflag:gzip -k <file> -
To unzip a file:
gunzip <file.gz>
bzip2
-
To compress a file using
bzip2:bzip2 <file>
xz
-
To compress a file using
xz:xz -k <file> -
To view detailed help for
bzip2:bzip2 --help -
To compress a file using
bzip2:bzip2 sample.tar -
To compress a file using
xzand keep the original file:xz -k sample.tar