Blog

Using find on Linux

Basic search with find:

find /directory -name 'search term'

Search files in subdirectories of current directory:

find . -name 'search term'

Find large files:

find / -type f -size +100000 -exec ls -lh {} \;

Search inside file contents:

find . | xargs grep --color=auto -iR 'search term' *

or

find . -type f -exec grep -lR 'search term' {} /dev/null \;

Find files modified in certain time period:

find . -name 'search term' -mtime -1 -print

Exclude certain directories in search:

find . -name 'search term' -prune -o -name 'excluded directory1' -prune -o -name 'excluded directory2' -prune -o -type f -print

Find files older than a certain date (2 days in example) & delete:

find . -mtime +2 -exec rm {} \;

Find and replace in files:

find ./* -type f -exec sed -i 's/search term/replacement/g' {} \;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>