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