Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

regex

linux

grep

I have a Directory(Linux/Unix) on a Apache Server with a lot of subdirectory containing lot of files like this:

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

How can i find all files with filesnames looking like: *_*_*_*.pdf ? where * is always a digit!!

I try to solve it like this:

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

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

Edit 1: Trying ls -l 2010-03 | grep -E '(\d+_){3}\d+\.pdf' | wc -l for example just return null. So it's dont work perfectly

like image 383
3logy Avatar asked Oct 07 '22 13:10

3logy


1 Answers

Try using find.

The command that satisfies your specification __*_*.pdf where * is always a digit:

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

You seem to be wanting a sequence of 4 numbers separated by underscores, however, based on the regex that you tried.

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

Or do you want to match all names containing solely numbers/underscores?

[\d_]+\.pdf
like image 132
vinnydiehl Avatar answered Oct 10 '22 01:10

vinnydiehl