Basic Use of FIND
If you are looking to find a file, one of the commonest tools is used Find. Here is a recap.
S/No | File Type | Description |
---|---|---|
1 | type -f | Limits search results to files only |
2 | type -d | Limits search results to directories only |
3 | type -l | Limits search results to symbolic links only |
For example, search for a case-insensitive file named “hello.mov”
find $HOME -type -iname "Hello.mov"
S/No | Parameters | Description |
---|---|---|
1 | -name | Perform a case-sensitive search for “files” |
2 | -iname | Perform a case-insensitive search for “files” |
3 | size +n | Matches files of size larger than size n |
4 | size -n | Matches files of size smaller than size n |
5 | -mtime n | Matches files or directories whose contents were last modified n*24 hours ago |
6 | -atime n | Matches files last access n*24 hours ago |
For example, search for all case-insensitive files with the extension *mov 2 days ago
find $HOME -type -iname "*.mov" -mtime 2
S/No | Operator | Explanation |
---|---|---|
1 | -and | Match for both sides of the operators |
2 | -or | Match for either test of the operators |
3 | -note | Don’t match the test of the operators |
For example, search for all files with Hello*, but excl ude pdf and jpg
find \( -name "Hello*" -mtime 2 \) -and -not \( -iname "*.jpg" -or -iname "*.pdf" \)
When using the () to combine tests, remember to escape the (\) brackets. You will need to leave a space after you open and close the brackets |
Using FIND and EXEC
find -type f -iname "*.mov" -exec chmod +x {} \;
The first part find -type f -iname”*.mov” will not be explained….. 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.
References:
- Linux Format – March Edition
- Use the Unix find command to search for files