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文件不进行压缩
时间: 2023-07-08 17:29:30 浏览: 218
好的,我可以帮你改成循环语句。可以使用以下脚本:
```
#!/bin/bash
DIRECTORIES=("/root/app/tcu/data/log" "/root/app/tcu/data/ccuLog" "/root/app/remote/data/log")
for DIR in "${DIRECTORIES[@]}"
do
find "$DIR" -type f -name '*.log*' ! -name '*.gz' ! -type l -mtime +2 | while read FILE
do
gzip "$FILE"
done
done
```
这个脚本会循环处理 `DIRECTORIES` 数组中指定的目录,对每个目录下的满足条件的文件进行处理。`find` 命令会查找文件名符合 `*.log*` 并且不以 `.gz` 结尾的普通文件(排除软链接),且最后修改时间在两天以前的文件。这些文件会被传递给 `while` 循环逐个处理,即使用 `gzip` 压缩这些文件。已经是 `.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` 文件进行压缩处理。同时,它会忽略已经压缩的文件和软链接。
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方式
#!/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!"
阅读全文