How to find all files in a directory with grep and regex?

I have a directory (Linux / Unix) on an Apache server with a lot of subdirectories containing many files like this:

- Dir  
  - 2010_01 /
    - 142_78596_101_322.pdf
    - 12_10.pdf
    - ...
  - 2010_02 /   
    - ...

How can I find all files with file names that look like this *_*_*_*.pdf:? where * is always a number!

I am trying to solve this as follows:

ls -1Rl 2010-01 | grep -i '\(\d)+[_](\d)+[_](\d)+[_](\d)+[.](pdf)$' | wc -l

But regex \(\d)+[_](\d)+[_](\d)+[_](\d)+[.](pdf)$doesn't work with grep.

Change 1 . Try ls -l 2010-03 | grep -E '(\d+_){3}\d+\.pdf' | wc -l, for example, just returning null. So it works great

+5
source share
3 answers

gbchaosmaster , :

:

find . | grep -P "(\d+_){3}\d+\.pdf" | wc -l

:

find 20*/ | grep -P "(\d+_){3}\d+\.pdf" | wc -l
0

find.

, __*_*.pdf where * is always a digit:

find 2010_10/ -regex '__\d+_\d+\.pdf'

, , , , .

(\d+_){3}\d+\.pdf

, / ?

[\d_]+\.pdf
+3

-, egrep vs grep grep -E .

, :

$ cat test2.txt
- Dir  
  - 2010_01/
    - 142_78596_101_322.pdf
    - 12_10.pdf
    - ...
  - 2010_02/   
    - ...

egrep :

cat test2.txt | egrep '((?:\d+_){3}(?:\d+)\.pdf$)'
- 142_78596_101_322.pdf

, .

, grep :

$ cat test2.txt | grep '((?:\d+_){3}(?:\d+)\.pdf$)'
... no return

DOES , ( , egrep):

$ cat test2.txt | grep -E '((?:\d+_){3}(?:\d+)\.pdf$)'
- 142_78596_101_322.pdf 
+1
source

All Articles