Find and locate commands

F

In UNIX operating systems, the find command allows you to search for a file through one or more directories. The search is based on a set of user-specified criteria. Possible criteria include a pattern or a time frame when the file was created or modified. The result of the command is a list of filenames that meet the specified criteria.

The find command is used to search for any kind of file (executable, text, source code) in a specific directory or on the entire hard drive in the directories where the user has the right to read.
If you will search on the entire hard disk, then the path is indicated by /, which means that searching starts from the root and continues descending in all directories and sub directories.
The parameter may be -perm, -name, -time, -user, depending on what is being searched: file name, files with a specific permission after the last access, files belonging to a particular user, etc.

The simplest search is by name. If you search from the root, you will list the standard output and the errors due to restrictive permissions to the different directories that the search goes through.
If you do not know the exact file name, you can also search by partial name.

Files can also be searched for by a specific feature such as size, date of last visit, owner or group to which it belongs. For example, to look for a file of a certain size, use the size option.

Other search orders:

1. find . -min -60 Finds files accessed in the last hour.
2. find / -time -3 Finds files accessed in the last 3 days.
3. find / -mmin -30 Finds all modified files in the last 30 minutes.
4. find / -mtime -2 Find all modified files in the last 2 days
5. find / -empty Find all empty files and directories.
6. find / -group xxx Finds all files belonging to the xxx group.
7. find / -user aaa Find all files belonging to the user aaa.
8. find / -nouser Finds all files that have a non-existent user as owner.

The locate command uses an indexed file database. The database is created using the find command and is updated at regular time intervals, typically with a cron job. This provides a faster search method, but does not have the accuracy of the find command.

The locate command works roughly the same as find to find a file by its name but differs from two important points of view:

1. The locate tool is less sophisticated than find. It is normally used to search only for a file name. The program returns all the files containing the specified character string. For example, when looking for pacman, locate will return us all files and directories that have the pacman character string inside their name.
2. To locate files, the locate command searches in a database. Most Linux distributions include a cron job for updating this database. Because of this, locate may not find recent files or may return filenames that no longer exist. However, you can use the updatedb command to perform this task at any time (updatedb has the configuration file in /etc/updatedb.conf). locate command works with a database and it is much faster than find command.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories