find

The command find allows to recursively descend a series of directories while trying to apply a given command to files specified by one or more selection criteria (name, type, date, ...).

find [directory] [expression]
  • [directory] is a set of directories to run through

  • [expression] is a set of options that expresses both file selection criteria and actions to apply to those files. When the criteria is true, action is executed.

By default, the command find recursively descend the current directory and displays all files.

find options meaning
-name motif true if motif matches the current file
-user name true if the current file belongs to name
-mtime n true if the file was last modified n days ago (exactly)
-mtime +n true if the file was last modified more than n days ago
-mtime -n true if the file was last modified less than n days ago
-newer file true if the current file is newer than the reference file
(expression) true if expression is true
-print always true, display the name of the file
-ls always true, apply the command ls -l to the current file
-exec command true if the command command returns a zero code
The command is terminated by the mark \;
and the special parameter {} indicates the current file

Expression elements can be connected using the following operators, in the decreasing priority order:

  • negation operator: !

  • and operator: the simple juxtaposition of the expressions

  • or operator: -o

Examples:

#find files that starts with f and are less than 1 day old
find . -name "f*" -mtime -1 -print

Double quotes ("f*") are here used to prevent the bash shell to expand the * character with file names in the current directory. "f*" can be replace by f\*.

#change permission access to all files (and directories) older than 1 day
find -mtime +1 -exec chmod o-rwx {} \;