The find command in UNIX is a command line utility for walking a file hierarchy. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’ other UNIX commands can be executed on files or folders found.
Syntax :
$ find [where to start searching from] [expression determines what to find] [-options] [what to find]
Options :
- -exec CMD: The file being searched which meets the above criteria and returns 0 for as its exit status for successful command execution.
- -ok CMD : It works same as -exec except the user is prompted first.
- -inum N : Search for files with inode number ‘N’.
- -links N : Search for files with ‘N’ links.
- -name demo : Search for files that are specified by ‘demo’.
- -newer file : Search for files that were modified/created after ‘file’.
- -perm octal : Search for the file if permission is ‘octal’.
- -print : Display the path name of the files found by using the rest of the criteria.
- -empty : Search for empty files and directories.
- -size +N/-N : Search for files of ‘N’ blocks; ‘N’ followed by ‘c’can be used to measure size in characters; ‘+N’ means size > ‘N’ blocks and ‘-N’ means size < ‘N’ blocks.
- -user name : Search for files owned by user name or ID ‘name’.
- \[expr \] : True if ‘expr’ is true; used for grouping criteria combined with OR or AND.
- ! expr : True if ‘expr’ is false.
Examples : Consider the following tree hierarchy :
1. Search a file with specific name.
$ find ./GFG -name sample.txt
It will search for sample.txt in GFG directory.
Output :
2. Search a file with pattern.
$ find ./GFG -name *.txt
It will give all files which have ‘.txt’ at the end.
Output :
3. How to find and delete a file with confirmation.
$ find ./GFG -name sample.txt -exec rm -i {} \;
When this command is entered, a prompt will come for confirmation, if you want to delete sample.txt or not. if you enter ‘Y/y’ it will delete the file. Output :
4. Search for empty files and directories.
$ find ./GFG -empty
This command find all empty folders and files in the entered directory or sub-directories.
Output :
5. Search for file with entered permissions.
$ find ./GFG -perm 664
This command find all the files in the GFG directory or sub-directory with the given permissions.
Output :
6. Search text within multiple files.
$ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;
This command print lines which have ‘Geek’ in them and ‘-type f’ specifies the input type is a file.
Output :
?list=PLqM7alHXFySFc4KtwEZTANgmyJm3NqS_L
To use the find
command, at the Unix prompt, enter:
find . -name "pattern" -print
Replace "pattern"
with a filename or matching expression, such as "*.txt"
. [Leave the double quotes in.]
Options
The general form of the command is:
find [starting directory] [matching criteria and actions]
The find
command will begin looking in the starting directory you specify and proceed to search through all
accessible subdirectories. You may specify more than one starting directory for searching.
You have several options for matching criteria:
-atime n
| File was accessed n days ago
|
-mtime n
| File was modified n days ago
|
-size n
| File is |
-type c
| Specifies file type: f=plain text, d=directory |
-fstype typ
| Specifies file system type: 4.2 or nfs |
-name nam
| The filename is nam
|
-user usr
| The file's owner is usr
|
-group grp
| The file's group owner is grp
|
-perm p
| The file's access mode is |
You can use +
[plus] and -
[minus] modifiers with the atime
, mtime
, and size
criteria to increase their usefulness, for example:
-mtime +7
| Matches files modified more than seven days ago |
-atime -2
| Matches files accessed less than two days ago |
-size +100
| Matches files larger than 100 blocks [50KB] |
By default, multiple options are joined by "and". You may specify "or" with the -o
flag and the use of grouped parentheses. To match all files modified more than 7 days ago and accessed more than 30 days ago, use:
\[ -mtime +7 -atime +30 \]
To match all files modified more than 7 days ago or accessed more than 30 days ago, use:
\[ -mtime +7 -o -atime +30 \]
You may specify "not" with an exclamation point. To match all files ending in .txt
except the file notme.txt
, use:
\! -name notme.txt -name \*.txt
You can specify the following actions for the list of files that the find
command locates:
-print
| Display pathnames of matching files. |
-exec cmd
| Execute command |
-ok cmd
| Prompt before executing the command |
-mount
| [System V] Restrict to file system of starting directory. |
-xdev
| [BSD] Restrict to file system of starting directory. |
-prune
| [BSD] Don't descend into subdirectories. |
Executed commands must end with \;
[a backslash and semi-colon] and may use {}
[curly braces] as a placeholder for each file that the find
command locates. For example, for a long listing of each file found, use:
-exec ls -l {} \;
Matching criteria and actions may appear in any order and are evaluated from left to right.
Full examples
- To find and report all C language source code files starting at the current
directory, enter:
find . -name \*.c -print
- To report all files starting in the directories
/mydir1
and/mydir2
larger than 2,000 blocks [about 1,000KB] and that have not been accessed in over 30 days, enter:find /mydir1 /mydir2 -size +2000 -atime +30 -print
- To remove [with prompting] all files starting in the
/mydir
directory that have not been accessed in over 100 days, enter:find /mydir -atime +100 -ok rm {} \;
- To show a long listing starting in
/mydir
of files not modified in over 20 days or not accessed in over 40 days, enter:find /mydir \[-mtime +20 -o -atime +40\] -exec ls -l {} \;
- To
list and remove all regular files named
core
starting in the directory/prog
that are larger than 500KB, enter:find /prog -type f -size +1000 -print -name core -exec rm {} \;
Note:
On some systems, the name of the starting directory must end with a
/
[slash], or thefind
command will return nothing. Thus, the starting directory in the previous example would be designated as/prog/
, with a trailing slash. On other systems, a trailing slash does not affect the command. A trailing slash is never needed when searching in/
[the root directory],.
[the current directory], or..
[the parent directory].
For more, consult the Unix manual page by entering at the Unix prompt:
man find
Some of the above information came from Essential System Administration, Aeleen Frisch [O'Reilly & Associates, Inc., 1991].
Related documents
This is document admm in the Knowledge Base.
Last modified on 2019-06-18 14:44:43.