17

I'm looking for shell scripts files installed on my system, but find doesn't work:

$ find /usr -name *.sh

But I know there are a ton of scripts out there. For instance:

$ ls /usr/local/lib/*.sh
/usr/local/lib/tclConfig.sh  
/usr/local/lib/tkConfig.sh

Why doesn't find work?

2

3 Answers 3

54

Try quoting the wildcard:

$ find /usr -name \*.sh

or:

$ find /usr -name '*.sh'

If you happen to have a file that matches *.sh in the current working directory, the wildcard will be expanded before find sees it. If you happen to have a file named tkConfig.sh in your working directory, the find command would expand to:

$ find /usr -name tkConfig.sh

which would only find files named tkConfig.sh. If you had more than one file that matches *.sh, you'd get a syntax error from find:

$ cd /usr/local/lib
$ find /usr -name *.sh
find: bad option tkConfig.sh
find: path-list predicate-list

Again, the reason is that the wildcard expands to both files:

$ find /usr -name tclConfig.sh tkConfig.sh

Quoting the wildcard prevents it from being prematurely expanded.

Another possibility is that /usr or one of its subdirectories is a symlink. find doesn't normally follow links, so you might need the -follow option:

$ find /usr -follow -name '*.sh'
4
  • What if the file is a shell script but lacks the .sh extension? Maybe something like find /xyz | xargs file | grep shell? Dec 14, 2015 at 17:52
  • How often, though, do shell scripts not have a .sh extension?
    – user4822346
    Dec 16, 2015 at 19:43
  • Is there any general way to figure out when/where a wildcard is being expanded?
    – isaacg
    Dec 21, 2015 at 9:51
  • Well, growing up in windows where everything has an extension I've seen quite a lot scripts on *ix omitting that .sh
    – Jan
    Dec 21, 2015 at 19:29
15

On some systems (Solaris, for example), there's no default action, so you need to add the -print command.

find /usr -name '*.foo' -print
1
  • 1
    I've been bit by that in the past. But I should point out that on a modern Solaris box, -print is on by default. In fact, I can't seem to find a find that doesn't work that way any more. Sep 8, 2008 at 22:49
8

For finding files on your disks, lean to use "locate" instead that is instantaneous (looks into a daily built index) you example would be:

locate '/usr*.sh'
2
  • A fine suggestion. (I had to run updatedb on my machine to test the idea, since I don't have it in my crontab for some reason. Not having an up-to-date database would be the main reason to not use locate.) Jan 27, 2009 at 18:39
  • And there are a few systems that don't have locate installed by default - I found I had to install the mlocate package on my Raspberry Pi - not forgetting the sudo updatedb afterwards to build the indexes...
    – SlySven
    Dec 24, 2015 at 22:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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