Find & Locate
FIND
Find is a linux search tool that can be used to find a variety of files based on the given criteria.
find [options] {search location} {search criteria} [actions]
| OPTION | Function |
| -type | Specify the file type for the search. Either f or d. |
| -name | Specify file/directory name to search for |
| -perm | Specify file permissions to search for. |
| -size |
Find files based on file size. |
| -user & -group |
Find files based on ownership properties. |
| -mmin -00 |
Find files that have been updated within a specified number of minutes. |
| ACTION |
FUNCTION |
|
Print the specific path of files found. |
|
| -exec |
Execute a command on the files found. |
| -delete |
Delete the files found. |
| -fprint |
Store the findings in a target file. |
Basic find
find / -type f -name hello.txt
wildcards
find / -name "*.txt"
Discard errors (ie permission denied)
find / -name "*.txt" 2>/dev/null
Find files with specific permissions
Find files with read,write, and execute permissions
find / -perm +rwx
Find files by size
Find files that are greater than 2MB:
find / -size +2M
Find files that are less than 2MB:
find / -size -2M
Find files that are exactly 2MB:
find / -size 2M
Find files by owner/group
Find files based on group
find / -group groupname
Find files based on owner
find / -user username
LOCATE
Locate is similar to find in its functionality, but it operates quite differently. Locate performs quick searches using its own mlocate database - a database of files that are stored on a server. The database needs to be regularly updated in order for the locate command to work accurately.
Update mlocate DB
updatedb
Locate file based on name
locate [options] test.txt
| Option | Function |
| -r | Search file names using regex |
| -c | Count the number of matching entries found |
| -e | Return only files that exist at the time of the search |
| -i | Ignore casing |
| --n [number of entries] | Retirn the first few matches up to the specified number. |
Additional configuration for locate command
The mlocate database used by the locate command is stored at;/var/lib/mlocate/mlocate.db
The configuration for this database is stored at;/etc/updatedb.conf
Within this config file, you can add exclusions for directories to be skipped within the mlocate DB, this is done using the below syntax;PRUNEPATH = "/path/to/exclude"
No Comments