0

I'm trying to tar all of the files in a directory (echo 1) to a tar archive called [directory name].tar in the archive directory (second echo). Is this correct ?

#!/bin/bash

#Author
#Josh
++++++++++++++++++++++++++
echo "Please enter the name of a folder to archive" 
++++++++++++++++++++++++++
echo "Please enter a foldername to store archives in"
++++++++++++++++++++++++++
touch fname
+++++++++++++++++++++++++
tar fname2.tar
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user1221987
  • 121
  • 1
  • 2
  • 5
  • 1
    possible duplicate of [How do I prompt the user for the name of a folder to archive, then prompt the user for the name of a folder to store the archives in ?](http://stackoverflow.com/questions/9780649/how-do-i-prompt-the-user-for-the-name-of-a-folder-to-archive-then-prompt-the-us) – Ignacio Vazquez-Abrams Mar 20 '12 at 03:25

1 Answers1

2
dir_to_be_tarred=
while [ ! -d "$dir_to_be_tarred" ]; do      
    echo -n "Enter path to directory to be archived: "   # -n makes "echo" not output an ending NEWLINE
    read dir_to_be_tarred
    if [ ! -d "$dir_to_be_tarred" ]; then   # make sure the directory exists
        echo "Directory \"$dir_to_be_tarred\" does not exist"       # use quotes around the directory name in case user enter an empty line
    fi
done

storage_dir=
while [ ! -d "$storage_dir" ]; do
    echo -n "Enter path to directory where to store the archive: "
    read storage_dir
    if [ ! -d "$storage_dir" ]; then
        echo "Directory \"$storage_dir\" does not exist"
    fi
done

tarfile="$storage_dir"/`date '+%Y%m%d_%H%M%S'`.tar.gz       # the tar archive is named "YYYYMMDD_HHMMSS.tar.gz"
tar cfz "$tarfile" "$dir_to_be_tarred"
echo 'Your archive is in:
'`ls -l "$tarfile"`
aqn
  • 2,542
  • 1
  • 16
  • 12