文件筛选并移动脚本

某文件夹下有如下文件
fc-rk3588-homedaily_20230815_180320.info.txt
fc-rk3588-homedaily_20230815_180320.tar.gz
fc-rk3588-homedaily_20230816_021720.info.txt
fc-rk3588-homedaily_20230816_021720.tar.gz
fc-rk3588-homedaily_20230817_021955.info.txt
fc-rk3588-homedaily_20230817_021955.tar.gz
fc-rk3588-homedaily_20230817_080832.info.txt
fc-rk3588-homedaily_20230817_080832.tar.gz
fc-rk3588-homedaily_20230818_022042.info.txt
fc-rk3588-homedaily_20230818_022042.tar.gz
fc-rk3588-homedaily_20230819_022312.info.txt
fc-rk3588-homedaily_20230819_022312.tar.gz
fc-rk3588-homedaily_20230819_140743.info.txt
fc-rk3588-homedaily_20230819_140743.tar.gz
fc-rk3588-homedaily_20230820_022519.info.txt

用shell筛选出以fc-rk3588-homedaily_开头20231001(不包含)之前的文件

#!/bin/bash

# 指定文件夹路径
folder_path="./"
dest_path="/mnt/A/storage/nightly-backup"

# 在指定文件夹中查找所有以fc-rk3588-homedaily_开头的文件
while read -r file; do
  # 提取文件名中的日期部分
  date_part=$(echo "$file" | grep -oP '(?<=fc-rk3588-homedaily_)\d{8}_\d{6}')
  
  # 将日期格式转换为YYYYMMDDHHMMSS形式以便比较
  date_formatted=$(echo "$date_part" | sed 's/_//;s/-//;s/://g')
  

  # 检查日期是否在20231001之前
  if [ "$date_formatted" -lt "20231001000000" ]; then
     #echo "$file"
     file_size=$( du -k "$file" |awk '{print $1}' )
     total_size=$(( total_size + file_size  ))
     mv $file $dest_path
     
  fi
done < <(find "$folder_path" -type f -name "fc-rk3588-homedaily_*")

echo $total_size

你可能感兴趣的:(linux)