find . -mtime +7
这个命令是用来在当前目录及其子目录中查找文件,并按照最后修改时间进行筛选。-mtime +7
表示查找最后修改时间早于7天的文件。
具体解释如下:
.
表示当前目录,你也可以替换成其他目录路径。-mtime
是用来指定修改时间的选项。+7
表示查找最后修改时间早于7天的文件,如果要查找最后修改时间在7天以内的文件,可以使用-7
。
请注意,该命令会递归地查找当前目录及其子目录中的文件,并输出符合条件的文件列表。
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!"
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/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
后缀的文件不会被处理。
相关推荐
















