You are here

The find command

Syntax for find

find . -name "*.php" -exec echo {} \;

To find mp3 files after 2009-01-26

touch -t 0901260000 /tmp/t
find /home -newer /tmp/t -name "*.mp3"

To find all text files containing the word “Pressflow”

find /home -name "*.txt" -exec grep -l "Pressflow" {} \;

To find all the files > 1Gb and print their paths & sizes

find /ednpdtu3/u01/pipe -type f -size +1048576 -printf "%s\t%h%f\n"

to chmod all directories to 755 and all files to 644

find . -type f -exec chmod 644 {} \;
find . -type d -exec chmod 755 {} \;

General purpose find script finddate.sh

#!/bin/bash
if [ $# -lt 1 -o "$1" == "--help" ]; then
  echo 'Find files between two dates. If second date is omitted, then use today'
  echo 'usage: finddate.sh path start-date [end-date] [command]'
  echo '  e.g. finddate.sh /home/ariadne 2012-01-01 2012-02-01'
  echo '  If end-date is omitted, then today is used'
  echo 'If a fourth argument is provided, it is used as the command to run against each file'
  echo '  e.g. finddate.sh /home/ariadne 2012-01-01 2012-02-01 "cp {} /tmp/newfiles"'
  echo '  The command defaults to "ls -lhtr {}"'
  exit
fi

if [ -n "$4" ]; then
  cmd=$4
else
  cmd="ls -lhtr {}"
fi

touch --date $2 /tmp/start
if [ -n "$3" ]; then
  touch --date $3 /tmp/end
  find $1 -type f -newer /tmp/start -not -newer /tmp/end -exec $cmd \;
else
  find $1 -type f -newer /tmp/start -exec $cmd \;
fi
Topic: