Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. RegEx- Find files in Linux which match the expression

RegEx- Find files in Linux which match the expression

Scheduled Pinned Locked Moved Regular Expressions
regexcomlinuxtutorial
4 Posts 3 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JDMils
    wrote on last edited by
    #1

    This is for my VMware vCenter servers where I am trying to clean out extra log files which are no longer required. The type of files for this example are:

    Quote:

    /storage/log/vmware/eam/web/localhost_access_log..2020-12-06.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-31.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-07-21.txt /storage/log/vmware/eam/web/localhost_access_log..2020-08-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-30.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-27.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-14.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-28.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-01.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-29.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-19.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-05.txt

    The expression which works for me in my Linux file system is this one:

    find /storage/log/vmware/ -mtime +10 -type f -name "localhost_access_log..2020-[0-9][0-9]-[0-9][0-9].txt"

    It uses the Linux "find" command to find the files, the files have to be "mtime=10" 10 days or older. I would like to shorten the regex to simplify it, and using RegExr: Learn, Build, & Test RegEx[^] as my tester, I found that the following regex works:

    localhost_access_log\.\.2020-[0-9]{2}-[0-9]{2}\.txt

    However when I try it on my Linux filesystem, it fails to produce results. I get nothing returned.

    | +-- JDMils | +-- VB6 +-- VB Dot Net |

    L K 2 Replies Last reply
    0
    • J JDMils

      This is for my VMware vCenter servers where I am trying to clean out extra log files which are no longer required. The type of files for this example are:

      Quote:

      /storage/log/vmware/eam/web/localhost_access_log..2020-12-06.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-31.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-07-21.txt /storage/log/vmware/eam/web/localhost_access_log..2020-08-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-30.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-27.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-14.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-28.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-01.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-29.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-19.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-05.txt

      The expression which works for me in my Linux file system is this one:

      find /storage/log/vmware/ -mtime +10 -type f -name "localhost_access_log..2020-[0-9][0-9]-[0-9][0-9].txt"

      It uses the Linux "find" command to find the files, the files have to be "mtime=10" 10 days or older. I would like to shorten the regex to simplify it, and using RegExr: Learn, Build, & Test RegEx[^] as my tester, I found that the following regex works:

      localhost_access_log\.\.2020-[0-9]{2}-[0-9]{2}\.txt

      However when I try it on my Linux filesystem, it fails to produce results. I get nothing returned.

      | +-- JDMils | +-- VB6 +-- VB Dot Net |

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Go back to the version that does work.

      1 Reply Last reply
      0
      • J JDMils

        This is for my VMware vCenter servers where I am trying to clean out extra log files which are no longer required. The type of files for this example are:

        Quote:

        /storage/log/vmware/eam/web/localhost_access_log..2020-12-06.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-31.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-13.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-07-21.txt /storage/log/vmware/eam/web/localhost_access_log..2020-08-03.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-30.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-08.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-27.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-14.txt /storage/log/vmware/eam/web/localhost_access_log..2020-09-28.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-01.txt /storage/log/vmware/eam/web/localhost_access_log..2020-11-29.txt /storage/log/vmware/eam/web/localhost_access_log..2020-10-19.txt /storage/log/vmware/eam/web/localhost_access_log..2020-12-05.txt

        The expression which works for me in my Linux file system is this one:

        find /storage/log/vmware/ -mtime +10 -type f -name "localhost_access_log..2020-[0-9][0-9]-[0-9][0-9].txt"

        It uses the Linux "find" command to find the files, the files have to be "mtime=10" 10 days or older. I would like to shorten the regex to simplify it, and using RegExr: Learn, Build, & Test RegEx[^] as my tester, I found that the following regex works:

        localhost_access_log\.\.2020-[0-9]{2}-[0-9]{2}\.txt

        However when I try it on my Linux filesystem, it fails to produce results. I get nothing returned.

        | +-- JDMils | +-- VB6 +-- VB Dot Net |

        K Offline
        K Offline
        k5054
        wrote on last edited by
        #3

        Unless told otherwise, find uses file globs, I think. But you can change the regex engine using -regextype. e.g. -regextype posix-extended. find will tell you what regex engines it knows about if you say -regex help. Possibly one of the engines knows how to parse your regex expression to your liking.

        Keep Calm and Carry On

        J 1 Reply Last reply
        0
        • K k5054

          Unless told otherwise, find uses file globs, I think. But you can change the regex engine using -regextype. e.g. -regextype posix-extended. find will tell you what regex engines it knows about if you say -regex help. Possibly one of the engines knows how to parse your regex expression to your liking.

          Keep Calm and Carry On

          J Offline
          J Offline
          JDMils
          wrote on last edited by
          #4

          Thanks K5054, that was the clincher. To find the files I need, I had to perform the following: * State the Regex Engine as 'posix-extended' * Put the expression '.*' at the start of the filename as the files are treated as fully qualified filenames (file path & filename). Thus, I can now use the following:

          find /storage/log/vmware/ -type f -regextype posix-extended -regex '.*vpxd-svcs-access-.2022-[0-9]{2}-[0-9]{2}.log.gz'

          And....

          find /storage/log/vmware/ -type f -regextype posix-extended -regex '.*sps.log.[0-9]{2}.gz'

          | +-- JDMils | +-- VB6 +-- VB Dot Net |

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups