02Linux-shell脚本(重要)

 1、给一个目录,遍历该目录下的所有文件

#!/bin/sh

show(){
 cd $1
 for i in `ls`
        do
           if [ -d "$i" ]; then
                 show "$i"

           else
                 echo `pwd`"/""$i"
           fi
    done
  cd ..
}

show $1

exit 0

2、使用rsync命令推送文件,并删除7天前的备份文件

#!/bin/sh

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"

#备份
tar -zcvf /backup/bak_$(date +%F_%s).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www  /app/logs  &>/dev/null  &&\

#删除7天以前的备份
find /backup/ -type f -name "*.tar.gz" -mtime +7 | xargs rm -rf  &&\

#推送
rsync -avz /backup rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password &>/dev/null

3、使用rsync脚本推送文件,根据ip地址来创建文件。

#!/bin/sh

export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"

#定义IP
IP=`hostname -i`

mkdir -p /backup/$IP/

#备份  ${IP}  是引用IP,防止出各种问题
tar -zcvf /backup/$IP/bak_${IP}_$(date +%F_%s).tar.gz /var/spool/cron/root /etc/rc.local /server/scripts /var/html/www  /app/logs  &>/dev/null  &&\

#删除7天以前的备份
find /backup/ -type f -name "*.tar.gz" -mtime +7 | xargs rm -rf  &&\

#推送
rsync -avz /backup/ rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password &>/dev/null

4、统计某个文件下的txt文件数量,超过515个就停止统计

#!/bin/bash
dir_path="/your/dir/path" # 替换为目标目录路径
while true; do
  count=0
  for file in $dir_path/*.txt; do
    if [ -f "$file" ]; then
      ((count++))
    fi
  done
  if [ $count -lt 515 ]; then
    echo "The number of txt files in $dir_path is $count, less than 515. Waiting for 10 seconds to check again..."
    sleep 10
  else
    echo "The number of txt files in $dir_path is $count, meets the requirement."
    break
  fi
done

5、统计某个文件下的txt文件数量,超过515个就停止统计。不满515个就一直循环检测

#!/bin/bash
dir_path="/your/dir/path" # 替换为目标目录路径
while true; do
  count=0
  for file in $dir_path/*.txt; do
    if [ -f "$file" ]; then
      ((count++))
    fi
  done
  if [ $count -lt 515 ]; then
    echo "The number of txt files in $dir_path is $count, less than 515. Waiting for 10 seconds to check again..."
    sleep 10
  else
    echo "The number of txt files in $dir_path is $count, meets the requirement."
    break
  fi
done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值