LINUX Shell:複製資料夾的指令碼
阿新 • • 發佈:2019-01-24
#!/bin/sh
# cpdir source_dir target_dir
# author hjack
# date:2006.3.25
# copy dir
#########variables define##########
SOURCE_DIR=$1
TARGET_DIR=$2
CURRENT_DIR=`pwd`
SOURCE_FILE=""
#########main start here###########
##check the argements.
if [ $# -lt 2 ]; then
echo "request more arguments."
exit
fi
##does the target dir exist?
if [ ! -d $TARGET_DIR ]; then
echo "not such dir."
exit
fi
if [ -e $SOURCE_DIR ] ; then
##sourcedir string ends with '/'.
echo $SOURCE_DIR | grep /$ >/dev/null
if [ $? -eq 0 ];then
##get the file or dir name.eg:/home/user1/test,it will get test.
SOURCE_FILE=$(echo $SOURCE_DIR | awk -F/ '{print$(NF-1)}')
else
SOURCE_FILE=$(echo $SOURCE_DIR | awk -F/ '{print$NF}')
fi
else
echo "not such dir."
exit
fi
##first,compress the dir.
tempFile="tmp.cpdir.0123456789".$SOURCE_FILE".tgz"
tar zcf $tempFile $SOURCE_FILE
#then ,decompress the tar file.
mv $tempFile $TARGET_DIR
cd $TARGET_DIR
tar zxf $tempFile
rm $tempFile
cd $CURRENT_DIR
echo "copy finished."
# cpdir source_dir target_dir
# author hjack
# date:2006.3.25
# copy dir
#########variables define##########
SOURCE_DIR=$1
TARGET_DIR=$2
CURRENT_DIR=`pwd`
SOURCE_FILE=""
#########main start here###########
##check the argements.
if [ $# -lt 2 ]; then
echo "request more arguments."
exit
fi
##does the target dir exist?
if [ ! -d $TARGET_DIR ]; then
echo "not such dir."
exit
fi
if [ -e $SOURCE_DIR ] ; then
##sourcedir string ends with '/'.
echo $SOURCE_DIR | grep /$ >/dev/null
if [ $? -eq 0 ];then
##get the file or dir name.eg:/home/user1/test,it will get test.
SOURCE_FILE=$(echo $SOURCE_DIR | awk -F/ '{print$(NF-1)}')
else
SOURCE_FILE=$(echo $SOURCE_DIR | awk -F/ '{print$NF}')
fi
else
echo "not such dir."
exit
fi
##first,compress the dir.
tempFile="tmp.cpdir.0123456789".$SOURCE_FILE".tgz"
tar zcf $tempFile $SOURCE_FILE
#then ,decompress the tar file.
mv $tempFile $TARGET_DIR
cd $TARGET_DIR
tar zxf $tempFile
rm $tempFile
cd $CURRENT_DIR
echo "copy finished."