The find command


lupa.jpg

On Linux we can perform file searching in many ways. For Instance, from Nautilus (the Gnome’s file manager) we can search on the current directory for all mp3′s files using this expression ‘*.mp3′. But if we need to do more complicated searches, for example all mp3′s files greater than 5 MB, Nautilus is not the answer. In this cases we have to use a versatile and powerful command: find.

Using find

Find lets us make file searching on the system according to an expression. The basic syntax is:

find path expression

Where expression is made up of options (change general behavior) , tests (to return true or false) and actions (for processing the files found)

Lets see some options, tests and actions…

Options

-maxdepth n Descend  at  most  levels
-mount Don’t  descend  directories on other filesystems

Tests

n for exactly n
+n for greater than n
-n For less than n
-amin n File was last accessed n minutes ago
-anewer file File  was last accessed more recently than file was modified
-atime n File was last accessed n*24 hours ago
-cmin n File’s status was last changed n minutes ago
-cnewer file File’s status was last changed more recently than file was modified.
-ctime n File’s  status  was  last  changed  n*24 hours ago
-mmin n File’s data was last modified n minutes ago
-newer file File  was  modified more recently than file
-mtime n File’s data was last modified n*24 hours ago
-empty File is empty and is either a regular file or a directory
-executables Matches files which are executable and directories which are searchable
-readable Matches files which are readable
-fstype type File is on a filesystem of type type
-uid n File’s numeric user ID is n
-user user_name File is owned by user user_name
-gid n File’s numeric group ID is n
-group group_name File belongs to group group_name
-name pattern Base  of  file  name  (the  path  with the leading directories removed) matches shell pattern pattern
-nogroup No group corresponds to file’s numeric group ID
-nouser No user corresponds to file’s numeric user ID
-perm mode File’s permission bits are exactly mode (octal or symbolic)
-perm -mode All  of  the permission bits mode are set for the file
-perm /mode Any  of the permission bits mode are set for the file
-size n[cwbkMG] File uses n units of space
-type c File is of type c (block, character, directory, symbolic link, regular file, etc.

Actions

-delete Delete  files;  true  if removal succeeded
-exec comando ; Execute command; true if 0 status is returned
-exec comando ‘{}’ \; This  variant of the -exec action runs the specified command on the selected files, but the command line is built by appending each selected file name at the end

Examples

find /home -name ‘*.bash*’ Any file which name contains .bash in its name, on the /home directory
find $HOME -mtime 0 Files on user’s home directory modified less than 24 hours ago
find -perm 644 Search for files which have read and write permission for their owner, and group, but which other users  can  read but not write to.  Files which meet these criteria but have other permissions bits set (for example if someone can execute the file) will not be matched
find -perm -644 Search for files which have read and write permission for their owner and group, and which other users  can  read, without  regard  to the presence of any extra permission bits (for example the executable bit).  This will match a file which has mode 0777, for example.
find -perm /222 Search for files which are writable by somebody (their owner, or their group, or anybody else)
find -perm /u=w Same than above example
find /home -nouser -exec rm -r ‘{ }’ \; Delete files on /home directory that belongs to nobody
find /home -size +1G Search for files greater than a 1 GB on /home
find / -size +1G -user lgallard Search for files greater than a 1 GB on /home which belongs to lgallard

References

,

  1. #1 by Luis Gallardo on 27/03/2010 - 7:24 am

    @antonio Buscar todos los archivos en el directorio actual, sin importar que tenga mayúsculas o minúsculas (un poco sin sentido, ya que usas el comodín *)

  2. #2 by antonio on 27/03/2010 - 5:41 am

    ¿que hace el comando find . iname *?

(will not be published)