The find command is similar to the ls command, but provides more options and capabilities.  

find files by type

The -type operator takes 'f' or 'd' to only output regular files or directories. The following only finds regular files.

            $ find -type f

find directory types

The following only finds directory files.

                $ find -type d

find files by name

The -name operator takes a regular expression and uses that against the filename. The following finds files named "*.java"

            $ find -name "*.java"

find files not named

The ! operator negates operand to the right. The following is an example of finding files not named *.txt

                $ find . -type f ! -name "*.txt" -print

find with grep

Using pipes and xargs, the grep command can works on each file found by the find command. The following finds files named "*.java", pipes that output to xargs, and then greps for the string "import org.gnu".

                $ find . -type f -name "*.java" -print0 | xargs -0 grep "import org.gnu"

find and sort the largest 15 files under a directory

                $ find . -type f -printf '%s %p\n' | sort -nr | head -n 15

find regular files and print filesize in ascending order with the file path

find -type f -printf "%f  %s %p\n"|sort

find regular files and print ordered by the last modified date

find . -type f -printf "%T@ %Tc %p\n" | sort -n

Missing -printf on Mac OS X

Resolve the issue that shows the following message while trying to use the -printf operator by installing fineutils:

find: -printf: unknown primary or operator

brew reinstall findutils

Once findutils is installed, the -printf is used with the gfind command.

Date Created: 2015-08-22 09:47:41 -0500 (Sat, 22 Aug 2015)

back    |    top