xargs -i cp -r {} /tmp,-i和-r参数的意义是什么,可以不用这些参数吗?
时间: 2024-04-19 12:24:24 浏览: 17
xargs 是一个用于构建和执行命令行的实用工具。在你提供的命令中,xargs 的参数 -i 表示将每个输入行的替换字符串 {} 替换为命令的参数。-r 参数表示当没有输入时不运行命令。
cp 命令用于复制文件和目录。-r 参数表示递归地复制目录及其内容。而 {} 表示 xargs 从标准输入接收的每个行的占位符,即文件或目录的名称。
如果不使用 -i 参数,xargs 默认会将输入行追加到命令的尾部作为参数,而不是使用 {} 进行替换。所以,在这个特定的命令中,-i 参数是必需的,以便正确地将每个输入行替换为 cp 命令的参数。
如果不使用 -r 参数,并且没有输入行时,cp 命令会试图将当前目录(.)复制到 /tmp 目录中,这可能会导致意外的结果。因此,为了避免这种情况,建议保留 -r 参数以及 -i 参数。
相关问题
find /opt/ -name "1.txt"|xargs -i cp {} /tmp -i是什么意思
在这个命令中,`-i` 是 `xargs` 命令的一个选项,用于替代字符串中的字符串(通常用 `{}` 表示)。它告诉 `xargs` 命令将输入的每个字符串中的 `{}` 替换为相应的参数,并执行后面的命令。在你给出的例子中,`{}` 会被 `find` 命令找到的每个文件路径所替代,然后 `cp` 命令会将这些文件复制到 `/tmp` 目录下。
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!"
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)