2012-03-20 141 views
0

我试图将目录中的所有文件(回显1)tar归档目录中的[目录名称] .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 
+0

这不是CodeReview.SE。 – 2012-03-20 03:24:45

+1

可能的重复[如何提示用户输入文件夹的名称以进行存档,然后提示用户输入存储文件夹的名称?](http://stackoverflow.com/questions/9780649/怎么办,我提示符-的用户换了名称对的一文件夹到归档,然后提示符-的-US) – 2012-03-20 03:25:08

回答

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"` 
相关问题