Find regex for timestamp in filename not working

7,087

The problem is the \d which is not understood by the sed regex so you should use a character class of digits: [0-9]. Also, sed braces for this use require preceding backslashes. Instead of:

.*\/\d{10}\..*

try

.*\/[0-9]\{10\}\..*

If you instead specify -regextype posix-egrep you would use:

.*\/[0-9]{10}\..*

There are many different versions of regular expressions. I would advise you to find the documentation on the command that you're using to find the correct regex syntax.

From the regxr.com website:

RegExr uses your browser's RegExp engine for matching, and its syntax highlighting and documentation reflect the JavaScript RegExp standard.

Which partly explains why the regex didn't work with find.

Share:
7,087

Related videos on Youtube

strangeman
Author by

strangeman

Updated on September 18, 2022

Comments

  • strangeman
    strangeman almost 2 years

    I want find mail files in user maildir, without mailfilter, dovecot-index etc.

    All mail files have unix timestamp in its filenames, I think it is the easiest way.

    I used this regex: .*\/\d{10}\..*. It working good in http://regexr.com/ tester, but not working in find command string.

    Full command string: find /var/lib/exim4/mail/markelov -regextype sed -regex '.*\/\d{10}\..*' -type f

    Bracket escaping (.*\/\d\{10\}\..*) not working also.

    String examples: Should find:

    /var/lib/exim4/mail/markelov/Maildir/.Sent/cur/1444787116.M8092P4423.mail,S=7264,W=7377:2,S
    /var/lib/exim4/mail/markelov/Maildir/.Sent/cur/1445824988.M900660P25060.mail,S=422,W=434:2,S
    /var/lib/exim4/mail/markelov/Maildir/.Trash/cur/1441681017.M637660P29838.mail,S=38938:2,Sa
    /var/lib/exim4/mail/markelov/Maildir/.Trash/cur/1441934431.M919612P29838.mail,S=1447:2,Sa
    

    Shouldn't find:

    /var/lib/exim4/mail/markelov/Maildir/.INBOX.zabbix.Alerts/maildirfolder
    /var/lib/exim4/mail/markelov/Maildir/.INBOX.zabbix.Alerts/dovecot-uidlist
    /var/lib/exim4/mail/markelov/Maildir/subscriptions
    /var/lib/exim4/mail/markelov/Maildir/.INBOX.megaplan/dovecot.index
    

    find --version

    find (GNU findutils) 4.4.2
    Copyright (C) 2007 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.
    
    Written by Eric B. Decker, James Youngman, and Kevin Dalley.
    Built using GNU gnulib version e5573b1bad88bfabcda181b9e0125fb0c52b7d3b
    Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION FTS() CBO(level=0)