.. include:: ../header.txt .. _find: Finding Files =========================== :title:`Linux for Programmers and Users`, Section 4.5 .. index:: find, locate, whereis, type .. program:: find The :command:`find` command is a very powerful program to find files that match almost any combination of criteria imaginable. Find is useful for locating and correcting problems with files and also just to find a file that you can not remember where the file is. .. seealso:: Other commands that can find files are: :command:`locate`, :command:`whereis` and :command:`which`. .. describe:: locate Uses a database to find files names matching a search pattern very quickly. Files newer than the last time the database was will not be found. .. describe:: whereis Locates system provided source/binary and manuals sections for specified files. It only searches in common locations for files. .. describe:: which Searches your :envvar:`PATH` for programs matching executable programs. When finding a file in the `PATH`, it is convenient to use :command:`which` with command substitution. It will also report if a command has an alias definition. .. describe:: type A Bash shell builtin command. It produces similar output as the :command:`which` command. The output is intended for human reading, rather than for use with command substitution. Unlike :command:`which`, :command:`type` will report if a command is a shell built-in command. .. _find_cmd: :command:`find` ----------------- SYNOPSIS :command:`find` [-H] [-L] [-P] [path...] [expression] DESCRIPTION search for files in a directory hierarchy The :option:`find -H`, :option:`find -L` and :option:`find -P` options control the treatment of symbolic links. Command-line arguments following these are taken to be names of files or directories to be examined, up to the first argument that begins with '-', '(' , ')', ',' , or '!'. That argument and any following arguments are taken to be the expression describing what is to be searched for. .. option:: -P Never follow symbolic links. This is the default behaviour. .. option:: -L Follow symbolic links. .. option:: -H Do not follow symbolic links, except while processing the command line arguments. EXPRESSIONS .. option:: -depth Process each directory's contents before the directory itself. .. option:: -mount Don't descend directories on other filesystems. .. option:: -xdev Don't descend directories on other filesystems. Numeric arguments can be specified as: .. describe:: +n for greater than n, .. describe:: -n for less than n, .. describe:: n for exactly n. FILE MATCHING EXPRESSIONS .. option:: -amin n File was last accessed n minutes ago. .. option:: -anewer file File was last accessed more recently than file was modified. .. option:: -atime n File was last accessed n*24 hours ago. .. option:: -cmin n File's status was last changed n minutes ago. .. option:: -cnewer file File's status was last changed more recently than file was modified. .. option:: -ctime n File's status was last changed n*24 hours ago. .. option:: -empty File is empty and is either a regular file or a directory. .. option:: -gid n File's numeric group ID is n. .. option:: -group gname File belongs to group gname (numeric group ID allowed). .. option:: -ilname pattern Like -lname, but the match is case insensitive. .. option:: -iname pattern Like -name, but the match is case insensitive. .. option:: -inum n File has inode number n. It is normally easier to use the -samefile test instead. .. option:: -iregex pattern Like -regex, but the match is case insensitive. .. option:: -links n File has n links. .. option:: -lname pattern File is a symbolic link whose contents match shell pattern pattern. .. option:: -mmin n File's data was last modified n minutes ago. .. option:: -mtime n File's data was last modified n*24 hours ago. .. option:: -name pattern Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters ('*', '?', and '[]') match a '.' at the start of the base name. .. option:: -newer file File was modified more recently than file. .. option:: -nouser No user corresponds to file's numeric user ID. .. option:: -nogroup No group corresponds to file's numeric group ID. .. option:: -perm mode -perm mode File's permission bits are exactly mode (octal or symbolic). Since an exact match is required, if you want to use this form for symbolic modes, you may have to specify a rather complex mode string. It is more likely that you will want to use the '/' or '-' forms, for example '-perm -g=w', which matches any file with group write permission. -perm -mode All of the permission bits mode are set for the file. Symbolic modes are accepted in this form, and this is usually the way in which would want to use them. You must specify 'u', 'g' or 'o' if you use a symbolic mode. -perm +mode On some systems, this may use /mode instead of +mode. Any of the permission bits mode are set for the file. Symbolic modes are accepted in this form. You must specify 'u', 'g' or 'o' if you use a symbolic mode. .. option:: -regex pattern File name matches regular expression pattern. This is a match on the whole path, not a search. .. option:: -size n[bcwk] True if the file uses `n` units of space. `b` is for 512-byte blocks, `c` is for characters (bytes), `w` is for two-byte words, `k` is for kilobytes. .. option:: -samefile name File refers to the same inode as name. When -L is in effect, this can include symbolic links. .. option:: -true Always true. .. option:: -type c File is of type c: d -- directory f -- regular file l -- symbolic link; this is never true if the -L option or the -follow option is in effect .. option:: -uid n File's numeric user ID is n. .. option:: -used n File was last accessed n days after its status was last changed. .. option:: -user uname File is owned by user uname (numeric user ID allowed). ACTIONS (see :ref:`xargs`) .. option:: -delete Delete files .. option:: -exec -exec command ; Execute command; true if 0 status is returned. All following arguments to find are taken to be arguments to the command until an argument consisting of ';' is encountered. The string '{}' is replaced by the current file name being processed everywhere it occurs in the arguments to the command. -exec command {} + This variant of the -exec option runs the specified command on the selected files, but the command line is built by appending each selected file name at the end. .. option:: -print Print the full file name on the standard output. The GNU :command:`find` turns this option on by default, so it is not necessary to add this option. It is needed with the AT&T UNIX find program. .. option:: -print0 True; print the full file name on the standard output, followed by a null character (instead of the newline character that :option:`find -print` uses). This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. OPERATORS Listed in order of decreasing precedence: ( expr ) Force precedence. ! expr True if expr is false. -not expr Same as ! expr, but not POSIX compliant. expr1 expr2 Two expressions in a row are taken to be joined with an implied "and"; expr2 is not evaluated if expr1 is false. expr1 -a expr2 Same as expr1 expr2. expr1 -and expr2 Same as expr1 expr2, but not POSIX compliant. expr1 -o expr2 Or; expr2 is not evaluated if expr1 is true. expr1 -or expr2 Same as expr1 -o expr2, but not POSIX compliant. .. _xargs: :command:`xargs` ----------------- .. index:: xargs .. program:: xargs The :command:`xargs` command works very well along with :command:`find` to execute a command for each file found. SYNOPSIS :command:`xargs` [OPTIONS] [command [initial-arguments]] DESCRIPTION :command:`xargs` reads items from the standard input, delimited by blanks (which can be protected with double or single quotes or a backslash) or newlines, and executes the command (default is /bin/echo). Because Unix filenames can contain blanks and newlines, this default behaviour is often problematic; filenames containing blanks and/or new lines are incorrectly processed by xargs. In these situations it is better to use the :option:`xargs -0` option, which prevents such problems. When using this option you will need to ensure that the program which produces the input for xargs also uses a null character as a separator. If that program is GNU :command:`find` for example, the :option:`find -print0` option does this for you. .. option:: --arg-file=file, -a file Read items from file instead of standard input. .. option:: --null, -0 Input items are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). .. option:: --delimiter=delim, -d delim Input items are terminated by the specified character.