Shell:将某一目录下的所有同名文件拷贝到指定目录下,并将目标文件按时间备份
程序员文章站
2022-06-16 17:19:59
...
dir=/root/testA
#time_f=$(date "+%Y.%m.%d")
#time1=$(date)
time_f=$(date "+%Y-%m-%d_%H:%M:%S")
echo $time_f
for file in $dir/*; do
temp_file=`basename $file`
echo $temp_file
#name = ${file##*/}
name_bak="$temp_file"_"$time_f"
cp -rf /root/testA/$temp_file /root/testB/$name_bak
echo $file
done
dir=/root/testB
for file in $dir/*; do
echo $file
done
-rw-r--r-- 1 root root 0 Jun 21 19:28 a.txt
-rw-r--r-- 1 root root 0 Jun 21 19:28 b.txt
-rw-r--r-- 1 root root 0 Jun 21 19:28 c.txt
-rw-r--r-- 1 root root 0 Jun 21 19:28 a.txt
-rw-r--r-- 1 root root 0 Jun 21 20:59 a.txt_2021-06-21_20:59:32
-rw-r--r-- 1 root root 0 Jun 21 19:28 b.txt
-rw-r--r-- 1 root root 0 Jun 21 20:59 b.txt_2021-06-21_20:59:32
-rw-r--r-- 1 root root 0 Jun 21 19:28 c.txt
-rw-r--r-- 1 root root 0 Jun 21 20:59 c.txt_2021-06-21_20:59:32
dir=/opt/js
#time_f=$(date "+%Y.%m.%d")
#time1=$(date)
time_f=$(date "+%Y-%m-%d_%H:%M:%S")
echo $time_f
target_dir=/usr/src/js/
for file in $dir/*; do
temp_file=`basename $file`
echo $temp_file
#name = ${file##*/}
name_bak="$temp_file"_"$time_f"
if [ -f $target_dir$temp_file ];then
cp -rf $target_dir$temp_file $target_dir$name_bak
fi
cp -rf $file $target_dir$temp_file
echo $file
done
上一篇: 文件拷贝到指定目录下面