2

I have the following task:

Write a program that, given a directory as argument, makes a tar file compressed with gzip and which name should be in the form "dirNameYYYY-MM-DD.tar.gz". If said file exists, it should return an error message.

What I tried:

#!/bin/bash
dir=$1
tarName=$dir`date '+%Y-%m-%d'`.tar.gz
if [ -e $tarName]
then
echo "That file already exists."
else
tar -cvz $HOME/$tarName '/'$dir *
fi

Why is this not working? Any suggestions on how I could fix it?

1
  • 1
    This furiously looks like homework, but whatever: Shouldn't it be ${dir}/* at the last line? Commented Jul 4, 2015 at 14:25

1 Answer 1

2

Try:

#!/bin/bash
dir=$1
tarName=$(basename $dir)$(date '+%Y-%m-%d').tar.gz
if [ -e $tarName ]
then
  echo "That file already exists."
else
  tar -cvzf $tarName $dir 
fi
Sign up to request clarification or add additional context in comments.

3 Comments

Add a space before ].
Thanks, that worked. What does the $(date) do vs `date`?
In general, is the same. But Posix and with a syntax that usually causes less errors.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.