mac terminal find command recursive

For reference, Find command documentation on apple

Find command lets you search a particular file or files depending on the criteria. Some of the things about find command may not be intuitive. I will explain it here

  • Find command is case sensitive. That is *.JPG and *.jpg will be interpreted as different files.
  • Find Command is recursive by nature. It does not need any switch to force looking into subdirectories.

Usage or Syntax

find [path/directory] -name [pattern]

directory = directory (or path) name where you want to perform search.
-name = is going to match the [pattern] to the filename (and path). Not that this is case sensitive. On the other hand -iname is case insensitive
[pattern] = quoted string, that can contain *, ? or (escape) characters.

In mac terminal and other operating systems, a single dot (.) is used for current folder. And ~ (tilde) is used to user home directory (this is specific to mac). ~ = /Users/username/

Example usage of find

find . -name *.mp3  # this will find all files in current directory(.)
that has mp3 extension. But will not match .MP3 because it is case sensitive.

find . -iname *.mp3 # will find all mp3 files 
regardless of case sensitivity

Using Regular Expression in Find

Find also supports regular expression (RegEx in short). The syntax is similar to string search.

find [directory] -regex [pattern]

In this case the pattern is a valid regex pattern. Otherwise you will get no results.

RegEx is a separate topic in itself. You may find the documentation online.

 

Example of Regular Expression in Mac Terminal

find . -iregex "[a-z/. ]*"

[a-z] is pseudo class used in regular expression. This means A-Z characters (all 26 included).

/ = The file name (and path) may contain backspace character ().

. = The file name may contain . character.

[space] = There is a space character after (.). This means the filename may contain a space character.
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s