How come "staff*22" does not show anything while "staff.*22" lists lines that contain staff followed by 22 although any of the lines does not contain "." in them?

$ ls -l | grep "staff*22"
$ ls -l | grep "staff.*22"
drwxrwxr-x  2 kim  staff   68 Jan 12 22:23 hihi
drwxrwxrwx  4 kim  staff  136 Jan 12 22:29 temp2
drwxrwxrwx  3 kim  staff  102 Jan 12 22:41 tes2
  • 1
    man grep and see the details about regular expressions and . = [^\n] = any char except newline.
    – user216043
    Jan 16, 2017 at 23:37
  • 2
    You may be confusing regular expressions with shell globs - see for example Globs compared to regular expressions Jan 16, 2017 at 23:45
  • 2
    What is the rule that you're trying to do here ? I mean, are you trying to find all files that belong to staff group and end with number 2 in filename ? Jan 16, 2017 at 23:46
  • I am trying to find lines that contain staff and followed by 22 in terms of regular expressions, not globs.
    – Mint.K
    Jan 16, 2017 at 23:51

1 Answer 1


What do given grep commands do

From your output it is apparent that you want to find all files that belong to "staff" group and created at hour 22 ( or 10 pm in 12 hour format ). We'll address that below, but lets first figure out what your grep commands do and why they're not the right tool for what you want.

First of all, the two grep commands that you do are slightly different in the way they are interpreted, and your approach is not proper - you shouldn't try to parse output of ls.

  • grep "staff*22" reads as "match all lines that start with 'staf' and may or may not have in them extra 'f' , immediately followed by '22'."

    $ echo "staff whatever 22" | grep 'staff*22'               
    $ echo "staff22" | grep 'staf*22'                          
    $ echo "staf22" | grep 'staff*22'                         
    $ echo "staf22" | grep 'staff*22'
  • grep "staff.*22" reads as "match all lines that have 'staff' in them, zero or more characters in between ( any type of character ),and number 22".

    $  echo "staff whatever 22" | grep 'staff.*22'                                                                           
    staff whatever 22

Better approach

ls command in general should only be used for interactive listing, i.e., when you only want to have a quick peek at what's inside a folder. When it is necessary to find specific files, or some how work with those files via script, find command is the way to go. It has -group variable, which will help you isolate all the files that belong to staff group, and -nevermt for finding files with modification time newer than some specific date. For instance, what you want is probably this:

find . -group staff -newermt "2017-01-12 21:59:59" 

See also this: https://superuser.com/questions/580273/ubuntu-linux-find-files-between-specific-times

  • @Mint.K You're welcome :) I'll add a few other things, but hopefully, I've answered the core of your question Jan 17, 2017 at 0:06
  • Please do so. I'd like to learn more.
    – Mint.K
    Jan 17, 2017 at 0:09
  • @Mint.K Edited, please review :) Jan 17, 2017 at 0:11
  • Can you give me some references to ".*"? does "." in ".*" mean anything in between? I am trying to figure out if there is something like a table that explains such symbols mentioned above.
    – Mint.K
    Jan 18, 2017 at 4:08
  • 1
    @Mint.K there's plenty of references online, simplest would be just go to en.wikipedia.org/wiki/Regular_expression. The . means any character. * means previous element repeated zero or more times. So in ab.*c it means match any string which has ab characters , which may have something after them, followed by c. Jan 19, 2017 at 14:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .