Trying to prevent myself from another home fiasco, I've decided to build a simple script to backup some of my directories. There are a lot of scripts / backup tools out there, but I just wanted to build my own for the fun of it, and since what I really wanted was to create a tar.gz of the contents of a specific directory (no root path), including hidden files, and without the need to support incremental backups. Take a look at the script: ====================== #!/bin/bash function showHelp() { if [ "$1" != "" ]; then echo "ERROR: $1" echo "" fi echo "Usage: $0 " echo "where:" echo " folder to backup" echo " where to save the backup (.tar.gz file)" } if [ $# -ne 2 ]; then showHelp elif [ ! -d "$1" ]; then showHelp "Directory $1 does not exist" exit 1 else echo -n "Creating $2 from $1: " if [ -f "$2" ]; then mv "$2" "$2.previous" fi tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null if [ $? -ne 0 ]; then echo "ERROR" exit 1 else echo "DONE" if [ -f "$2.previous" ]; then rm -f "$2.previous" fi fi fi ======================== The line that probably looks scary is: Code: tar -cpzf "$2" --exclude="$2" --ignore-failed-read --transform="s/"`echo "$1" | sed -e 's/^\\///g' | sed -e 's/\\/$//g' | sed s/\\\//'\\\\'\\\//g`"//" "$1" &>/dev/null The complicated part comes from the transform expression, which is used to get rid of the absolute path to the directory we are backing up. The tar --directory option here is not useful since it adds the "." special path to the packaged file, so let's break it down: * echo "$1": the folder to backup * sed -e 's/^\\///g': remove starting slash, if any * sed -e 's/\\/$//g': remove trailing slash, if any * sed s/\\\//'\\\\'\\\//g`: convert forward slash to escaped slashes (so every / is replaced by \/)