Find and grep commands for searching

find command finds files by file name, path or file attributes. grep command searches for the text inside files.

Table of Contents:

find grep

find examples:

Find files named core

find . -name core

Find files starting with core in or below the current directory

find . -name core*

Find files with permission 664 in or below the /temp directory

find /temp -perm 664

Search for files in your home directory which have been modified in the last twenty-four hours.

find $HOME -mtime 0

Find all files in /var/www folder being accessed in the last 50 minutes

find /var/www -amin 50

Find all files in /var/www folder being modified (content) in last 50 minutes. If the file has been modified then the folder container will be reported

find /var/www -mmin 50

Find all files in /var/www folder being changed from the perspective of access rights chmod

find /var/www -cmin 50

Find command with xargs command for printing full information for all files in /var/www changed in last 50 min

find /var/www -type f -cmin 50 | xargs ls -l

Find all files with php extension that contain Class and return all lines

find /var/www -name "*.php" | xargs grep "Class"

Find last 20 modified files

find /var/www/ -printf '%T+ %p\n' | sort -r | head -20`

grep examples:

Find “some text” text string in file file_name

grep "some text" file_name

Find string in file case insensitive

grep -i 'string' FILE

Find string ‘string’ recursive inside /etc folder

grep -r 'string' /etc

List the name of matching files

grep -l 'main' *.c

tags: searching - find - grep & category: linux