Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is zsh globbing not working with find command?

Tags:

unix

zsh

glob

I have been using zsh globbing for commands such as:

 vim **/filename
 vim *.html.erb

and so on, but when I type in something like:

 find . -name *mobile*

I get the response:

 zsh: no matches found: *mobile*

Why?

like image 636
ovatsug25 Avatar asked Oct 05 '12 20:10

ovatsug25


People also ask

What is Globbing in zsh?

Globbing is a way of expanding wildcard characters in a. non-specific file name into a set of specific file names. Shells use globbing. all the time.

How do you search zsh history?

To use it, press CTRL + R in your terminal session. This will change your terminal session to search mode, and you can type for previous commands. As you type, the shell will search for a matching command in the history and suggest it. To search for the next matching suggestion, press CTRL + R.

What does find command do in Linux?

The Linux find command is one of the most important and frequently used command command-line utility in Unix-like operating systems. The find command is used to search and locate the list of files and directories based on conditions you specify for files that match the arguments.

What can you do with zsh?

What is Zsh? Zsh, also known as the Z shell, extends functionality of the Bourne Shell (sh), offering newer features and more support for plugins and themes. Starting with MacOS Catalina in 2019, Zsh became the default login and interactive shell in Mac machines.


2 Answers

find . -name *mobile* # does not work

vs

find . -name '*mobile*' # works

The difference is due to the steps that the shell takes when it parses a line. Normally, the shell expands any wildcards it finds before it runs the command. However, single quotes marks the argument as being a literal, which means that the shell does not preform wildcard expansion on that argument before running the command.

To demonstrate the difference, suppose you are in a directory with the following files:

$ tree
./
mobile.1
dir/
    mobile.2

zsh will expand the first form to the following before running it:

find . -name mobile.1

Which means that find will only look for files named literally mobile.1

The second form will be run as follows:

find . -name *mobile*

Which means that find will look for any filename containing the string "mobile".

The important thing to note here is that both zsh and find support the same wildcard syntax, but you want find to handle the wildcards in this case, not zsh.

like image 190
Swiss Avatar answered Oct 15 '22 10:10

Swiss


Turns out that all you have to do to solve the problem is add some quotes around the input:

find . -name '*mobile*'

I don't really have an answer as to why just yet...and the documentation doesn't have an something that sticks out to me, but let me know if you know the answer!

like image 28
ovatsug25 Avatar answered Oct 15 '22 10:10

ovatsug25