find . -name 'my*'

The quotes avoid the shell expansion - without them the shell would replace my* with the list of files whose names begin with my in the current directory.

What thus the above statement mean? Isn't it doing the same thing I mean looking for file names beginning with my?

Recommended Answers

All 2 Replies

find . -name 'my*'

The quotes avoid the shell expansion - without them the shell would replace my* with the list of files whose names begin with my in the current directory.

What thus the above statement mean? Isn't it doing the same thing I mean looking for file names beginning with my?

Do an exercise:

touch my0
mkdir foo
touch foo/my1
find -name 'my*'
find -name my*

and compare results.

find . -name 'my*'

The quotes avoid the shell expansion - without them the shell would replace my* with the list of files whose names begin with my in the current directory.

What thus the above statement mean? Isn't it doing the same thing I mean looking for file names beginning with my?

No. While it can be the same, it is not always or necessarily the same. Quoting or escaping the asterisk lets find use the asterisk as a wildcard character.

It is the same when your current directory contains no files matching that glob pattern. In this case, the shell passes your my* to find as-is.

It is not the same if your current directory contains one or more files starting with my. For example, if you have just one file named myfile, find will recursively search your directory tree for files named myfile. If you have two or more files that begin with my, you will get a syntax error.

This is why quoting (using args like "my*") or escaping (using args like my\*) arguments that contain shell meta-characters is important.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.