0

I have a folder who have files names according to date for one month i.e. AP_20140101.gz, AP_20140102.gz, ... for each day. Here 2014 is year, 01 is month and 01 is date (AP_YYYYMMDD.gz is general format )

For selecting files for date 10 to 19 I can do this :

ls | grep *2014011*

But I am unable to grep from 15 to 31. Any suggestions?

migrated from serverfault.com Feb 20 '14 at 15:00

This question came from our site for system and network administrators.

  • By the way, you never need to grep '*foo*' that is exactly the same as grep foo. – terdon Feb 20 '14 at 15:14
5

You shouldn't need grep to do the work, your shell should be able to do it. If you're using bash then you could try using ranges

ls AP_201401{15..31}.gz

seems to work.

If any of the files don't exist then you will get an error message so you may need to pipe stderr to a file.

  • Excellent !! Simple and Effective. – user2922822 Feb 18 '14 at 7:10
1

You're confusing shell wildcards (like the pattern you typed) with regular expressions (which grep expects). A regular expression to do what you want with grep would be

ls | grep '201401(1[5-9]|2|3)'

The shell wildcard expression in bash would be

ls *201401{1{5..9},2,3}*

Please read about shell wildcards in the EXPANSION section in man bash, and regular expressions in man grep or man 7 regex.

0

grep patterns are regular expressions, and '*2014011*' isn't doing what you think it is. Take some time to learn the basics if you don't understand why. Hint: '*' doesn't just match any character.

In this case simple wildcards will work perfectly well without grep, so you could simply do this:

ls *2014011*

For more info, you can read about globbing. Link is for bash, which I assume you are using.

0

@Iian gave the perfect answer. BTW you can also use 'find' command as well.

find . -type f -name 'AP_201401[15-31].gz'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy