2

This question already has an answer here:

I've found using the 'find' command to search for individual files that some inconsistencies occur. Eg, I wanted to search for a particular file with the word paper in its name. If I type sudo find /mnt -iname *paper* the output is:-

/mnt/data/MyPhotos/Wallpapers

which is fair enough, except it wasn't what I was looking for. But if I type sudo find /mnt -iname "*paper*" the output becomes:-

/mnt/data/MyMusic/FLV-Music/Joe_Jackson_Sunday_Papers_small.mp4
/mnt/data/MyMusic/FLV-Music/Status_Quo_Paper_Plane_medium.mp4
/mnt/data/MyPhotos/Wallpapers

Now I see the file I was searching for (the 2nd one). So why in the first case is only the last file shown as output, whereas in the second case all files including the string paper in their names are output. Why should the quotes make a difference here.

marked as duplicate by muru, Jeff Schaller Jun 15 at 1:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

Without the quotes, the shell is getting to parse the * before find is, and it's expanding that glob (if it's able to). With the quotes, the globbing does not occur.

To demonstrate:

$ tree
.
├── Wallpapers
└── stuff
    ├── Newspapers
    ├── Wallflowers
    └── Wallpapers

2 directories, 3 files
$ find stuff -iname *paper*
stuff/Wallpapers
$ find stuff -iname '*paper*'
stuff/Wallpapers
stuff/Newspapers
  • 1
    Specifically: there's a file named "Wallpapers" in the current working directory, so the shell expands sudo find /mnt -iname *paper* to sudo find /mnt -iname Wallpaper. If there was no matching file in the current directory, it would've worked. If there were multiple matches, it probably would cause a find error. – Gordon Davisson Jun 14 at 22:36
  • 'Wallpapers' is found in a subfolder, not current working one. It is a folder by the way, not a file. But find fails to list files elsewhere with the same string pattern within sub-folders when not using quotes. That suggests to me that I need quotes for the 'find' command to list files as well. – Paul Benson Jun 15 at 1:51
  • @PaulBenson I'm not talking about the "Wallpapers" folder the find command found, I'm saying there's another "Wallpapers" file/folder/whatever in the directory you're in when you run the find command. That is, if you ran ls Wallpapers before running find, it would list something (not give a "No such file or directory" error). Try running cd /var/empty; sudo find /mnt -iname *paper* -- since there isn't anything named "Wallpapers" in /var/empty, this should leave the wildcard unexpanded and hence find all matches. – Gordon Davisson Jun 15 at 7:47
  • @Gordon Davisson The only 'Wallpapers' result is that folder that was listed in the output for that path recursively (/mnt). There is no other file/folder with that name. I don't know why you seem to think there is. ls Wallpapers produces nothing as 'Wallpapers' also happens to be a folder in my Home folder, but it is empty, and I'm searching on a different path anyway. – Paul Benson Jun 15 at 16:45
  • 1
    @PaulBenson The "Wallpapers" folder in your home folder is the other one I'm talking about. I know it's not where you told find to search, but that doesn't matter; when the shell sees *paper* in the argument list, it looks for matches in your current working directory (in this case, your home folder), and replaces it with the match(es). – Gordon Davisson Jun 16 at 7:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.