find /root/app/tcu/data/log/.log -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.0 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.1 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.2 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.3 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.4 -mtime +2 -type f |xargs -i gzip {} # /root/app/tcu/data/ccuLog find /root/app/tcu/data/ccuLog/.log -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/ccuLog/.log.0 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/ccuLog/.log.1 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/ccuLog/.log.2 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/ccuLog/.log.3 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/ccuLog/.log.4 -mtime +2 -type f |xargs -i gzip {} # /root/app/remote/data/log find /root/app/remote/data/log/.log -mtime +2 -type f |xargs -i gzip {} find /root/app/remote/data/log/.log.0 -mtime +2 -type f |xargs -i gzip {} find /root/app/remote/data/log/.log.1 -mtime +2 -type f |xargs -i gzip {} find /root/app/remote/data/log/.log.2 -mtime +2 -type f |xargs -i gzip {} find /root/app/remote/data/log/.log.3 -mtime +2 -type f |xargs -i gzip {} find /root/app/remote/data/log/.log.4 -mtime +2 -type f |xargs -i gzip {} 改成循环语句,文件夹内的.gz文件不进行压缩,要用#!bin/bin/sh方式
时间: 2023-09-26 07:14:12 浏览: 56
#!/bin/bash
# Define the directories to be searched
directories=(/root/app/tcu/data/log /root/app/tcu/data/ccuLog /root/app/remote/data/log)
# Loop through the directories
for dir in "${directories[@]}"
do
# Loop through the log files in the directory
for log_file in "$dir"/*.log*
do
if [[ -f "$log_file" && ! "$log_file" =~ \.gz$ ]]; then
# Check if the file is older than 2 days and if it is not already compressed
if [[ "$(find "$log_file" -mtime +2 -type f | wc -l)" -gt 0 && ! -f "${log_file}.gz" ]]; then
# Compress the file
gzip "$log_file"
fi
fi
done
done
echo "Compression complete!"
相关问题
find /root/app/tcu/data/log/.log -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.0 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.1 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.2 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.3 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/.log.4 -mtime +2 -type f |xargs -i gzip {} 改成循环语句,文件夹内的.gz文件不进行压缩
可以使用下面的循环语句来替代上述命令:
```
#!/bin/bash
for logfile in /root/app/tcu/data/log/*.log*
do
if [[ -f "$logfile" && ! -L "$logfile" && "$(basename "$logfile")" != "*.gz" ]]
then
find "$logfile" -mtime +2 -type f ! -name "*.gz" -exec gzip {} \;
fi
done
```
这个循环语句会遍历 `/root/app/tcu/data/log/` 目录下的所有文件和子目录,并对每个 `.log` 文件进行压缩处理。同时,它会忽略已经压缩的文件和软链接,并且不对已经是 `.gz` 后缀的文件进行压缩操作。
find /root/app/tcu/data/log/*.log -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/*.log.0 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/*.log.1 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/*.log.2 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/*.log.3 -mtime +2 -type f |xargs -i gzip {} find /root/app/tcu/data/log/*.log.4 -mtime +2 -type f |xargs -i gzip {} 改成循环语句
可以使用下面的循环语句来替代上述命令:
```
#!/bin/bash
for logfile in /root/app/tcu/data/log/*.log*
do
if [[ -f "$logfile" && ! -L "$logfile" && "$(basename "$logfile")" != "*.gz" ]]
then
find "$logfile" -mtime +2 -type f -exec gzip {} \;
fi
done
```
这个循环语句会遍历 `/root/app/tcu/data/log/` 目录下的所有文件和子目录,并对每个 `.log` 文件进行压缩处理。同时,它会忽略已经压缩的文件和软链接。