1

I'm trying to search through the files and directories of a specific folder. For example, I was looking in /usr/bin for my python binaries. To do so, I used ls | grep python. When I did this, I was able to find, for example, python3, python3-config, etc.

While this works fine, I know that there are easier ways to do that: I shouldn't have to pipe to grep. But when I tried find . -name python, as per my understanding of the find man page, it yielded no results.

I know that grep searches through a file. What is the correct way to search through a given directory?

Yehuda
  • 259

1 Answers1

2

you can do several things, using "globbing" In a nutshell: the shell tries to match

? to any character, (unless it is "protected" by single or double quotes
* to any string of characters (even empty ones), unless protected by single or double quotes 
[abc] can match either 'a', 'b' or 'c'
[^def] is any single character different than 'd', 'e' or 'f'

So to match under /usr/bin anything with python in it:

ls -d /usr/bin/*python*  # just looks into that directory

or with find, you can also use globbing. However you need to surround it in quotes so that the shell does not expand them, but instead give them to the find command with the '*' intact:

find /usr/bin -name '*python*'  # could descend into subfolders if present
  • 1
    Nice answer. But you seem to have used "as is" in a strange way. No I think it is the "and" I had to re-parse 3 times, and then I got what you mean. The natural reading is "expand them and ...". I think I have a fix. – ctrl-alt-delor Feb 01 '21 at 07:35
  • @ctrl-alt-delor : I reworded the "as is" ^^ thx for pointing it out. I am French, and sometimes I won't use the proper way to express things in English. – Olivier Dulac Feb 01 '21 at 22:21
  • I think the "as is" was fine. The problem was with the natural parsing order (left to right) cased a crash blosom (make the and attach to the wrong thing) – ctrl-alt-delor Feb 02 '21 at 12:05
  • @ctrl-alt-delor : thx for the link! (and for your edit!) – Olivier Dulac Feb 02 '21 at 12:13