如何在文件查找中使用正则表达式 [英] How to use regex in file find

查看:85
本文介绍了如何在文件查找中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图查找所有日期和 3 天或更长时间前的文件.

I was trying to find all files dated and all files 3 days or more ago.

find /home/test -name 'test.log.d{4}-d{2}-d{2}.zip' -mtime 3

它没有列出任何东西.有什么问题吗?

It is not listing anything. What is wrong with it?

推荐答案

find /home/test -regextype posix-extended -regex '^.*test.log.[0-9]{4}-[0-9]{2}-[0-9]{2}.zip' -mtime +3

  1. -name 使用 globular 表达式,又名通配符.你想要的是-regex
  2. 要按照您的意图使用间隔,您需要告诉 find 使用 Extended正则表达式通过-regextype posix-extended 标志
  3. 你需要避开句号因为在正则表达式中,一个时期有任何单个的特殊含义字符.你想要的是一个用 .
  4. 表示的文字句点
  5. 只匹配那些大于超过 3 天,您需要在您的号码前加上 + 作为前缀在 -mtime +3 中.
  1. -name uses globular expressions, aka wildcards. What you want is -regex
  2. To use intervals as you intend, you need to tell find to use Extended Regular Expressions via the -regextype posix-extended flag
  3. You need to escape out the periods because in regex a period has the special meaning of any single character. What you want is a literal period denoted by .
  4. To match only those files that are greater than 3 days old, you need to prefix your number with a + as in -mtime +3.

概念证明

$ find . -regextype posix-extended -regex '^.*test.log.[0-9]{4}-[0-9]{2}-[0-9]{2}.zip'
./test.log.1234-12-12.zip

这篇关于如何在文件查找中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆