find is one of the more powerful and flexible command-line programs in the daily toolbox, so it's worth spending a little more time on it.
At a minimum,
find takes a path to find things. For example:
will find (and print) every file on the system. And since everything is a file, you will get a lot of output to sort through. This probably doesn't help you find what you're looking for. You can change the path argument to narrow things down a bit, but it's still not really any more helpful than using the
ls command. So you need to think about what you're trying to locate.
Perhaps you want to find all the JPEG files in your home directory. The
-name argument allows you to restrict your results to files that match the given pattern.
find ~ -name '*jpg'
But wait! What if some of them have an uppercase extension?
-iname is like
-name, but it is case-insensitive.
find ~ -iname '*jpg'
Great! But the 8.3 name scheme is so 1985. Some of the pictures might have a .jpeg extension. Fortunately, we can combine patterns with an "or," represented by
find ~ ( -iname 'jpeg' -o -iname 'jpg' )
We're getting closer. But what if you have some directories that end in jpg? (Why you named a directory
bucketofjpg instead of
pictures is beyond me.) We can modify our command with the
-type argument to look only for files.
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f