find command

How to use multiple filename search patterns with Linux find

Linux find FAQ: How do I use the Linux find command to find multiple filename extensions (patterns) with one find command?

Problem

You want to use the Unix/Linux find command to search for multiple filename types (or patterns). You know you can run the find command several times, one for each filename extension you're looking for, but there must be a way to search for multiple filenames at one time.

Linux find: How to search multiple directories with find

Problem: You need to use the Unix/Linux find command to search multiple folders. Specifically, you'd like to search several folders beneath your current location, but not all folders.

For example, your current directory may have 20 subdirectories, and you don't want to search them all like this:

find . -name "*.java"

or this:

A Linux find, mtime, exec ls, and sort command

It looked like one of our servers was getting hit pretty hard a while ago, and since we have about 100 sites on that server, I needed a way to find the largest log file. How to solve this? The Linux find command!

First, cd to your apache log file directory. Next, here's the command that showed me the access log files on our system, sorted in ascending order by size:

find . -name "access*" -mtime -1 -exec ls -ld {} \; | sort +4n

Ahh, brings tears of joy to my eye just looking at it. :)

 

Linux find, multiple directories, and exec grep

Yesterday I needed to find all the files in two subdirectories (/tmp and /home) on a Linux platform ending with the characters ".java", that contained the string "commit". Here's the little ditty that made that happen:

find /tmp /home -type f -name "*.java" -exec grep 'commit' {} \; -print

For finding files and directories on Unix systems (or Cygwin) the find command rules.