5

To use locate command with regex , do we need to enclose the pattern in quotes along with passing --regex option ? If yes, then what do the following mean -

a) locate --regex file* ? Here regex will happen or shell globbing ?

b) locate 'file*' ? Will locate do regex search even though we did not passed --regex ?

c) locate file* // I understand shell globbing will happen

d) locate --regex 'file*' // I understand regex search will happen in Database file

terdon
  • 242,166
Number945
  • 163

1 Answers1

5

You type the command at the shell prompt. The shell processes what you typed, which includes globbing, substituting variables, substituting $() and so on. After processing what you typed, the shell executes the command.

Quotes are needed if a string contains characters that are special to the shell, such as spaces or asterisks, but you don't want the shell to process them. You will get away without quoting an asterisk if there are no matching files in the current directory, but it's good practice to quote it anyway.

It is important to understand that the --regex option has no effect on the shell's actions. First, the shell processes the command that you type. locate gets the result of that processing.

a) If there are files in the current directory that match file*, the shell will replace file* with the list of those files before calling locate. If there is no match, the shell won't touch file*, and locate looks for files that are named file, filee, fileee and so on. In short, the shell attempts globbing, then locate performs a regular expression search if the shell's globbing results in correct syntax.

b) The quotes tell the shell to leave the asterisk alone. locate will look for files that start with file. No regex search.

c) The shell attempts globbing as in a). If there is no match, locate will look for files that start with file. No regex search.

d) The shell leaves the expression alone. locate will perform a regex search and look for files that are named file, filee, fileee and so on.

berndbausch
  • 3,557
  • 2
    The behavior when there is no glob match depends on the shell (and shell options) - for example, bash by default passes the unexpanded glob pattern as you describe (with failglob and nullglob as options), whereas zsh would only pass the unmodified pattern to locate with option nonomatch. See for example Why zsh tries to expand * and bash does not? – steeldriver Jan 28 '21 at 15:47
  • @berndbausch for b) is not that also a type of regex search ? Suppose I pass locate '*file*' , then locate will search for all file paths in Db having keyword file in it. So, is not it again a regex search ? If yes, then is not --regex redundant ? – Number945 Jan 28 '21 at 16:19
  • @steeldriver The link explains wildcard meanings when used in globbing vs regex context. But my question is different. locate '*file*' , here locate is using to search through database and not shell expanding the wildcard. So, what is this process called ? Still we call it globbing or regex ? – Number945 Jan 28 '21 at 17:31
  • 1
    @Number945 the exact terminology was discussed recently in comments here. I don't feel qualified to offer an opinion. – steeldriver Jan 28 '21 at 17:56
  • @Number945 locate '*file*' is not a regex search. It finds files that contain the string file (actually, the wildcards are redundant here). locate --regex '*file*' is regular expression search. It finds filenames that start with an asterisk, then fil, then any number of e's. – berndbausch Jan 28 '21 at 22:35
  • 1
    @Number945 A clearer example, perhaps: locate 's*p' finds sleep, smtp among others. The asterisk stands for "any string". On the other hand, in locate --regex 's*p', the asterisk means "a sequence of zero or more of the previous character", i.e. the empty string, a single s, or any number of s's. – berndbausch Jan 29 '21 at 00:00
  • @steeldriver thanks for pointing out that the shell's globbing behaviour is highly dependent on parameters and shell type. It's easy (for me) to overlook that. – berndbausch Jan 29 '21 at 00:03