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 {} 改成循环语句
时间: 2023-09-16 22:12:50 浏览: 43
可以使用下面的循环语句来替代上述命令:
```
#!/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 {} 改成循环语句,文件夹内的.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` 后缀的文件进行压缩操作。
if ls /root/app/backup/*.db 1> /dev/null 2>&1; then cp /root/app/backup/*.db /root/app/tcu/data/db/ fi
这段代码是一个简单的条件语句,用于检查指定目录下是否存在以.db结尾的文件。如果存在,则将这些文件复制到另一个目录中。
以下是代码的解释:
```shell
if ls /root/app/backup/*.db 1> /dev/null 2>&1; then
cp /root/app/backup/*.db /root/app/tcu/data/db/
fi
```
- `if ls /root/app/backup/*.db 1> /dev/null 2>&1`:使用`ls`命令检查`/root/app/backup/`目录下是否存在以.db结尾的文件。`1> /dev/null`表示将标准输出重定向到空设备,`2>&1`表示将标准错误输出重定向到标准输出。
- `then`:如果上述命令执行成功(即目录下存在以.db结尾的文件),则执行下面的命令。
- `cp /root/app/backup/*.db /root/app/tcu/data/db/`:将`/root/app/backup/`目录下所有以.db结尾的文件复制到`/root/app/tcu/data/db/`目录中。
这段代码的作用是将指定目录下的以.db结尾的文件复制到另一个目录中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)