1

I am trying to find files that have only numbers in their filenames. For example, if I have the files 123.txt, 45.doc, alpha_123, beta_123.txt, 45 and 123, I only want to match files 45 and 123. I have tried various options with find and grep or combinations of them, but nothing worked as I wanted or gave me syntax errors.

Also is there any chance that I can only match for empty files? I tried using the option "empty" but also was unsuccessful, although this was probably due to using the wrong syntax.

This find . -name "[0-9]*" kind of worked, as in it found files with numbers in their filenames, for example file 123 and file 45, but it also found files with an extension, for example 123.txt and 45.doc, and unfortunately this is not what I want.

From the suggestions below (as my original question was heavily edited after answers were posted), what worked exactly as I wanted was $ find . -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'.

I apologise for the initial misleading/ambiguous post, I hope the edited version will prove helpful for other users also confused about how to use find with regex.

kouki
  • 35
  • 2
  • 9
  • try `find . -regex '.*/[0-9]+'` – Johnny Everson Jan 27 '16 at 17:45
  • Possible duplicate of [How to use regex with find command?](http://stackoverflow.com/questions/6844785/how-to-use-regex-with-find-command) – Johnny Everson Jan 27 '16 at 17:47
  • 2
    How about just `find . -name '[[:digit:]]*'` – glenn jackman Jan 27 '16 at 18:01
  • @glenn jackman: unfortunately this finds all files that have numbers in their filename - I'm trying to find files that have only numbers in their filename. – kouki Jan 27 '16 at 18:07
  • @Jhonny Everson: unfortunately this returns directories and not files. Also, although it is somewhat indeed a duplicate, I had already read that thread and the answers didn't help - I do apologise for the duplicate. – kouki Jan 27 '16 at 18:07
  • Oops sorry I forgot to say that I will edit my original post and include a line that indeed worked for what I wanted to do, it would be great if you commented on it. – kouki Jan 27 '16 at 18:08
  • The title and your attempts indicate file names *starting* with a digit, while the examples consist completely of digits, and you confirm in a comment that this is actually what you want. Please [edit] your question so that it is internally consistent, and adequately describes your requirements. This is very basic, but you are wasting your own time as well as ours by not being completely clear right from the start on what you need. – tripleee Jan 27 '16 at 18:10
  • I want to match only files that their filename has nothing else but a number. For example, if I have files like 450, alpha_134.doc and 123.txt, I ONLY want to match file 450. I sincerely apologise because I have posted an ambiguous question and comments, sometimes you think you are getting your point across but in reality you're vague and ambiguous. I'll try to edit efficiently. – kouki Jan 27 '16 at 18:14
  • 1
    @kouki, not true. `-name '*[[:digit:]]*'` would find files containing a digit. `-name '[[:digit:]]*'` finds files starting with a digit. The argument to -name is a **glob** pattern, not a regex. – glenn jackman Jan 27 '16 at 18:14
  • `find . -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'` – dawg Jan 27 '16 at 18:26

3 Answers3

4

Either this:

$ find -E .  -type f -regex '.*/[[:digit:]]+$'

Or this:

$ find .  -type f -name '[[:digit:]]*' | grep -E '.*/[0-9]+$'

work to find files with names composed entirely of digits (i.e., no extension)

dawg
  • 98,345
  • 23
  • 131
  • 206
  • Thank you dawg :) The first one gives me a `find: unknown predicate -E'` error. However the second one works exactly as intended. Thanks again :) – kouki Jan 28 '16 at 11:00
  • The `-E` is for extended regex on BSD `find` It may be a different switch on GNU version of find. – dawg Jan 30 '16 at 17:42
0

A most compatible (BSD & Linux) invocation of find without an additional (e)grep is this:

find . -regex "\./[0-9][0-9]*"

This will find in current directory all files with names containing only digits.

And this will find the files also recursively:

find . -regex ".*/[0-9][0-9]*"
reichhart
  • 813
  • 7
  • 13
-1

On Debian 11 with find 4.8.0

I used

find . -type f -regextype egrep -iregex './[0-9]{1,2}_.*'

to find files in and bellow the current directory matching

./0_abc upto ./99_abc

but excluding

./000_abc

Note -iregex is case insensitive as apposed to -regex

  • Well... how does case insensitive search work if it is just for NUMBERS? `regex`is sufficient. No `iregex` required. Additionally your answer is wrong because the question is for names with numbery ONLY. – reichhart Aug 04 '22 at 20:31
  • Sorry, just pointing out that I had used a case insensitive search which as you have surmise plays no role in a numeric only search. – Eric Maasdorp Aug 06 '22 at 06:00