Mark Lucernas
Sep 18, 2020

Find: Usage

Basic usage

find <directory> -name "pattern"

Match files with pattern

# Search for all .png files starting from current directory recursively
find . -name "*.png"

Match all files

find . -type f

Match all directories

find . -type d

Match all files excluding specific directory

# Excludes .git directory
find . -type f -not -path "*.git*" -name "*.png"
# or
find . \( -type d -name ".git" -prune \) -o -type f -name "*.png"

Do something to all matched files or directory

# chmod 644 all matched .png files recursively
find . -name "*.png" -print0 | xargs -0 chmod 644
# or
find . -name "*.png" -exec chmod 644 {} +

Do something to all matched files only

# chmod 644 all matched .png files recursively
find . -name "*.png" -type f -print0 | xargs -0 chmod 644
# or
find . -name "*.png" -type f -exec chmod 644 {} +

Do something to all matched files excluding directory

# chmod 644 all files recursively. Excludes .git directory
find . -type f -not -path "*.git*" -print0 | xargs -0 chmod 644
# or
find . \( -type d -name ".git" -prune \) -o -type f -print0 | xargs -0 chmod 644

Ref:


Resources