Find command in Linux and examples for everyday work

It is very important to be able to find all necessary information in Linux. Linux terminal search is the fastest and most flexible. So, we don’t need to install special tools for it since find utility is present in all Linux distributions. It can search for files by their name, regular expressions, creation date and content. In addition, it has the ability to execute your program or script. So, you can use found files as an argument for any program.

Basic information about find

Find is the main tool to find files and directories in Linux. It also can search by file properties. This is a really cool tool, it has no analogues.

The find command has the following syntax

find [directory] [options] tests pattern [action]

In this example,

Directory – the directory in which we search

Options — additional options, such as search depth, and so on.

Tests – by what test we will search for: name, creation date, rights, owner, etc.

Pattern – directly the value by which we will select files.

Basic options

Never open symbolic links:

-P

Get information about files by symbolic links. It is important for further processing so that the file is processed, and not a symbolic link

-L

Maximum search depth for subdirectories, to search only in the current directory, set 1.

-maxdepth <levels>

Search first in the subdirectories, and then in current directory

-depth or -d (synonym for -depth)

Search files only in this file system

-mount

Show version of find utility

-version

Show full filenames

-print

Search for files only

-type f

Search for folders only

-type d

Basic tests

For numeric arguments:

+n means greater than n,
-n means less than n,
n means exactly n.

Search files by name

-name <pattern>

The same but exception is case sensitivity

-iname <pattern>

File is empty and is either a regular file or a directory

-empty

Search files by access mode(must be octal or symbolic)

-perm <mode>

Search for files by owner

-user <username>

Search by group

-group <gname>

File was last accessed n days after its status was last changed

-used <n>

Search by last modification time (n*24 hours)

-mtime <n>

Search files by the last access date. It is in n*24 hours format. So, in simple terms n means the number of full days.

-atime <n>

Search for files that don’t belong to any group

-nogroup

Search for files without owners

-nouser

Find files newer than specified one

-newer <file>

Search for files by their size. It is in 512-byte blocks by default. Use c suffix for bytes, k for Kilobytes, M for Megabytes and G for Gigabytes

-size <n>

Examples of using

Find all files in the specified directory

find ./Music

Search files by name through current working directory

find . -name "*.jpg"

Ignore case when searching by name

find . -iname "jack*"

Search files by name only in this(current) directory

find . -maxdepth 1 -name "*.conf"

Invert. Find files that don’t match the specified name

find . -not -name "advertising"

Several tests. Search by several tests, with the exception operator. This will find all .mp4 videos but exception is ballet.

find . -name "*.mp4" -not -iname "*ballet*"

Operator OR (-o). It will find both png and jpg images

find -name "*.png" -o -name "*.jpg"

Search in multiple directories at the same time. It will search for the passwords file in the specified directories

find ./Where_did ./I_put_it -type f -name "passwords"

Find all hidden files in the home directory

find ~ -maxdepth 1 -type f -name ".*"

Find files from 500 megabytes to 1 gigabyte in the size

find ~ -size +500M -size -1G

Find the five smallest files, -exec option executes commands with arguments

find . -type f -exec ls -s {} \; | sort -n | head -5

And this will find the five largest files

find . -type f -exec ls -s {} \; | sort -n -r | head -5

Find all image files using regular expressions

find . -type f -regex ".*\.\(jpg\|jpeg\|gif\|png\|JPG\|JPEG\|GIF\|PNG\)"

Delete empty regular files

find ./Directory -type f -empty -delete

Print the files that contain the specified string

find ./Directory -type f -exec grep 'search string' /dev/null '{}' \+