简体   繁体   English

在Shell中使用正则表达式查找命令以查找具有两个扩展名的文件

[英]Find command in shell with regular expression to find files with two extensions

I am trying to list generated log and zip files from my application server. 我正在尝试列出我的应用程序服务器中生成的日志和zip文件。

  • Files which are .log or .zip .log或.zip文件
  • These files include digits in their name. 这些文件的名称中包含数字。 ie Files with any number of digits in their name 即文件名称中包含任意数字
  • Files should be older than +5 days. 文件应早于+5天。

I used below expression. 我用下面的表达。 but looks something wrong. 但看起来有问题。 Could you please assist with regular expression? 您能协助您进行正则表达吗?

ROOT_DIR=applications/jboss-as/servers/
find $ROOT_DIR -name '*[0-9]*[zip|log]' -mtime +5

Finally I wish to delete these files using command 最后我希望使用命令删除这些文件

find $ROOT_DIR -name '*[0-9]*[zip|log]' -mtime +5 -exec rm {} \;

The first command will find them and display. 第一个命令将找到它们并显示。

find $ROOT_DIR ! -readable -prune -mtime +5 -type f |  egrep -e "^.*\.(log|zip)$"

The second one will remove them all 第二个将全部删除

find $ROOT_DIR ! -readable -prune -mtime +5 -type f |  egrep -e "^.*\.(log|zip)$" | xargs -L 1 rm

You could do it this way (with most versions of find ): 您可以通过以下方式进行操作(使用find大多数版本):

find "$ROOT_DIR" '(' -name '*[0-9]*.log' -o -name '*[0-9]*.zip' ')' -mtime +5 -exec rm {} +

The + is from POSIX 2008 and means "run the exec'd command with as many file names as convenient" whereas the older alternative ';' +来自POSIX 2008,表示“以尽可能方便的文件名运行exec'd命令”,而较早的替代项';' (or \\; ) means "run the exec'd command once per file name". (或\\; )表示“每个文件名运行一次exec'd命令”。

If you have GNU find , you can use various dialects of regular expression: 如果您拥有GNU find ,则可以使用各种正则表达式的方言:

find "$ROOT_DIR" -regex '.*\.\(zip\|bz2\)' -mtime +5 -delete

This uses the default regex mode; 这使用默认的正则表达式模式; you can use some alternatives to avoid using so many backslashes. 您可以使用一些替代方法来避免使用太多的反斜杠。 The -delete option uses the unlink() system call rather than invoking an external command; -delete选项使用unlink()系统调用而不是调用外部命令。 it is more efficient, therefore. 因此,它效率更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM