xargs排除
时间: 2023-08-01 21:14:12 浏览: 40
xargs命令可以使用选项 "-I" 或 "-i" 来指定一个占位符,用于替换从stdin读取的每个输入行中的参数。如果要从stdin读取多个参数,并且想要排除其中的某些参数,可以使用grep命令来过滤输入,然后使用xargs命令来处理剩余的参数。
例如,假设要从一个文件列表中排除所有以".txt"结尾的文件,并将剩余的文件传递给另一个命令进行处理,可以使用以下命令:
```
find . -type f | grep -v '\.txt$' | xargs command
```
上面的命令首先使用find命令查找当前目录下的所有文件,然后使用grep命令过滤掉所有以".txt"结尾的文件。最后,使用xargs命令将剩余的文件传递给"command"命令进行处理。
相关问题
linux中删除文件夹中文件,如何排除其中某个文件
�� Linux 中,可以使用 `rm` 命令来删除文件。如果要删除文件夹中的所有文件,可以使用 `rm -r` 命令。
如果要排除某个文件,可以使用 `find` 命令来查找文件夹中的文件,然后使用 `xargs` 命令将查找到的文件传递给 `rm` 命令进行删除。
例如,假设要删除文件夹 `/path/to/folder` 中的所有文件,但是要排除文件 `/path/to/folder/exclude.txt`,可以使用以下命令:
```
find /path/to/folder -type f ! -name "exclude.txt" -print0 | xargs -0 rm
```
这条命令会查找文件夹 `/path/to/folder` 中的所有文件(`-type f`),排除文件名为 `exclude.txt` 的文件(`! -name "exclude.txt"`),然后将查找到的文件名以 null 结尾的形式输出(`-print0`),最后使用 `xargs` 命令将文件名传递给 `rm` 命令进行删除。
注意:使用 `rm -r` 命令删除文件夹时,如果文件夹中还包含其他文件夹,则这些文件夹及其中的文件也会被删除。因此,在使用 `rm -r` 命令删除文件夹时,应谨慎操作,以免误删重要文件。
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` 后缀的文件不会被处理。