tar目录中的文件并将存档放在另一个目录中

时间:2012-03-20 03:23:36

标签: linux bash unix tar

我正在尝试将目录(echo 1)中的所有文件tar到归档目录(第二个echo)中名为[directory name] .tar的tar归档文件中。这是对的吗?

#!/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

1 个答案:

答案 0 :(得分: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"`