To provide Linux administrators and Field Engineers with guidance on using the find and locate utilities to efficiently search for files and directories within a Linux filesystem based on criteria such as name, type, size, timestamps, ownership, permissions, and directory depth.
This article covers the basic usage of the find and locate commands in Linux, including installation and database updates for locate, searching by file name and type, filtering by size and time, searching by owner and permissions, controlling search depth, combining search criteria, and executing commands on files returned by find. It is intended for routine file-search and filesystem administration tasks on Linux systems.
find command, which is available by default in virtually all Linux distributions. The second is the locate command, which must be installed separately before it can be used.The find command is part of Linux by default. It offers multiple parameters to refine your search.
The simplest searching command is to search by name where you are located:
You can specify the directory where you want to search for the file:
Take into consideration that this command is case sensitive. If you want to ignore the case use:
In case you want to search for files that do not adhere to the specified pattern you can negate the search parameters by using the -not parameter or \!. For example:
The file command also allows you to search for file types. Some common file type descriptors are:
f: regular filed: directoryl: symbol linkc: character devicesb: block devicesThe desired file type goes after the -type attribute.
For example, to find al JSON files under the etc directory:
There is also a parameter used to filter the search result depending on the size of the files: -size.
For the size units you can use the following suffixes:
c: bytesk: kilobytesM: megabytesG: gigabytesb: 512-byte blocksAnd to specify if you are talking about an exact size, a less than, or greater than use the following prefixes:
- prefix+ prefixFor example, to find all 3 gigabytes files under the home directory:
For all files under 3 gigabytes:
Files over 3 gigabytes:
You can also search files according to their last access, modification, or change times.
-atime for the last time the file was read.-mtime for the last time the file contents were modified.-ctime for the last time where the file's inode meta-data was changed.It is also possible to compare against a reference file to return the ones that are newer:
For the times, the parameters specified are in days. The same prefixes as in search by size can be used:
To find files using time in minutes you can use:
There are also parameters to find files according to their owners (-user and -group) and their permissions (-perm):
When searching under a directory, the find command will return all files under that directory, even if they are inside other subdirectories. To control this depth you can define a maximum depth, a minimum depth, or both.
For example, to search only on the top directory and one level of subdirectories you could set the -maxdepth parameter to 2:
To define the minimum depth where find should search you use the -mindepth parameter:
These parameters can be combined:
As saw in the depth commands, all find parameters can be combined to obtain very specific results. When you get the results you wanted it is possible to execute commands over them by using the -exec parameter.
When combining search parameters you can also use the -and and -or options to personalize your search criteria. When none of them are used, the -and option is the default.
For example, to change the permissions to all files under the /my_dir directory that currently have 777 permissions to 664 use the following:
The locate command is an alternative for find. The difference is that it uses a database of the files in the filesystem so it can perform quicker. To install the locate tool you can use:
Locate uses a cron job to update the database daily, but it can be manually updated by using:
For searching the "basename", namely that the query is contained only in the file's name and not in the file's path, use the -b flag:
To find files that still exist (after the last updatedb call) use the -e flag: