3pc9pa
Last Updated: February 25, 2016
·
793
· poupougnac

Find files on Linux with... find

Find is a very useful command-line tool to find some files and execute commands. Here are some examples of use which should help you getting started with what you want to achieve.

You know what this one do :

find . -name "*.txt"

Find files edited less than 10 minutes :

find / -mmin -10

Between 24 and 48h :

find . -mtime 1 

More than 48h :

find . -mtime +1

 Less than 48h :

find . -mtime -1

Use atime for days.

 You can use several criteria :

AND : criteria1 -a criteria2
OR : \ (criteria1 -o criteria2)
NOT : !criteria

Example :

find . \  ( -name a.out -o -name " *.c " \  ) -print

And even use regexp :

Delete all images like image-123x123.jpg

find . -iregex "\(.*\)[\-][0-9]+x[0-9]+\.\(jpg\|png\)" -exec rm -v {} \;

To resize some images (like wordpress images)

find . -iregex ".*\.jpg" -not -iregex "\(.*\)[\-][0-9]+x[0-9]+\.jpg" -exec bash -c '[ -f ${1//.jpg}-110x110.jpg ] || { convert $1 -resize 110x110^ -gravity center -crop 110x110+0+0 $1-110x110.jpg ; rename "s/\.jpg-110x110/-110x110/i" $1-110x110.jpg ; } ' - {} \;

 Resize to a larger which won't exceed 630px (use ImageMagick)

find . -iregex "\(.*\)[\-][0-9]+x[0-9]+\.\(jpg\|png\)" -exec convert -resize 630x\> {} {} \;