guest@linuxthings: ~/guides/archiving.md

guest@linuxthings:~/guides$ man archiving

Backups & Archives

tar (Tape Archive)

tar -cvf archive.tar dir/    # Create archive (Create, Verbose, File)
tar -xvf archive.tar         # Extract archive (eXtract)
tar -tvf archive.tar         # List contents without extracting (Table)
tar -czf archive.tar.gz dir/ # Create gzipped archive
tar -xzf archive.tar.gz      # Extract gzipped archive
tar -cjf archive.tar.bz2 dir/  # Create bzip2-compressed archive
tar -cJf archive.tar.xz dir/   # Create xz-compressed archive (smallest, slowest)
tar -xf archive.tar.xz       # Modern tar detects the compression itself
tar -xf archive.tar.gz -C /tmp/out  # Extract into another directory
tar -xf archive.tar.gz dir/file.txt # Extract a single file
tar -czf backup.tar.gz --exclude='*.log' dir/  # Leave some files out

The f option must be followed by the archive name, so keep it last in a group like -czf. Before extracting an archive from an unknown source, list it with -t to make sure it won't scatter files or overwrite something important.

Option

Compression

Extension

-z

gzip

.tar.gz or .tgz

-j

bzip2

.tar.bz2

-J

xz

.tar.xz

--zstd

zstd

.tar.zst

Compression Tools

gzip file                  # Compresses to file.gz (removes the original)
gzip -k file               # Keep the original file
gunzip file.gz             # Uncompresses
gzip -9 file               # Best (slowest) compression
bzip2 file                 # Compress to file.bz2
bunzip2 file.bz2           # Uncompress it
xz file                    # Compress to file.xz
unxz file.xz               # Uncompress it
zip -r archive.zip dir/    # Create zip file
unzip -l archive.zip       # List a zip file's contents
unzip archive.zip          # Extract zip file
unzip archive.zip -d out/  # Extract into a directory
7z a archive.7z dir/       # Create a 7-Zip archive
7z x archive.7z            # Extract it

rsync

rsync -a source/ dest/     # Sync locally (Archive mode)
rsync -avz source/ user@host:dest/  # Sync to remote (Archive, Verbose, Compress)
rsync -av user@host:dir/ local/     # Pull from a remote host
rsync -av --progress big.iso host:  # Show progress per file
rsync -avn source/ dest/   # Dry run: show what would change
rsync -av --delete source/ dest/    # Make dest an exact mirror (deletes extras)
rsync -av --exclude='node_modules' src/ dest/

A trailing slash matters. rsync -a dir dest/ creates dest/dir, while rsync -a dir/ dest/ copies only the contents of dir into dest. Always do a dry run (-n) before using --delete.

Use -z only over slow network links; compressing a local copy just wastes CPU.

Backups to the Cloud (rclone)

rclone config                       # Set up a remote (Google Drive, S3, ...)
rclone ls remote:backups            # List remote files
rclone copy dir remote:backups/dir  # Upload, never deleting anything
rclone sync dir remote:backups/dir  # Mirror local to remote (deletes extras)
rclone sync -n dir remote:backups/dir  # Dry run first

Disk Images (dd)

dd overwrites its output without asking. Double-check of= before pressing Enter.
sudo dd if=/dev/sdb of=usb.img bs=4M status=progress   # Image a whole disk
sudo dd if=distro.iso of=/dev/sdb bs=4M status=progress conv=fsync  # Write an ISO to USB

Encrypting Archives (gpg)

gpg -c backup.tar.gz       # Encrypt with a passphrase (creates .gpg)
gpg -d backup.tar.gz.gpg > backup.tar.gz  # Decrypt

guest@linuxthings:~/guides$