shell脚本:备份文件

1.备份文件
1
2
3
4
5
6
7
8
9
10
#!/usr/bin/env bash
 
date=$(date "+%Y%m%d")
file_path="/root/zy/test/bak/bak.txt"
if [[ -f ${file_path}.${date} ]];then
    num=$(ls -l $file_path.${date}*| wc -l)
    cp $file_path ${file_path}.${date}_${num}
else
    cp $file_path ${file_path}.${date}
fi

  

2.备份文件(写成函数)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/env bash
bak(){
  date=$(date "+%Y%m%d")
  file_path=$1
  if [[ -f ${file_path}.${date} ]];then
      num=$(ls -l $file_path.${date}*| wc -l)
      cp $file_path ${file_path}.${date}_${num}
  else
      cp $file_path ${file_path}.${date}
  fi
}
 
# 使用函数
bak json.json

  

3.备份文件(备份某个文件,加上使用方法提示)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/env bash<br># 错误使用参数,则提示
if [ "$#" -ne 1 ]; then
    echo -e "\033[32mUsage:\n\t$0 <filepath>\033[0m"
    echo -e "for example:\n\t $0 log.log"
    echo -e "\t $0 /root/log.log"
    exit 1
fi
 
date=$(date "+%Y%m%d")
file_path=$1
if [[ -f ${file_path}.${date} ]];then
    num=$(ls -l $file_path.${date}*| wc -l)
    cp $file_path ${file_path}.${date}_${num}
else
    cp $file_path ${file_path}.${date}
fi

  

posted @   铿锵有力自信且坚定  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示