2012-03-20 104 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"` 
相關問題