0

I would like to call the script ./archive-it.sh with folder name ( like project1 ) without trailing / and expect to archive it, I am sure its pretty simple, as I am new to bash script i am stuck. Can someone help.

but currently i am getting error here

$target=$(echo "$1"-`date +%m-%d-%Y-%H_%M`)

Here is the script

!/bin/bash                        
######### archive-it.sh ############

echo "Argument  $1";
$target=$(echo "$1"-`date +%m-%d-%Y-%H_%M`)
tar -czvf $target.tar.gz  $1  

echo "####################################"
echo "ARCHIVE Successfully done at :"
echo "${target}" ; 

also suggest me if its good to exclude .git folder, as I am taking only backup on a daily basis.

3 Answers 3

3

To assign a variable, it's target="something", then to access it you use $target

Also add quotes around $1 if the path contains spaces

echo "Argument  $1";

if [[ ! -d "${1}" ]]; then
    echo "Folder '$1' doesn't exists"
    exit 1
fi

#filter git files if you want
nogitfiles="$(find "${1}" -name * | grep -v .git)"

target="$(date +"$1-%m-%d-%Y-%H_%M")"
tar -czvf "${target}.tar.gz"  ${nogitfiles}  

echo "####################################"
echo "ARCHIVE Successfully done at :"
echo "${target}" ; 
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need the echo at all:

target="$1$(date +%m-%d-%Y-%H_%M)"

Comments

0

In addition to assigning to target, not $target, and instead of concatenating $1 and the output of date, you can include $1 in the format string for date:

target=$(date +"$1-%m-%d-%Y-%H_%M")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.