Skip to content
Advertisement

Linux count files with a specific string at a specific position in filename

I have a directory which contains data for several years and several months. Filenames have the format yy/mm/dd, f.e.

20150415, 20170831, 20121205

How can I find all data with month = 3? F.e.

20150302, 20160331, 20190315

Thanks for your help!

Advertisement

Answer

ls -ltra ????03??

A question mark is a wildcard which stands for one character, so as your format seems to be YYYYmmDD, the regular expression ????03?? should stand for all files having 03 as mm.

Edit
Apparently the files have format YYYYmmDDxxx, where xxx is the rest of the filename, having an unknown length. This would correspond with regular expression *, so instead of ????03?? you might use ????03??*.

As far as the find is concerned: the same regular expression holds here, but as you seem to be working inside a directory (no subdirectories, at first sight), you might consider the -maxdepth switch):

find . -name "????03??*" | wc -l             // including subdirectories
find . -maxdepth 1 -name "????03??*" | wc -l  // only current directory

I would highly advise you to check without wc -l first for checking the results. (Oh, I just see the switch -type f, that one might still be useful too 🙂 )

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement