Home / CodeShell / Shell / Bash Script to Backup and Compress Directories with Timestamp

Bash Script to Backup and Compress Directories with Timestamp

Bash Script to Backup and Compress Directories with Timestamp
  • Category Shell
  • Type Command
  • Platform Linux
  • Language Bash
  • Price Free
  • Copy 1 128
  • Comments 0
Go to Code
Bash Script to Backup and Compress Directories with Timestamp

Bash Script to Backup and Compress Directories with Timestamp

Copy this Bash snippet on Clayi Code — one click, no account required. Free command snippets with syntax highlighting and instant copy — built for developers and sysadmins.

What it does and when to use

A ready-to-run terminal command you can paste into your shell and execute. Handy for file search, disk usage, permissions, networking, process management, and everyday admin work.

How to copy from Clayi Code

Scroll to the code box, click Copy, and paste into your terminal or SSH session.

Run it on the right shell

Do not paste Bash into CMD, or PowerShell into Bash. Use the shell that matches your system.

Command safety

Read the full command before running. Some one-liners can change or delete data — test on non-production systems when unsure. Watch quoting, paths, and sudo usage; use bash -n or ShellCheck on longer scripts.

Shell reference

Category: Shell. Language: Bash. Platform: Linux. Format: Command.

Popularity
0%
  • Votes: 98
  • Comments: 0

Bash Script to Backup and Compress Directories with Timestamp

#!/usr/bin/env bash
# Create a timestamped tar.gz backup of a specific directory
# Usage: ./backup.sh [directory_to_backup] [backup_destination]

SOURCE_DIR="$1"
BACKUP_DIR="${2:-./backups}"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")

if [ -z "$SOURCE_DIR" ]; then
    echo "Error: Specify a source directory."
    exit 1
fi

mkdir -p "$BACKUP_DIR"
BACKUP_FILE="$BACKUP_DIR/backup_$(basename "$SOURCE_DIR")_$TIMESTAMP.tar.gz"

tar -czf "$BACKUP_FILE" "$SOURCE_DIR"
echo "Backup created successfully: $BACKUP_FILE"
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.

Similar Snippets

Populer Snippets

There are no comments yet :(

Bash Script to Backup and Compress Directories with Timestamp
Tell us what you think about "Bash Script to Backup and Compress Directories with Timestamp"
Information
Users of Guests are not allowed to comment this publication.