find command in linux

Find Command in Linux

GNU/Linux (in particular, the Bash console shell) allows you to search for files on a system with a variety of parameters that are not available in well-known Windows. The find command is responsible for this. Its popularity cannot be overestimated, since the speed of its work, combined with the flexibility of parameterization, forces any user to study it in a more detailed way.

Syntax and Options of Find Command in Linux

The find command is an integral part of any GNU/Linux distribution. It can search files by various criteria, such as the owner, type, size, and all other properties possessed by any file.

This utility has the following syntax:

find [directory] [options] criteria pattern [action]

where directory is the search location, options are additional instructions for the type of search depth, etc., criteria is the necessary file property for the search (name, owner, type, etc.), pattern is the value directly to which the search will be oriented, action indicates additional actions for the found files.

The main find options are listed in the table below:

Option Value
-P symbolic links will not be displayed
-L get data about files from symbolic links. It is necessary for subsequent processing of files using these links
-maxdepth setting the maximum search depth in subdirectories. To limit only to the current directory, the value 1 is used
-depth primary search is performed in the current directory, and then in subdirectories
-mount search for files only in the current file system
-version display version of find
-print show full object names
-type f search exclusively files
-type d search directories only

The following table shows the main search criteria:

Criterion Value
-name by name
-perm by permission access
-user by owner
-group by group
-mtime by object modification time
-atime by last read date
-nogroup without belonging to any group
-nouser without owner
-newer newer than the specified object
-size by size

Linux Find Command Examples

The following are typical examples of using the find utility using various options and criteria.

Search for All Objects

In fact, these commands are synonymous with each other. They display all files in the current directory, including all subdirectories:


find .

find . -print

Search in a Specific Directory

Shows found objects in the specified folder:

find /tmp

find command in linux

Search for objects using the specified regular expression pattern (if only letters are specified, you can omit quotation marks):

find /tmp -name *apt*

linux find file

Same search, but not case sensitive:

find /tmp -iname *apt*

linux search for file

Search Depth Limit

Finds objects only in the current directory, without looking at subdirectories:

find . -maxdepth 1 -name “*.php”

linux find file by name

Inverted Pattern Search

Find only files in the current directory that do not fit the specified pattern:

find . -maxdepth 1 -type f -not -name “*.php”

linux find directory

Search in Several Directories

Searches for the specified folders at the same time:

find ./wp-admin ./wp-content -type f -name “*index*”

linux find large files

Search for Hidden Objects

Finds all files whose names begin with a dot (.):

find . -type f -name “.*”

linux find folder

Search by Permissions

Finds all objects by the specified access mask:

find . -perm 0666

linux find example

Finds all objects available for execution to all users:

find . -maxdepth 1 -perm /a=x

linux find command examples

Actions with Found Objects

The find utility can execute various terminal commands with found objects, which allows several commands to fit into one. To do this, use the -exec expression, curly braces {}, which indicate the substitution location of the found files, and \; at the end of the expression.

Find objects that are larger than 2 MB and show detailed information about them:

find . -maxdepth 2 -size 2M

find file by name linux

Remove all files in the directory containing the text specified in the template:

find /tmp -type f -iname "*apt*" -exec rm -f {} \;

Remove all objects from the specified directory that are larger than 200 MB:

find /tmp -size +200M -exec rm -f {} \;

Add Your Comment