Search code examples
regexfindls

find file with numeric values greater than a specified number


When I run the following command, I get a list of files

find . -type f -name '*_duplicate_[0-9]*.txt'

./prefix_duplicate_001.txt 

./prefix_duplicate_002.txt 

./prefix_duplicate_003.txt 

./prefix_duplicate_004.txt 

./prefix_duplicate_005.txt

Now I'm only interested in files which have the numbers greater than or equal to 003. How can I get this done?

Thank you in advance.


Solution

  • Using -regex option in find, you can tweak regex to get all files with 3 or higher value after _duplicate_ with leading zeroes:

    find . -regextype posix-extended -type f \
           -regex '.*_duplicate_0*([3-9]|[1-9][0-9])[0-9]*\.txt'
    

    On OSX use this find:

    find -E . -type f -regex '.*_duplicate_0*([3-9]|[1-9][0-9])[0-9]*\.txt'
    
    ./prefix_duplicate_003.txt
    ./prefix_duplicate_004.txt
    ./prefix_duplicate_005.txt